VOLT: Template Engine

Volt provides Phalcon fast execution as it is very fast and designer friendly templating language written in C for PHP. It has many helpers defined to write views. Volt is inspired by Jinja and written by Armin Ronacher.

Implementation

Volt views are compiled in php which saves time to write php code manually.

{# app/views/products/show.volt #}  

{% block last_products %}  

{% for product in products %}  

    * Name: {{ product.name|e }}  

    {% if product.status === 'Active' %}  

       Price: {{ product.price + product.taxes/100 }}  

    {% endif  %}  

{% endfor  %}  

{% endblock %}

Activating Volt

In this we register Volt in view component having extension .phtml.

<?php  

use Phalcon\Mvc\View;  

use Phalcon\Mvc\View\Engine\Volt;  

// Register Volt as a service  

$di->set(  

    'voltService',  

    function ($view, $di) {  

        $volt = new Volt($view, $di);  

        $volt->setOptions(  

            [  

                'compiledPath'      =>'../app/compiled-templates/',  

                'compiledExtension' => '.compiled',  

            ]  

        );  

        return $volt;  

    }  

);  

// Register Volt as template engine  

$di->set(  

    'view',  

    function () {  

        $view = new View();  

        $view->setViewsDir('../app/views/');  

        $view->registerEngines(  

            [  

'.volt' => 'voltService',  

            ]  

        );  

        return $view;    });

Options available in volt

OptionDescriptionDefault
compiledPathA writable path where the compiled PHP templates will be placed./
compiledExtensionAn additional extension appended to the compiled PHP file.php
compiledSeparatorVolt replaces the directory separators / and \ by this separator in order to create a single file in the compiled directory%%
StatWhether Phalcon must check if exists differences between the template file and its compiled pathTrue
compileAlwaysTell Volt if the templates must be compiled in each request or only when they changeFalse
PrefixAllows to prepend a prefix to the templates in the compilation pathNull
autoescapeEnables globally autoescape of HTMLFalse

Variables

Object variables may have attributes which can be accessed using the syntax: foo.bar. If you are passing arrays, you have to use the square bracket syntax: foo[‘bar’]

  {{ post.title }} {# for $post->title #}  

{{ post['title'] }} {# for $post['title'] #}  

    Filters

    Variables can be formatted or modified using filters. The pipe operator (|) is used to apply filters to variables:

    {{ post.title|e }}  
    
    {{ post.content|striptags }}  
    
    {{ name|capitalize|trim }}

    Following is the list of filters that can be used:

    FilterDescription
    absApplies the abs PHP function to a value.
    CapitalizeCapitalizes a string by applying the ucwords PHP function to the value
    convert_encodingConverts a string from one charset to another
    DefaultSets a default value in case that the evaluated expression is empty (is not set or evaluates to a falsy value)
    escapeApplies Phalcon\Escaper->escapeHtml() to the value
    escape_attrApplies Phalcon\Escaper->escapeHtmlAttr() to the value
    json_encodeConverts a value into its JSON representation
    json_decodeConverts a value from its JSON representation to a PHP representation

    Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *