A brief introduction to Twig; a fast, concise, and secure template engine for PHP.
Linux + Apache + MySQL + PHP
Learn More
*/NewProject
*/NewProject/src
*/NewProject/views
*/NewProject/index.php
*/path/to/web/directory
Dependency Manager for PHP
Learn More
*/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/"
}
},
"require": {
"twig/twig": "~1.0"
}
}
*/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, array(
'cache' => 'cache',
));
*/NewProject/views/layout.html
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: blue;
}
</style>
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
*/NewProject/views/index.html
{% extends 'layout.html' %}
{% block content %}
<h1>Hello World!</h1>
{% endblock %}
*/NewProject/Classes/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/views/index.html
{% extends 'layout.html' %}
{% block content %}
<h1>{{ salute }} {{ target }}!</h1>
{% endblock %}
*/NewProject/Classes/Page.php
return [
'salute' => 'goodnight',
'target' => 'moon'
];
localhost/NewProject/index.php
*/NewProject/views/index.html
{% extends 'layout.html' %}
{% block content %}
<h1>{{ salute|title }} {{ target|title }}!</h1>
{% endblock %}
String Filters
Number Filters
Set Variable Value
{{ greeting|title }} = Goodbye Moon
Learn More
*/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/Classes/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 %}
*/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/views/decor.html
<hr style="color: red">
<hr style="color: blue">
<hr style="color: purple">
*/NewProject/views/index.html
{% extends 'layout.html' %}
{% block content %}
{% include 'decor.html' %}
<h1>{{ salute|title }} {{ my_set.blue }}!</h1>
{% include 'decor.html' %}
{% endblock %}
localhost/NewProject/index.php
Margaret Staples
@dead_lugosi on twitter
deadlugosi on freenode