forked from bramus/ws1-sws-course-materials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
08.templates.summary.html
251 lines (203 loc) · 10.1 KB
/
08.templates.summary.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<!doctype html>
<html>
<head>
<title>PHP: Templates using Twig</title>
<style>
.main div { padding-left: 1em; margin-left: 1em; border-left: 0.125em solid #666; }
.main pre { border: 0.125em dotted #333; padding: 1em; width: 80%; overflow: scroll; }
a {color: #0066FF;}
.main a, a.higher { text-decoration: none; font-size: 70%; vertical-align: top; padding: 1em; margin: -1em; }
a.normal { font-size: 100%; vertical-align: inherit; padding: 0; margin: 0;}
.info, .warning { font-style: italic; }
.info { opacity: 0.75; }
.warning-sign { font-size: 2em; font-style: normal;}
</style>
</head>
<body>
<h1>PHP: Templates using Twig<a href="http://twig.sensiolabs.org" class="higher">⚑</a></h1>
<section class="info">
<p>This page forms the code summary of <a href="08.templates.html">08.templates.html</a>, part of the <strong>Webscripting1 — Serverside Webscripting</strong> course, part of the <a href="http://www.ikdoeict.be/">Professional Bachelor ICT</a> study programme, taught at Odisee, Ghent, Belgium. The materials and this summary were developed by Bram(us) Van Damme, lecturer ICT at Odisee, who blogs over at <a href="http://www.bram.us/">bram.us</a> and Twitters as <a href="http://www.twitter.com/bramus">@bramus</a>. The materials and this summary may be used freely, as long as credit to Bramus is present and a clear an upfront link to <a href="http://www.ikdoeict.be/">ikdoeict.be</a> remains in place. Suggestions and additions may be mailed to Bramus, or sent via <a href="https://github.com/bramus/ws1-sws-course-materials">a pull request on GitHub</a>.</p>
</section>
<section class="main">
<h2>Structure on disk</h2>
<ul>
<li>Place Twig in the <code>includes/</code> folder.</li>
<li>Store all templates in <code>templates/</code> and give them a <code>.twig</code> extension.</li>
<li>Create a <code>cache/</code> folder where templates are cached in.</li>
</ul>
<h2>Loading and displaying templates</h2>
<p>PHP file</p>
<div><pre class="source"><code><?php
// includes & requires
require_once __DIR__ . '/includes/Twig/Autoloader.php';
Twig_Autoloader::register();
// Twig Bootstrap
$loader = new Twig_Loader_Filesystem(__DIR__ . '/templates');
$twig = new Twig_Environment($loader, array(
'cache' => __DIR__ . '/cache',
'auto_reload' => true // set to false on production
));
// Data (fetched from DB for example)
$user = array(
'id' => 1,
'username' => 'bramus',
'firstname' => 'Bramus',
'lastname' => 'Van Damme',
'email' => '[email protected]',
'password' => '$1$oGlMhJl6$EbgQIf8kQSr6MxA3YYYpm0'
);
// load template
$tpl = $twig->loadTemplate('template.twig');
// render template with our data
echo $tpl->render(array(
'user' => $user
));
// EOF</code></pre></div>
<p>Source <code>template.twig</code></p>
<div><pre class="source"><code><p>Hi {{ user.firstname }} {{ user.lastname }}!</p></code></pre></div>
<h2>Variable Filters</h2>
<p>Variables can be filtered before display. Multiple filters can be added.</p>
<div><pre class="source"><code><p>The value of <em>title</em> is {{ title }}</p>
<p>{{ tagline|replace({'like':'love', 'Twig':'you'}) }}</p>
<p>Today: {{ curdate|date("d/m/Y") }}</p>
<p>{{ name|upper|reverse }}</p></code></pre></div>
<p>Full list of filters at <a href="http://twig.sensiolabs.org/doc/filters/index.html" class="normal">http://twig.sensiolabs.org/doc/filters/index.html</a></p>
<h2>Working with arrays</h2>
<h3>Looping Arrays</h3>
<p>PHP array</p>
<div><pre class="source"><code>$colleagues = array(
array(
'name' => 'Rogier van der Linde',
'city' => 'Ghent',
'courses' => array('Webtechnology', 'Webdesign & Usability', 'Webscripting 1', 'Webprogramming')
), array(
'name' => 'Kevin Picalausa',
'city' => 'Ghent',
'courses' => array('Webscripting 2', 'Webprogramming')
), array(
'name' => 'Davy De Winne',
'city' => 'Schellebelle',
'courses' => array('Webtechnology', 'Webdesign & Usability', 'Webscripting 2')
), array(
'name' => 'Joske Vermeulen'
)
);</code></pre></div>
<p>Template code</p>
<div><pre class="source"><code><h2>My web colleagues</h2>
<div>
{% for colleague in colleagues %}
<h3>
{{ colleague.name }}
{% if colleague.city %}<em>({{ colleague.city }})</em>{% endif %}
</h3>
<div>
{% if colleague.courses %}
<p>You might know him from:</p>
<ul>
{% for course in colleague.courses %}
<li>{{ course }}</li>
{% endfor %}
</ul>
{% else %}
<p>(He's not teaching any web courses)</p>
{% endif %}
</div>
{% endfor %}
</div></code></pre></div>
<h3>Looping Arrays and getting the Keys / Keys & Values</h3>
<p>Getting keys<a href="http://twig.sensiolabs.org/doc/tags/for.html#iterating-over-keys">⚑</a> of an array</p>
<div><pre class="source"><code><ul>
{% for k in courses|keys %}
<li>key: {{ k }}</li>
{% else %}
<li>(no items in the array)</li>
{% endfor %}
</ul></code></pre></div>
<p>Getting keys & Values<a href="http://twig.sensiolabs.org/doc/tags/for.html#iterating-over-keys-and-values">⚑</a> of an array</p>
<div><pre class="source"><code><ul>
{% for key, val in courses %}
<li>{{ key }} = {{ val }}</li>
{% else %}
<li>(no items in the array)</li>
{% endfor %}
</ul></code></pre></div>
<h3>The <code>loop</code> variable<a href="http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable">⚑</a></h3>
<div><pre class="source"><code><ul>
{% for val in courses %}
<li>
{{ val }}
<ul>
<li><code>loop.index</code>: {{ loop.index }}</li>
<li><code>loop.index0</code>: {{ loop.index0 }}</li>
<li><code>loop.revindex</code>: {{ loop.revindex }}</li>
<li><code>loop.revindex0</code>: {{ loop.revindex0 }}</li>
<li><code>loop.first</code>: {{ loop.first }}</li>
<li><code>loop.last</code>: {{ loop.last }}</li>
<li><code>loop.length</code>: {{ loop.length }}</li>
</ul>
</li>
{% endfor %}
</ul></code></pre></div>
<h3>Generating your own sequences</h3>
<div><pre class="source"><code><p>{% for i in 0..10 %}{{ i }}{% endfor %}</p>
<p>{% for letter in 'a'..'z' %}{{ letter }}{% endfor %}</p></code></pre></div>
<h2>More on if</h2>
<p>If can also do comparisons and various checks</p>
<div><pre class="source"><code>{% if username == 'bramus' %} YOLO {% endif %}</code></pre></div>
<div><pre class="source"><code>{% if user in ['bramus', 'rogier'] %} YOLO {% endif %}</code></pre></div>
<div><pre class="source"><code>{% if blogpost.visibility == 'password' %}
<p>Blogpost is password protected</p>
{% elseif blogpost.visibility == 'link' %}
<p>Blogpost is public for those who have the link</p>
{% else %}
<p>Blogpost is public for all</p>
{% endif %}</code></pre></div>
<h2>Template Inheritance</h2>
<p>Use <code>block</code><a href="http://twig.sensiolabs.org/doc/tags/block.html">⚑</a> to define a few blocks in a parent template and use <code>extends</code> in the child template to extend from it.</p>
<p>In the child template, overwrite blocks from the parent template, using the same <code>block</code><a href="http://twig.sensiolabs.org/doc/tags/block.html">⚑</a>. Use <code>parent()</code><a href="http://twig.sensiolabs.org/doc/functions/parent.html">⚑</a> to render the code of the parent block. Use <code>set</code><a href="http://twig.sensiolabs.org/doc/tags/set.html">⚑</a> to set variables in the parent template.</p>
<h3>Master/Parent template</h3>
<div><pre class="source"><code><!doctype html>
<html>
<head>
<title>{{ pageTitle }}</title>
<style>
{% block pageStyle %}
div { padding-left: 1em; margin-left: 1em; border-left: 0.125em solid #666;}
{% endblock %}
</style>
</head>
<body>
<h1>{{ pageTitle }}</h1>
<div>
{% block pageContent %}
{% endblock %}
</div>
{% block pageFooter %}
<footer>This is the footer</footer>
{% endblock %}
</body>
</html></code></pre></div>
<h3>Child template</h3>
<div><pre class="source"><code>{% extends 'layout.twig' %}
{% set pageTitle = pageTitle|replace({'e': 'a'}) ~ ' (Manipulated)' %}
{% block pageStyle %}
{{ parent() }}
footer { text-align: center; padding-top: 2em; }
{% endblock %}
{% block pageContent %}
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc quis neque libero, non rutrum nisi. Donec venenatis, nibh in aliquet consectetur, leo tellus elementum libero, at pretium ante enim nec tellus. Sed quis tempor erat.</p>
<p>Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Etiam a urna sit amet augue iaculis fringilla. Nulla semper nulla mauris, at pellentesque mauris. Aenean viverra dictum ullamcorper. Nam eros dui, pellentesque et ultrices vitae, adipiscing feugiat sapien. Pellentesque tincidunt eros eu sem convallis fermentum. Donec dui ante, scelerisque in sollicitudin in, consectetur vel diam.</p>
{% endblock %}</code></pre></div>
<h3>Load the child template in your PHP:</h3>
<div><pre class="source"><code>// load template
$tpl = $twig->loadTemplate('child.twig');
// render template with our data
echo $tpl->render(array(
'pageTitle' => 'Template Inheritance'
));</code></pre></div>
<h3>Inclusion of another template also possible</h3>
<p>Suggested to place these partial templates into <code>templates/partials/</code></p>
<div><pre class="source"><code>{% include 'partials/sidebar.twig' %}</code></pre></div>
</section>
</body>
</html>