Volt 模版引擎(Volt: Template Engine) ====================================== Volt 是一个用C为PHP编写的超快的并且对设计师友好的模板语言。Volt 提供一组辅助工具有助于你以一种更简单的的方式编写视图(Views)。 同时,Volt与Phalcon的其他组件高度集成在一起,就像你在应用中单独使用Volt一样。 .. 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 视图被编译成纯PHP代码,所以基本上他们节省手工编写PHP代码的工作: .. 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 %} 启用 Volt(Activating Volt) ---------------------------- As with other templating 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. :code:`{% ... %}` is used to execute statements such as for-loops or assign values and :code:`{{ ... }}`, 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