2017 © Pedro Peláez
 

library template-engine

Webiny Template Engine Component

image

webiny/template-engine

Webiny Template Engine Component

  • Friday, September 29, 2017
  • by SvenAlHamad
  • Repository
  • 4 Watchers
  • 1 Stars
  • 17 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 23 Versions
  • 0 % Grown

The README.md

Template Engine Component

TemplateEngine component provides a layer for rendering view templates., (*1)

Install the component

The best way to install the component is using Composer., (*2)

composer require webiny/template-engine

For additional versions of the package, visit the Packagist page., (*3)

Installation error

If you get an error like this:, (*4)

[RuntimeException]
Package could not be downloaded, sh: 1: svn: not found

Then probably you don't have Subversion installed which is preventing the installation of Smarty library. This can be easily solved by installing the Subversion, prior to the composer install., (*5)

$ sudo apt-get install subversion

Usage

The definition of the view template depends on the selected driver. By default the template engine comes with a driver for Smarty, but you can easily add support for Twig or some other template engines., (*6)

The provided functionality of every driver is defined by the TemplateEngineInterface which defines the following methods: - fetch - fetch the template from the given location, parse it and return the output - render - fetch the template from the given location, parse it and output the result to the browser - assign - assign a variable and its value into the template engine - setTemplateDir - directory where the template files are stored - registerPlugin - register a plugin for the template engine, (*7)

To create a new driver just create a new class that implements the \Webiny\Component\TemplateEngine\Bridge\TemplateEngineInterface and adapt your config., (*8)

The default configuration looks like this:, (*9)

    TemplateEngine:
        Engines:
            Smarty:
                ForceCompile: false
                CacheDir: '/var/tmp/smarty/cache'
                CompileDir: '/var/tmp/smarty/compile'
                TemplateDir: '/var/www/theme/templates'
                AutoEscapeOutput: false
                MuteExpectedErrors: true

Usage

The preferred usage is over the TemplateEngineTrait. Here is an example:, (*10)

class MyClass
{
    use \Webiny\Component\TemplateEngine\TemplateEngineTrait;

    function __construct() {
        // assing name and id to the template and render it
        $this->templateEngine('Smarty')->render('template.tpl', ['name'=>'John', 'id'=>15]);
    }
}

Smarty

If you wish to use the Smarty template engine, with the built in driver, make sure you include path/to/Smarty/libs/Smarty.class.php somewhere inside you application, before using the component. This is due to the problem that Smarty doesn't provide a suitable autoloader to be integrated with the component., (*11)

Plugins & extensions

The template engine is designed so that it can be expanded with different plugins and modifiers, depending on the assigned driver., (*12)

Best practice for expanding the template engine is first to create an extension and then register it as a service tagged with the $driverName.Extension, for example Smarty.Extension., (*13)

An Extension is a package of one or multiple plugins. Plugin type depends on the template engine, for example, Smarty supports these plugin types: - functions - http://www.smarty.net/docs/en/plugins.functions.tpl - modifiers - http://www.smarty.net/docs/en/plugins.modifiers.tpl - blocks - http://www.smarty.net/docs/en/plugins.block.functions.tpl - compiler functions - http://www.smarty.net/docs/en/plugins.compiler.functions.tpl - pre filters - http://www.smarty.net/docs/en/plugins.prefilters.postfilters.tpl - post filters - http://www.smarty.net/docs/en/plugins.prefilters.postfilters.tpl - output filters - http://www.smarty.net/docs/en/plugins.outputfilters.tpl - resources - http://www.smarty.net/docs/en/plugins.resources.tpl - inserts - http://www.smarty.net/docs/en/plugins.inserts.tpl, (*14)

To create a smarty extension, create a class that extends \Webiny\Component\TemplateEngine\Drivers\Smarty\AbstractSmartyExtension and then overwrite the methods, based on the plugin type your wish to create., (*15)

For example, let's say we want to register a modifier called 'customUpper'. First we create our extension class like this:, (*16)

namespace MyApp\Demo;

class MySmartyExtension extends \Webiny\Component\TemplateEngine\Drivers\Smarty\SmartyExtension
{
    /**
     * @overwrite
     * @return array
     */
    function getModifiers(){
        return [
            new SmartySimplePlugin('custom_upper', 'modifier', [$this, 'customUpper'])
        ];
    }

    /**
     * Callback for my custom_upper modifier.
     *
     * @param $params
     *
     * @return string
     */
    function customUpper($params){
        return strtoupper($params);
    }

    /**
     * Returns the name of the plugin.
     *
     * @return string
     */
    function getName() {
        return 'my_extension';
    }
}

Once we have our extension, we must register it using the service manager:, (*17)

MyApp:
    CustomExtension:
        Class: \MyApp\Demo\MySmartyExtension
        Tags: [Smarty.Extension]

And that's it, we can now use the modifier in our templates:, (*18)

{'this is my name'|custom_upper}
// outputs: THIS IS MY NAME

Resources

To run unit tests, you need to use the following command:, (*19)

$ cd path/to/Webiny/Component/TemplateEngine/
$ composer.phar install
$ phpunit

The Versions

29/09 2017

dev-master

9999999-dev http://www.webiny.com/

Webiny Template Engine Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

smarty template engine views

29/09 2017

1.6.x-dev

1.6.9999999.9999999-dev http://www.webiny.com/

Webiny Template Engine Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

smarty template engine views

29/09 2017

v1.6.0

1.6.0.0 http://www.webiny.com/

Webiny Template Engine Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

smarty template engine views

29/09 2017

v1.6.1

1.6.1.0 http://www.webiny.com/

Webiny Template Engine Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

smarty template engine views

08/09 2017

1.5.x-dev

1.5.9999999.9999999-dev http://www.webiny.com/

Webiny Template Engine Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

smarty template engine views

08/09 2017

v1.5.0

1.5.0.0 http://www.webiny.com/

Webiny Template Engine Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

smarty template engine views

21/08 2016

1.4.x-dev

1.4.9999999.9999999-dev http://www.webiny.com/

Webiny Template Engine Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

smarty template engine views

21/08 2016

v1.4.3

1.4.3.0 http://www.webiny.com/

Webiny Template Engine Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

smarty template engine views

04/02 2016

v1.4.0

1.4.0.0 http://www.webiny.com/

Webiny Template Engine Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

smarty template engine views

04/02 2016

v1.4.1

1.4.1.0 http://www.webiny.com/

Webiny Template Engine Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

smarty template engine views

04/02 2016

v1.4.2

1.4.2.0 http://www.webiny.com/

Webiny Template Engine Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

smarty template engine views

16/03 2015

1.3.x-dev

1.3.9999999.9999999-dev http://www.webiny.com/

Webiny Template Engine Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

smarty template engine views

16/03 2015

v1.3.0

1.3.0.0 http://www.webiny.com/

Webiny Template Engine Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

smarty template engine views

05/02 2015

v1.2.2

1.2.2.0 http://www.webiny.com/

Webiny Template Engine Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

smarty template engine views

05/02 2015

v1.2.3

1.2.3.0 http://www.webiny.com/

Webiny Template Engine Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

smarty template engine views

05/02 2015

v1.2.1

1.2.1.0 http://www.webiny.com/

Webiny Template Engine Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

smarty template engine views

04/02 2015

1.2.x-dev

1.2.9999999.9999999-dev http://www.webiny.com/

Webiny Template Engine Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

smarty template engine views

04/02 2015

v1.2.0

1.2.0.0 http://www.webiny.com/

Webiny Template Engine Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

smarty template engine views

28/11 2014

v1.1.1

1.1.1.0 http://www.webiny.com/

Webiny Template Engine Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

twig smarty template engine views

23/11 2014

1.1.x-dev

1.1.9999999.9999999-dev http://www.webiny.com/

Webiny Template Engine Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

twig smarty template engine views

23/11 2014

v1.1.0

1.1.0.0 http://www.webiny.com/

Webiny Template Engine Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

twig smarty template engine views

20/09 2014

1.0.x-dev

1.0.9999999.9999999-dev http://www.webiny.com/

Webiny Template Engine Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

twig smarty template engine views

20/09 2014

v1.0.0

1.0.0.0 http://www.webiny.com/

Webiny Template Engine Component

  Sources   Download

MIT

The Requires

 

The Development Requires

by Sven Al Hamad
by Pavel Denisjuk

twig smarty template engine views