2017 © Pedro PelĂĄez
 

library template

Templating library for PHPixie

image

phpixie/template

Templating library for PHPixie

  • Friday, February 16, 2018
  • by dracony
  • Repository
  • 3 Watchers
  • 3 Stars
  • 10,930 Installations
  • PHP
  • 7 Dependents
  • 0 Suggesters
  • 5 Forks
  • 0 Open issues
  • 10 Versions
  • 6 % Grown

The README.md

Template

Build Status Test Coverage Code Climate HHVM Status, (*1)

Author Source Code Software License Total Downloads, (*2)

PHPixie Template uses PHP as the templating language, but can also handle layouts, content blocks, custom extensions and even custom formats. It’s super easy to use it to parse HAML, Markdown or whatever else you like. All you need to do is provide a compiler that will translate your format into a plain PHP template, the library will take care of caching the result, updating it when the files are modified etc. by itself., (*3)

Inheritance
It’s pretty inuitive to understand template inheritance, especially if you used Twig or even Smarty. Here is a quick example:, (*4)


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

    </body>
</html>
``````php

layout('layout'); ?>
startBlock('title'); ?>
Fairy page
endBlock(); ?>



Hello =$_($name) ?>

Now lets render it:, (*5)

echo $template->render('fairy', array('name' => 'Pixie'));
<html>
    <title>Fairy page</title>
    <body>
        <h2>Hello Pixie</h2>
    </body>
</html>

You can also include a subtemplate to render a partial:, (*6)

include $this->resolve('fairy');

Template name resolution
Usually templating libraries have some way of providing fallback tempates, that are used if the template you wan’t to use does not exist. And usually they handle it via some naming convention. PHPixie alows you to fine tune name resolutiion using 3 locator types:, (*7)

  • Directory – Maps template name to a folder location, simplest one
  • Group – allows you to specify an array of locators. The template will be searched in those locators one by one until it’s found. This is used for providing fallbacks
  • Prefix – allows you to route the resolution based on a prefix

This sounds more complex then it actually is, so let’s look at an example, assume the following config:, (*8)

$config = $slice->arrayData([
    'resolver' => [
        'locator' => [
            'type' => 'prefix',
            'locators' => [

                'Site' => [
                    'directory' => __DIR__.'/site/',
                ],

                'Theme' => [
                    'type' => 'group',
                    'locators' => [
                        [
                            'directory' => __DIR__.'/templates/',
                        ],

                        [
                            'directory' => __DIR__.'/fallback/',
                        ],
                    ] 
                ]
            ]
        ]
    ]
]);

It means that Site::layout template will be searched for in the site/ folder, while the Theme::home one will be searched for in templates/ and fallback/., (*9)

When using the PHPixie Framework you define your locator in the templateLocator.php configuration file. Your locator will be prefixed using the name of your bundle., (*10)

Extensions, (*11)

Extensions provide additional methods that you may use inside your views, they are helpful for things like formatting and escaping. As an example let’s look at the HTML extension:, (*12)

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);
    }
}

Your Extensions then have to be injected in the library constructor just like Formats., (*13)

Creating a custom format

So let’s try integrating it with Markdown, we’ll use mthaml/mthaml for that:, (*14)

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

And here is our compiler:, (*15)

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);
    }
}

And now let’s inject it into Template:, (*16)

$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()
));

That’s it, we can now use HAML for our templates while retaining all the original features like Extensions and inheritance., (*17)

The Versions

16/02 2018

dev-master

9999999-dev http://phpixie.com

Templating library for PHPixie

  Sources   Download

BSD BSD-3-Clause

The Requires

 

The Development Requires

templating template

16/02 2018

3.2.4

3.2.4.0 http://phpixie.com

Templating library for PHPixie

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

templating template

23/01 2017

3.2.3

3.2.3.0 http://phpixie.com

Templating library for PHPixie

  Sources   Download

BSD

The Requires

 

The Development Requires

templating template

13/01 2017

3.2.2

3.2.2.0 http://phpixie.com

Templating library for PHPixie

  Sources   Download

BSD

The Requires

 

The Development Requires

templating template

13/01 2017

3.2.1

3.2.1.0 http://phpixie.com

Templating library for PHPixie

  Sources   Download

BSD

The Requires

 

The Development Requires

templating template

12/01 2017

3.2

3.2.0.0 http://phpixie.com

Templating library for PHPixie

  Sources   Download

BSD

The Requires

 

The Development Requires

templating template

06/01 2017

3.1.1

3.1.1.0 http://phpixie.com

Templating library for PHPixie

  Sources   Download

BSD

The Requires

 

The Development Requires

templating template

06/01 2017

3.1

3.1.0.0 http://phpixie.com

Templating library for PHPixie

  Sources   Download

BSD

The Requires

 

The Development Requires

templating template

11/02 2016

3.0.1

3.0.1.0 http://phpixie.com

Templating library for PHPixie

  Sources   Download

BSD

The Requires

 

The Development Requires

templating template

11/10 2015

3.0

3.0.0.0 http://phpixie.com

Templating library for PHPixie

  Sources   Download

BSD

The Requires

 

The Development Requires

templating template