Guide for combining Volt module engine and phalcon framework, voltphalcon

Source: Internet
Author: User
Tags php template

Guide for combining Volt module engine and phalcon framework, voltphalcon

---- In recent work, web pages use the VOLT template engine compiled by C language, which is faster than loading pages dynamically by js, and more conducive to Baidu data capturing. Now we will sort out the usage ideas based on the document.

(Volt is a super-fast and designer-friendly template language, written in C language PHP. It provides you with a set of comments on Assistant writing a simple method. The phalcon framework is highly integrated with other components, just as you can use it as an independent component in your application.

Volt is an ultra-fast and user-friendly PHP template engine written in C language. It provides you with a set of convenient view assistants. Volt is highly integrated with other components, just as you can use it independently in applications.

)


1. Activation

Register the Volt with the view component and set an extension name or use the default extension. phtml:

<?php//Registering Volt as template engine$di->set('view', function() {    $view = new \Phalcon\Mvc\View();    $view->setViewsDir('../app/views/');    $view->registerEngines(array(        ".volt" => 'Phalcon\Mvc\View\Engine\Volt'    ));    return $view;});

 

<?php$view->registerEngines(array(    ".phtml" => 'Phalcon\Mvc\View\Engine\Volt'));
2. Basic usage

The Volt code is composed of PHP and HTML. Volt has a special set of delimiters, {%... %} is used to execute loop statements and condition judgments ,{{...}} used for value assignment.

You can use Phalcon \ Mvc \ View: setVar to pass the variables in the Controller to the View.

<?phpclass PostsController extends \Phalcon\Mvc\Controller{    public function showAction()    {        $post = Post::findFirst();        $this->view->setVar("title", $post->title);        $this->view->setVar("post", $post);        $this->view->setVar("menu", Menu::find());        $this->view->setVar("show_navigation", true);    }}

 

In the preceding example, three variables are passed to the view: title, menu, and post:

3. Variables

Variables can have attributes and can be accessed through the syntax, such as foo. bar. If they are arrays, you can access them using foo ['bar '].

{{ post.title }}{{ post['title'] }}
4. Filter

Variables can be formatted or modified using filters. The pipeline operator "|" is used to receive filter variables.

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

Add a comment using the separator {#... #}

6. Logical Syntax 1. for Loop
2. if judgment

    

7. Assignment

You can use "set" to set or change the value of a variable.

{% set fruits = ['Apple', 'Banana', 'Orange'] %}{% set name = robot.name %}
8. Mathematical computing

You can use the operator +-* %/

9. Comparison Operators

=! = <>><===! =

10. logical operators

Or and not

11. Other operators

~ |... Is not

{% set robots = ['Voltron', 'Astro Boy', 'Terminator', 'C3PO'] %}{% for index in 0..robots|length %}    {% if isset robots[index] %}        {{ "Name: " ~ robots[index] }}    {% endif %}{% endfor %}
12. View Integration

Volt template integratedPhalcon \ Mvc \ ViewYour template hierarchy is the same as the default hierarchy. You can use partials as follows:

{{ content() }}{{ partial("partials/footer.volt") }}
13. template inheritance

You can create a basic template that inherits from other targets to improve the reusability of template files. The basic template uses a block to define a block, which can be covered by the quilt template.

For example, the following is a basic template.

{# templates/base.volt #}<!DOCTYPE html>

Other templates can inherit from this basic template and replace the block in the Basic Template:

{% extends "templates/base.volt" %}{% block title %}Index{% endblock %}{% block head %}<style type="text/css">.important { color: #336699; }</style>{% endblock %}{% block content %}    

Not all blocks need to be replaced in the subtemplate (meaning that you can replace the desired part ). The final output result of the above example is as follows:

<!DOCTYPE html>

As a piece, the path followed by "extends" is a relative path relative to the view storage directory (that is, app/views ).

/* By default, for performance considerations, Volt only checks whether the Sub-template has been modified. Because we recommend that you use 'compilealway' => true at the beginning of the development phase. In this case, the template always checks whether the parent template has been modified. */

14. Configuration

Volt can change the default behavior through configuration.

  

<?php//Register Volt as a service$di->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;});

If you do not reuse Volt, you can register the Volt template engine as an anonymous function when registering the view service instead of using it as a service.

<?php//Register Volt as template engine with an anonymous function$di->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;});

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.