Welcome on my awesome homepage.
{% endblock %}
并非所有的 block 都需要在子模板中进行替换(意思是,你可以替换需要的那一部分)。上面的示例最终输出结果如下:
.. code-block:: html
Index
Welcome on my awesome homepage.
作为一个片断,"extends" 后跟的路径是一个相对于视图存放目录的相对路径(即 app/views).
.. highlights::
默认情况下,出于对性能的考虑,Volt只检查子模板是否有修改。因为建议在开发阶段初始时使用 'compileAlways' => true。这样的话,模板始终检查父模板是否有修改。
Setting up the Volt Engine
--------------------------
Volt是可以通过配置改变默认的行为的,下面的例子说明如何做到这一点:
.. code-block:: php
set('voltService', function($view, $di) {
$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
$volt->setOptions(array(
"compiledPath" => "../app/compiled-templates/",
"compiledExtension" => ".compiled"
));
return $volt;
});
//Register Volt as template engine
$di->set('view', function() {
$view = new \Phalcon\Mvc\View();
$view->setViewsDir('../app/views/');
$view->registerEngines(array(
".volt" => 'voltService'
));
return $view;
});
如果你不重用 Volt,你可以不把它作为一个服务,而是在注册 view 服务的时候,用匿名函数的方式注册Volt模板引擎:
.. code-block:: php
set('view', function() {
$view = new \Phalcon\Mvc\View();
$view->setViewsDir('../app/views/');
$view->registerEngines(array(
".volt" => function($view, $di) {
$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
//set some options here
return $volt;
}
));
return $view;
});
Volt 配置选项:
+-------------------+--------------------------------------------------------------------------------------------------------------------------------+---------+
| Option | Description | Default |
+===================+================================================================================================================================+=========+
| compiledPath | A writable path where the compiled PHP templates will be placed | ./ |
+-------------------+--------------------------------------------------------------------------------------------------------------------------------+---------+
| compiledExtension | An additional extension appended to the compiled PHP file | .php |
+-------------------+--------------------------------------------------------------------------------------------------------------------------------+---------+
| compiledSeparator | Volt replaces the directory separators / and \\ by this separator in order to create a single file in the compiled directory | %% |
+-------------------+--------------------------------------------------------------------------------------------------------------------------------+---------+
| stat | Whether Phalcon must check if exists differences between the template file and its compiled path | true |
+-------------------+--------------------------------------------------------------------------------------------------------------------------------+---------+
| compileAlways | Tell Volt if the templates must be compiled in each request or only when they change | false |
+-------------------+--------------------------------------------------------------------------------------------------------------------------------+---------+
相关资源
------------------
* A bundle for Sublime/Textmate is available `here