PHP code example of phpixie / template

1. Go to this page and download the library: Download phpixie/template library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phpixie / template example snippets


<!--layout.php-->
<html>
    <title>
         $this->block('title'); 

<!--fairy.php-->
 $this->layout('layout'); 

echo $template->render('fairy', array('name' => 'Pixie'));



$config = $slice->arrayData([
    'resolver' => [
        'locator' => [
            'type' => 'prefix',
            'locators' => [
                
                'Site' => [
                    'directory' => __DIR__.'/site/',
                ],
                    
                'Theme' => [
                    'type' => 'group',
                    'locators' => [
                        [
                            'directory' => __DIR__.'/templates/',
                        ],
                        
                        [
                            'directory' => __DIR__.'/fallback/',
                        ],
                    ] 
                ]
            ]
        ]
    ]
]);

class HTML implements \PHPixie\Template\Extensions\Extension
{
    public function name()
    {
        return 'html';
    }
    
    //Methods we would like to expose
    public function methods()
    {
        return array('escape', 'output');
    }
    
    //Also we can assign aliases to some methods, meaning that they will also
    //be assigned to a specified local variable
    //In this case it allows us to use $_($name) instead of $this->escape($name)
    public function aliases()
    {
        return array(
            '_' => 'escape'
        );
    }
    
    public function escape($string)
    {
        return htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
    }
    
    public function output($string)
    {
        echo $this->escape($string);
    }
}

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

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

$slice = new \PHPixie\Slice();

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

$template = new \PHPixie\Template($slice, $config, array(), array(
    new HamlCompiler()
));