Twig's function Learning _php tutorial

Source: Internet
Author: User
Currently twig built-in functions include

attribute, block, constant, cycle, dump, parent, random, range.


In fact, part of the function, in the study of tags have already seen.


attribute function
1.2 version added
He's the equivalent of a. Operator.
{{Attribute (object, method)}}
{{Attribute (object, method, arguments)}}
{{attribute (array, item)}}
{{Attribute (object, method)}}
{{Attribute (object, method, arguments)}}
{{attribute (array, item)}}

Block function
Outputs the contents of the block block.
{% block title%} {% Endblock%}

{{block (' title ')}}



{% block body%} {% Endblock%}
<title>{% block title%} {% Endblock%}</title>

{{block (' title ')}}

{% block body%} {% Endblock%}

Constant function
Read constant {{Some_date|date (constant (' date_w3c ')}}}
{{constant (' Namespace\\classname::constant_name ')}}
{{Some_date|date (' constant (' date_w3c ')}}}
{{constant (' Namespace\\classname::constant_name ')}}


Cycle function
The contents of the loop output array,
{% Set fruits = [' apple ', ' orange ', ' citrus ']%}

{% for I in 0..10%}
{{cycle (fruits, I)}}
{% ENDFOR%}
{% Set fruits = [' apple ', ' orange ', ' citrus ']%}

{% for I in 0..10%}
{{cycle (fruits, I)}}
{% ENDFOR%}

Dump function
1.5 version added
Print variables, which is the PHP var_dump function,
In addition twig default is not open debug mode, you need to first open his
$twig = new Twig_environment ($loader, $config);
$twig->addextension (New Twig_extension_debug ());
$twig = new Twig_environment ($loader, $config);
$twig->addextension (New Twig_extension_debug ());

You can pass one or more variables, and if you don't pass the variable, he'll print all the variables
{{Dump (user, categories)}}
{{dump ()}}
{{Dump (user, categories)}}
{{dump ()}}

Parent function
Getting the contents of a parent block is especially useful when you're ready to add rather than overwrite
{% extends "base.html"%}

{% block sidebar%}

Table of Contents


...
{{parent ()}}
{% Endblock%}
{% extends "base.html"%}

{% block sidebar%}

Table of Contents


...
{{parent ()}}
{% Endblock%}

Random function
Version 1.5 was added, and a random return from an array
{{[' Apple ', ' orange ', ' Citrus ']}}
{{[' Apple ', ' orange ', ' Citrus ']}}

Range function
Returns an array of numbers, starting with the first argument, ending with the second argument (containing the second one), and the third argument being the step (which can be omitted). and 0.. 101 Samples
{% for I in range (0, 3)%}
{{i}},
{% ENDFOR%}

{# returns 0, 1, 2, 3 #}
{% for I in range (0, 3)%}
{{i}},
{% ENDFOR%}

{# returns 0, 1, 2, 3 #}


and learned a lot. Continue to applause applause. Towards the new page ...

Excerpt from Jiaochangyun's column

http://www.bkjia.com/PHPjc/478457.html www.bkjia.com true http://www.bkjia.com/PHPjc/478457.html techarticle currently twig built-in functions include attribute, block, constant, cycle, dump, parent, random, range. In fact, part of the function, in the study of tags have already seen. Attribute function 1.2 Version new ...

  • 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.