Blog and News

Introduction series for 0.6: Volt: An ultra-fast template engine for Phalcon

image

Following the major refactoring in 0.5.0, we are going to be posting several blog posts introducing the changes in the upcoming version of Phalcon, 0.6.0.

One of the most important component introduced in the 0.6.0 series is Volt, a completely new template engine, written in C, ready to be used in PHP applications.

Volt is enhancing the view layer with a simple and user friendly syntax. Volt is inspired on Jinja, originally created by Armin Ronacher. Therefore many developers will be in familiar ground using the same syntax they have been using with other 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.

The following example shows its basic usage:

{# app/views/products/show.volt #}
{% for product in products %}
  * Name: {{ product.name|e }}
  {% if product.status == "Active" %}
       Price: {{ product.price + product.taxes/100 }}
  {% endif  %}
{% endfor  %}

Volt views are compiled to pure PHP code, so basically they save the effort of writing PHP code manually. For the above example, the following PHP code is generated:

<?php foreach ($products as $product) { ?>
  * Name: <?php echo $this->escaper->escapeHtml($product->name); ?>
  <?php if ($product->status == 'Active') { ?>
       Price: <?php echo $product->price + $product->taxes / 0; ?>
  <?php } ?>
<?php } ?>

As an added value, Volt is integrated with Phalcon\Tag, making the creation of views in Phalcon applications much easier than before:

{{ form('products/save', 'method': 'post') }}

    <label>Name</label>
    {{ text_field("name", "size": 32) }}

    <label>Type</label>
    {{ select("type", productTypes, 'using': ['id', 'name']) }}

</form>

Producing:

<?php echo Phalcon\Tag::form(['products/save', 'method' => 'post']); ?>

    <label>Name</label>
    <?php echo Phalcon\Tag::textField(['name', 'size' => 32]); ?>

    <label>Type</label>
    <?php echo Phalcon\Tag::select(['type', $productTypes, 'using' => array('id', 'name')]); ?>

</form>

A core design feature in Volt is security, therefore Volt offers a limited set of functions that can be used in the templates, while aiding in escaping potentially malicious input by the users.

The first iteration of the Volt templating engine is introduced in 0.6.0, which covers a basic functional basis. As time passes by, more and more features will be added.You can try Volt installing Phalcon from the 0.6.0 branch on Github.

A preliminary documentation is available here

Stay tuned, there's more news on the upcoming version of Phalcon!

<3 The Phalcon Team

01010000011010000110000101101100011000110110111101101110010100000100100001010000