📜  Phalcon伏特

📅  最后修改于: 2021-01-07 09:22:45             🧑  作者: Mango

VOLT:模板引擎

Volt提供Phalcon快速执行,因为它非常快速,并且使用C for PHP编写的对设计人员友好的模板语言。它定义了许多帮助程序来编写视图。 Volt受Jinja启发,由Armin Ronacher撰写。

实作

Volt视图以php编译,从而节省了手动编写php代码的时间。

{# 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 %}

激活电压

在此,我们在扩展名为.phtml的视图组件中注册Volt。

set(
    'voltService',
    function ($view, $di) {
        $volt = new Volt($view, $di);
        $volt->setOptions(
            [
                'compiledPath'      =>'../app/compiled-templates/',
                'compiledExtension' => '.compiled',
            ]
        );
        return $volt;
    }
);
// Register Volt as template engine
$di->set(
    'view',
    function () {
        $view = new View();
        $view->setViewsDir('../app/views/');
        $view->registerEngines(
            [
'.volt' => 'voltService',
            ]
        );
        return $view;    });

可用电压选项

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
Prefix Allows to prepend a prefix to the templates in the compilation path Null
autoescape Enables globally autoescape of HTML False

变数

对象变量可能具有可以使用以下语法访问的属性: foo.bar 。如果要传递数组,则必须使用方括号语法: foo ['bar']

{{ post.title }} {# for $post->title #}
{{ post['title'] }} {# for $post['title'] #}

筛选器

可以使用过滤器格式化或修改变量。管道运算符(|)用于将过滤器应用于变量:

{{ post.title|e }}
{{ post.content|striptags }}
{{ name|capitalize|trim }}

以下是可以使用的过滤器列表:

Filter Description
abs Applies the abs PHP function to a value.
Capitalize Capitalizes a string by applying the ucwords PHP function to the value
convert_encoding Converts a string from one charset to another
Default Sets a default value in case that the evaluated expression is empty (is not set or evaluates to a falsy value)
escape Applies Phalcon\Escaper->escapeHtml() to the value
escape_attr Applies Phalcon\Escaper->escapeHtmlAttr() to the value
json_encode Converts a value into its JSON representation
json_decode Converts a value from its JSON representation to a PHP representation