with Margaret Staples
Linux + Apache + MySQL + PHP
*/NewProject
*/NewProject/src
*/NewProject/views
*/NewProject/index.php
*/path/to/web/directory
Dependency Manager for PHP
*/NewProject/composer.json
{
"autoload": {
"psr-4": {
"NewProject\\Classes\\": "src/"
}
},
"require": {
"twig/twig": "~1.0"
}
}
> php composer.phar install
> php composer.phar init
Search for a package: Twig
Enter package # to add ...: 0
...
"autoload": {
"psr-4": {
"NewProject\\Classes\\": "src/"
}
}
...
*/NewProject/index.php
<?php
require_once 'vendor/autoload.php';
require_once 'vendor/twig/twig/lib/Twig/Autoloader.php';
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem('views');
$twig = new Twig_Environment($loader, [
'cache' => 'cache',
]);
*/NewProject/views/layout.html
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: blue;
}
</style>
</head>
<body>
</body>
</html>
Add Blocks to the Layout
...
<body>
{% block content %}
{% endblock %}
{% block footer %}
© Copyright 2015 by MyCompany.
{% endblock %}
</body>
...
Blocks designate areas that may be replaced by child templates.
Block content in the layout will be the default.
*/NewProject/views/index.html
{% extends 'layout.html' %}
{% block content %}
<h1>Hello World!</h1>
{% endblock %}
*/NewProject/src/Page.php
<?php
namespace NewProject\Classes;
class Page
{
public function index()
{
return [
'salute' => 'goodnight',
'target' => 'moon'
];
*/NewProject/index.php
<?php
...
use NewProject\Classes\Page;
...
$page = new Page();
echo $twig->render('index.html', $page->index());
localhost/NewProject/index.php
*/NewProject/src/Page.php
return [
'salute' => 'goodnight',
'target' => 'moon'
];
*/NewProject/index.php
echo $twig->render('index.html', $page->index());
*/NewProject/views/index.html
{% extends 'layout.html' %}
{% block content %}
<h1>{{ salute }} {{ target }}!</h1>
{% endblock %}
localhost/NewProject/index.php
*/NewProject/views/index.html
{% extends 'layout.html' %}
{% block content %}
<h1>{{ salute|title }} {{ target|title }}!</h1>
{% endblock %}
{{ salute|capitalize }} = Goodnight
{{ salute|lower }} = goodnight
{{ salute|upper }} = GOODNIGHT
{{ salute }} moon = goodnight moon
{{ salute ~ ' ' ~ target }} = goodnight moon
{{ "goodnight ${target}" }} = goodnight moon
{{ "hi moon"|replace({ 'hi': 'goodnight' }) }} = goodnight moon
{{ -1234.567|round }} = -1235
{{ -1234.567|abs }} = 1234.567
{{ "1 + 2 = ${1 + 2}" }} = 1 + 2 = 3
{{ -1234.567|number_format(2) }} = -1,234.56
{{ 5 // 2 }} = 2
{{ 2 ** 3 }} = 8
{% set my_var = "I am a new value!" %}
{{ my_var }}
I am a new value!
trim upper url_encodereplace reverse round nl2br number_format convert_encoding date |
abs batch capitalize date_modify default escape first formatjoin json_encode |
keys last length lower merge rawslice sort split striptags |
---|
Implement the Twig_ExtensionInterface Interface
Register using the addExtension() method
Override the getFilters() method
Register functions using the getFunctions() method
Override the getTokenParsers() method
{{ "goodnight" starts with "g" }} = true
{{ "moon" ends with "p" }} = false
{{ "night" in "goodnight" }} = true
{{ 1 in [ 1, 2, 3 ] }} = true
{{ 1 is even }} = false
{{ 4 not in [ 1, 2, 3 ] }} = true
{{ "moon"|length < 3 }} = false
*/NewProject/views/index.html
{% if target|lower == 'sun' %}
{% set title_color = 'red' %}
{% else %}
{% set title_color = 'purple' %}
{% endif %}
<h1 style="color:{{ title_color }}">
{{ salute|title }} {{ target }}!
</h1>
*/NewProject/src/Page.php
public function index()
{
return [
'salute' => 'Goodnight',
'my_set' => [
'red' => 'Sun',
'blue' => 'Moon',
'purple' => 'Stars'
]
];
}
*/NewProject/views/index.html
{% for key,value in my_set %}
<h1 style="color: {{ key }}">
{{ loop.index }}. {{ salute }} {{ value }}!
</h1>
{% endfor %}
1. Goodnight Sun!
2. Goodnight Moon!
3. Goodnight Stars!
class MyRecord
{
protected title;
protected description;
protected link;
protected color;
...
}
*/NewProject/views/index.php
{% for record in my_records %}
<a href="{{ record.link }}">
<h1 style="color: {{ record.color }}">
{{ record.title }}
</h1>
</a>
<p>{{ record.description }}</p>
{% endfor %}
$my_array = [
1 => [
'title' => "My Title",
'description' => "This is a description",
'link' => "http://twig.sensiolabs.org",
'color' => "blue"
],
2 => [
'title' => "Other Title",
'description' => "Another description",
'link' => "http://getcomposer.org",
'color' => "green"
]
];
*/NewProject/views/index.php
{% for record in my_records %}
<a href="{{ record.link }}">
<h1 style="color: {{ record.color }}">
{{ record.title }}
</h1>
</a>
<p>{{ record.description }}</p>
{% endfor %}
*Bracket notation also works: myObject['objectProperty']
*Bracket notation also works: myArray['arrayKey']
*/NewProject/images/fancyheading.png
*/NewProject/views/heading.html
<div class="heading">
<img src="../images/fancyheading.png">
</div>
*/NewProject/views/feature.html
<div class="feature">
<img src="../images/feature.png">
</div>
*/NewProject/images/feature.png
*/NewProject/views/index.html
{% extends 'layout.html' %}
{% block content %}
{% include 'heading.html' %}
<h1>{{ salute|title }} {{ my_set.blue }}!</h1>
{% include 'feature.html' %}
{% endblock %}
localhost/NewProject/index.php
Margaret Staples
@dead_lugosi on twitter
deadlugosi on freenode