TWIG
About Me


           Wake Liu


wake.gs@gmail.com
Before the session starts
Do I need a template
      engine?
PHP is the most
powerful template
     engine
So, we really need a
 template engine ?
TWIG
Performance
• Compile & cache
• PHP Extension - Twig_Template::getAttribute()
• APC & Memcached
How it feels to use Twig
Clean and Beautiful
Variables
•   <?php echo $name; ?>   •   {{ name }}

•   <?php echo             •   {{ member.nickname }}
    $member->nickname;
    ?>                     •   {{ member[‘nickname’] }}

•   <?php                  •   {{
    echo                       member
    $member[‘nickname’];         ? member.nickname
    ?>                           : ‘Guest’
                               }}
Filters / Functions
• <?php echo nl2br ($content); ?>
• <?php echo html_entity_decode ($content); ?>
• <?php echo rand(0, 10); ?>
•   {{ content|nl2br }}

•   {{ content|raw }}     {{ content|nl2br|length }}


•   {{ random(10) }}
Logic & Loop

•   {% if %} {% elseif %} {% else %} {% endif %}



•   {% for key, value in data %}   {% endfor %}



•   {% set name = ‘wake’ %}
Organizational and
    reusable
Extends
layout.html
 <!DOCTYPE html>
                                        guest.html
<html lang="en">
<head>
 <meta charset="utf-8">                 {% extends layout.html %}
 <title>The HTML5 Herald</title>
</head>
<body>


   {% block wrapper %}
    default content
                                        member.html
   {% endblock %}                       {% extends layout.html %}
</body>
</html>
Horizontal reuse
                        page.html
navigator.html
                         {% use navigator.html %}
                         {% use login.html %}
{% block navigator %}   <div class=”wrapper”>
                         <div class=”navigator”>

                           {{ block (‘navigator’) }}
login.html               </div>

                         <div class=”login”>

                           {{ block (‘login’) }}
{% block login %}        </div>
                        </div>
Expandable
Filter, Function, Macro
       and others
The role of Twig
Template engine
View helper
MVC
Controller
                        Control ( Logic / Loop )

                    {
   C
             View       Data handling ( Filters / Functions )
              V         View binding ( Structures / Macros )
  Model

   M
Hmm, it looks not bad,
        but ...
......

 VS
Some more
interesting things
Photo URL passing
                             photo.html
original data                <img src=”


http://www/:size/photo.jpg    {{ url | image_to_large }}
                              ”>




php define                    Twig - addFilter
_LARGE_PHOTO ‘L’
_SMALL_PHOTO ‘S’              image_to_*
Template driven
member.php
                                  A.html
class Member {
                                  <div>


    function __get ($name) {       {{ member.age }}
      $loader = “_load$name”;     </div>


      return $this->$loader ();
    }                             B.html
    function _loadAge () {        <div>


      // load from database        {{ member.age }}
    }                             </div>


}
Thank you

 Q &A

PHPConf-TW 2012 # Twig

  • 1.
  • 2.
  • 3.
  • 4.
    Do I needa template engine?
  • 5.
    PHP is themost powerful template engine
  • 7.
    So, we reallyneed a template engine ?
  • 8.
  • 9.
    Performance • Compile &cache • PHP Extension - Twig_Template::getAttribute() • APC & Memcached
  • 10.
    How it feelsto use Twig
  • 11.
  • 12.
    Variables • <?php echo $name; ?> • {{ name }} • <?php echo • {{ member.nickname }} $member->nickname; ?> • {{ member[‘nickname’] }} • <?php • {{ echo member $member[‘nickname’]; ? member.nickname ?> : ‘Guest’ }}
  • 13.
    Filters / Functions •<?php echo nl2br ($content); ?> • <?php echo html_entity_decode ($content); ?> • <?php echo rand(0, 10); ?> • {{ content|nl2br }} • {{ content|raw }} {{ content|nl2br|length }} • {{ random(10) }}
  • 14.
    Logic & Loop • {% if %} {% elseif %} {% else %} {% endif %} • {% for key, value in data %} {% endfor %} • {% set name = ‘wake’ %}
  • 15.
  • 16.
    Extends layout.html <!DOCTYPE html> guest.html <html lang="en"> <head> <meta charset="utf-8"> {% extends layout.html %} <title>The HTML5 Herald</title> </head> <body> {% block wrapper %} default content member.html {% endblock %} {% extends layout.html %} </body> </html>
  • 17.
    Horizontal reuse page.html navigator.html {% use navigator.html %} {% use login.html %} {% block navigator %} <div class=”wrapper”> <div class=”navigator”> {{ block (‘navigator’) }} login.html </div> <div class=”login”> {{ block (‘login’) }} {% block login %} </div> </div>
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
    MVC Controller Control ( Logic / Loop ) { C View Data handling ( Filters / Functions ) V View binding ( Structures / Macros ) Model M
  • 24.
    Hmm, it looksnot bad, but ...
  • 25.
  • 26.
  • 27.
    Photo URL passing photo.html original data <img src=” http://www/:size/photo.jpg {{ url | image_to_large }} ”> php define Twig - addFilter _LARGE_PHOTO ‘L’ _SMALL_PHOTO ‘S’ image_to_*
  • 28.
    Template driven member.php A.html class Member { <div> function __get ($name) { {{ member.age }} $loader = “_load$name”; </div> return $this->$loader (); } B.html function _loadAge () { <div> // load from database {{ member.age }} } </div> }
  • 29.