TWIG
About Me           Wake Liuwake.gs@gmail.com
Before the session starts
Do I need a template      engine?
PHP is the mostpowerful 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->nicknam...
Filters / Functions• <?php echo nl2br ($content); ?>• <?php echo html_entity_decode ($content); ?>• <?php echo rand(0, 10)...
Logic & Loop•   {% if %} {% elseif %} {% else %} {% endif %}•   {% for key, value in data %}   {% endfor %}•   {% set name...
Organizational and    reusable
Extendslayout.html <!DOCTYPE html>                                        guest.html<html lang="en"><head> <meta charset="...
Horizontal reuse                        page.htmlnavigator.html                         {% use navigator.html %}          ...
Expandable
Filter, Function, Macro       and others
The role of Twig
Template engine
View helper
MVCController                        Control ( Logic / Loop )                    {   C             View       Data handlin...
Hmm, it looks not bad,        but ...
...... VS
Some moreinteresting things
Photo URL passing                             photo.htmloriginal data                <img src=”http://www/:size/photo.jpg ...
Template drivenmember.php                                  A.htmlclass Member {                                  <div>    ...
Thank you Q &A
PHPConf-TW 2012 # Twig
Upcoming SlideShare
Loading in …5
×

PHPConf-TW 2012 # Twig

2,657 views

Published on

Twig - A simple, fast, modern template engine for PHP

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,657
On SlideShare
0
From Embeds
0
Number of Embeds
122
Actions
Shares
0
Downloads
56
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • PHPConf-TW 2012 # Twig

    1. 1. TWIG
    2. 2. About Me Wake Liuwake.gs@gmail.com
    3. 3. Before the session starts
    4. 4. Do I need a template engine?
    5. 5. PHP is the mostpowerful template engine
    6. 6. So, we really need a template engine ?
    7. 7. TWIG
    8. 8. Performance• Compile & cache• PHP Extension - Twig_Template::getAttribute()• APC & Memcached
    9. 9. How it feels to use Twig
    10. 10. Clean and Beautiful
    11. 11. Variables• <?php echo $name; ?> • {{ name }}• <?php echo • {{ member.nickname }} $member->nickname; ?> • {{ member[‘nickname’] }}• <?php • {{ echo member $member[‘nickname’]; ? member.nickname ?> : ‘Guest’ }}
    12. 12. 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) }}
    13. 13. Logic & Loop• {% if %} {% elseif %} {% else %} {% endif %}• {% for key, value in data %} {% endfor %}• {% set name = ‘wake’ %}
    14. 14. Organizational and reusable
    15. 15. Extendslayout.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>
    16. 16. Horizontal reuse page.htmlnavigator.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>
    17. 17. Expandable
    18. 18. Filter, Function, Macro and others
    19. 19. The role of Twig
    20. 20. Template engine
    21. 21. View helper
    22. 22. MVCController Control ( Logic / Loop ) { C View Data handling ( Filters / Functions ) V View binding ( Structures / Macros ) Model M
    23. 23. Hmm, it looks not bad, but ...
    24. 24. ...... VS
    25. 25. Some moreinteresting things
    26. 26. Photo URL passing photo.htmloriginal data <img src=”http://www/:size/photo.jpg {{ url | image_to_large }} ”>php define Twig - addFilter_LARGE_PHOTO ‘L’_SMALL_PHOTO ‘S’ image_to_*
    27. 27. Template drivenmember.php A.htmlclass 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>}
    28. 28. Thank you Q &A

    ×