Home    Articles


How to use Phalcon template engine ( VOLT ) as standalone


Volt Template Engine from Phalcon seems to be very fast for displaying large amount of data and it is a real sollution for large projects.

Very often we don't want to use the entire framework, in this case we need only the template engine.

In order to use the Phalcon Template Engine (Volt) as standalone into your project you have to use this code, this code is only an example:

The folders structure:

...we will use only the main index.php  and the views/index/index.html to make this demo.

Files content:

1) index.php

ini_set('display_errors', 1);

$testData = array(
					'title'=>'This is a test string',
					'fruits'=> array('apples',
					'content'=>'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'

$view = new Phalcon\Mvc\View();

#dependency injector container is required
$di = new Phalcon\DI\FactoryDefault();	
#set main view directory
#set the file extensions , allowed phtml also
    ".html" => 'Phalcon\Mvc\View\Engine\Volt'
#set vars
$view->post = $testData;
$view->test_the_if = true;
# means:      views/   index /  index.html
echo $view->getRender('index', 'index');

2) views/index/index.html

<!DOCTYPE html>
        <title>{{ post['title'] }} - An example blog</title>

        {% if test_the_if %} 
            <ul id="navigation">
            {% for fruit in post['fruits'] %}
            	{% if fruit is type('string') %}
                	<li><a href="{{ fruit }}">{{ fruit }}</a></li>
                {% endif %}
                {% if fruit is type('array') %}
                  <ul id="navigation">
                  	{% for fruit_color in fruit %}
                		<li><a href="{{ fruit_color }}">{{ fruit_color }}</a></li>
                  	{% endfor %}
                {% endif %}
            {% endfor %}
        {% endif %}

        <h1>{{ post['title'] }}</h1>

        <div class="content">
            {{ post['content'] }}


Note:  - you need to have phalcon extension installed on your server, this is mandatory.

          - for more references please see: Installation and Volt Template Engine