Volt: Template Engine ===================== Volt is an ultra-fast and designer friendly templating language written in C for PHP. It provides you a set of helpers to write views in an easy way. Volt is highly integrated with other components of Phalcon, just as you can use it as a stand-alone component in your applications. .. figure:: ../_static/img/volt.jpg :align: center Volt is inspired by Jinja_, originally created by `Armin Ronacher`_. Therefore many developers will be in familiar territory using the same syntax they have been using with similar template engines. Volt’s syntax and features have been enhanced with more elements and of course with the performance that developers have been accustomed to while working with Phalcon. Introduction ------------ Volt views are compiled to pure PHP code, so basically they save the effort of writing PHP code manually: .. code-block:: html+jinja {# 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 --------------- As other template engines, you may register Volt in the view component, using a new extension or reusing the standard .phtml: .. code-block:: php set('view', function() { $view = new View(); $view->setViewsDir('../app/views/'); $view->registerEngines(array( ".volt" => 'Phalcon\Mvc\View\Engine\Volt' )); return $view; }); Use the standard ".phtml" extension: .. code-block:: php registerEngines(array( ".phtml" => 'Phalcon\Mvc\View\Engine\Volt' )); Basic Usage ----------- A view consists of Volt code, PHP and HTML. A set of special delimiters is available to enter into Volt mode. {% ... %} is used to execute statements such as for-loops or assign values and {{ ... }}, prints the result of an expression to the template. Below is a minimal template that illustrates a few basics: .. code-block:: html+jinja {# app/views/posts/show.phtml #}
# | Id | Name |
---|---|---|
{{ loop.index }} | {{ robot.id }} | {{ robot.name }} |
' ~ my_input('name', 'input-text') ~ '
' }} And receive optional parameters: .. code-block:: html+jinja {%- macro my_input(name, class="input-text") %} {% return text_field(name, 'class': class) %} {%- endmacro %} {# Call the macro #} {{ '' ~ my_input('name') ~ '
' }} {{ '' ~ my_input('name', 'input-text') ~ '
' }} Using Tag Helpers ----------------- Volt is highly integrated with :doc:`Phalcon\\TagWelcome on my awesome homepage.
{% endblock %} Not all blocks must be replaced at a child template, only those that are needed. The final output produced will be the following: .. code-block:: htmlWelcome on my awesome homepage.
{{ post.content }}
{% endcache %} The caching is done by the :doc:`Phalcon\\Cache