📜 ⬆️ ⬇️

Snacks from the new PHPixie Template

image
Today another PHPixie library has become available, this time it is a templating engine. As in the previous version, PHP itself is used as a language, but now new features have been added, including a blocking approach to template inheritance. We will talk about this later, and now about the most delicious: compilation of arbitrary formats. PHPixie Template allows you to use almost any format, you just need to provide a compiler that translates it in PHP, and file uploading, caching, and so on. will be done for you. Also, this means that you can simultaneously use multiple formats in one project.

So let's try to make Template with HAML. For this we use the package mthaml / mthaml


//composer.json { "require": { "phpixie/template": "3.*@dev", "phpixie/slice": "3.*@dev", "mthaml/mthaml": "1.7.0" } } 

')
Create the simplest wrapper:

 <?php class HamlFormat implements \PHPixie\Template\Formats\Format { protected $mtHaml; public function __construct() { $this->mtHaml = new \MtHaml\Environment('php'); } //      public function handledExtensions() { return array('haml'); } //  public function compile($file) { $contents = file_get_contents($file); return $this->mtHaml->compileString($contents, $file); } } 


It remains only to transfer our new format through the constructor to the Template itself:

 //       //   ,     $slice = new \PHPixie\Slice(); $config = $slice->arrayData(array( 'resolver' => array( 'locator' => array( //   'directory' => __DIR__.'/templates/', 'defaultExtension' => 'haml', ) ), 'compiler' => array( //    'cacheDirectory' => > __DIR__.'/cache/', ) )); $template = new \PHPixie\Template($slice, $config, array(), array( new HamlCompiler() )); 


Now all haml templates will be automatically compiled and cached. In this way, you can invent or adapt any language, while maintaining all the other features of the library, such as inheritance and extensions.

Inheritance
In the previous version, the only way to connect one template to another was include . That is, the rendering of the page had to go from top to bottom by connecting the necessary templates along the way. The disadvantage of this approach is that the connected template can not change anything in the parent. And now let's see what is available to us now:

 // layout.php <html> <title> <?php $this->block('title'); ?> </title> <body> <?php $this->childContent(); ?> </body> </html> 


 // fairy.php <?php $this->layout('layout'); ?> <?php $this->startBlock('title'); ?> Fairy page <?php $this->endBlock(); ?> <h2>Hello <?php $_($name); ?></h2> 


The fairy.php template extends layout.php with the ability to override blocks in the parent template. For those who use Twig, this approach is very familiar. For those who prefer to use include it is possible further:
 include $this->resolve('fairy'); 


Search template
Template engines often have a template search mechanism, where if another template is not found, another one is used instead. Often this is necessary when creating new themes based on old ones. Instead of one approach, PHPixie Template allows you to customize your search using combinations of 3 locators:



Take a look at this config:
 <?php $config = $slice->arrayData([ 'resolver' => [ 'locator' => [ 'type' => 'prefix', 'locators' => [ 'Site' => [ 'directory' => __DIR__.'/site/', ], 'Theme' => [ 'type' => 'group', 'locators' => [ [ 'directory' => __DIR__.'/templates/', ], [ 'directory' => __DIR__.'/fallback/', ], ] ] ] ] ] ]); 


Thus, the Site :: layout template will be searched in the site / folder, and Theme :: home in the templates / and fallbacks / folders.

Extensions
Extensions are classes that add extra features to templates. As an example, take a look at the HTML extension that is used to display and escape lines.

 class HTML implements \PHPixie\Template\Extensions\Extension { public function name() { return 'html'; } //  \      public function methods() { return array('escape', 'output'); } //         //      $_($name)  $this->output($name) public function aliases() { return array( '_' => 'output' ); } //  public function escape($string) { return htmlspecialchars($string, ENT_QUOTES, 'UTF-8'); } public function output($string) { echo $this->escape($string); } } 


Such extensions analogue formats can be passed through the constructor.

Demo
To try a do-it-yourself Template is enough:
 git clone https://github.com/phpixie/template cd template/examples #      curl -sS https://getcomposer.org/installer | php php composer.phar install php quickstart.php 


And by the way, like all the other libraries from PHPixie, you will receive 100% code coverage with tests and work under any version of PHP older than 5.3 (including the new 7 and HHVM)

Source: https://habr.com/ru/post/253973/


All Articles