More Related Content
Similar to PHPConf-TW 2012 # Twig (20)
PHPConf-TW 2012 # Twig
- 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’ %}
- 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>
- 23. MVC
Controller
Control ( Logic / Loop )
{
C
View Data handling ( Filters / Functions )
V View binding ( Structures / Macros )
Model
M
- 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>
}
Editor's Notes
- \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