Download - Empezando con Twig
Twig
http://twig.sensiolabs.org/Monday, September 16, 13
Características
• Conciso
• Template Oriented Sintax
• Completo
• Fácil de aprender
• Extensible
• Probado
• Documentado
• Seguro
• Mensajes de error descriptivos
• Rápido
Monday, September 16, 13
Conciso
<?php echo $variable ?><?= $variable ?>
<?php echo htmlspeciachars($variable) ?>
<?php echo strtolower($variable) ?>
Monday, September 16, 13
<?php echo $variable ?><?= $variable ?>{{ variable }}
<?php echo htmlspeciachars($variable) ?>{{ variable|escape }}{{ variable|e }}
<?php echo strtolower($variable) ?>{{ variable|lower }}
Conciso
Monday, September 16, 13
Template Oriented Sintax
if (0 == count($items)) {echo “No se han encontrado items.”
}
foreach ($items as $item) {echo “*” . $item;
}
Monday, September 16, 13
Template Oriented Sintax
if (0 == count($items)) {echo “No se han encontrado items.”
}
foreach ($items as $item) {echo “*” . $item
}
{% for item in items %} * {{ item.name }}{% else %} No se han encontrado items.{% endfor %}
Monday, September 16, 13
Seguro
{% autoescape true %} {{ var }} {{ var|raw }} {# escape inhabilitado #} {{ var|escape }} {# escape simple #}{% endautoescape %}
{{ include('page.html', sandboxed = true) }}
Monday, September 16, 13
SintaxisTags
{% block %}{% endblock %}
{% for item in items %}{% endfor %}
{% if condicion %}{% endif %}
{% include “template.html” %}
Monday, September 16, 13
SintaxisFiltros
{{ variable|filtro }}
{{ variable|filtro(true) }}
{{ variable|filtro(“texto”) }}
Monday, September 16, 13
SintaxisFunciones
{{ function(variable) }}
{{ function(variable, true) }}
{{ function(variable, “texto”) }}
Monday, September 16, 13
SintaxisTests
{% if variable is numero %} ...{% endif %}
{% if variable is numero(...) %} ...{% endif %}
Monday, September 16, 13
SintaxisHerencia
<!DOCTYPE html><html> <head> {% block head %} <link rel="stylesheet" href="style.css" /> <title>{% block title %}{% endblock %} - My Webpage</title> {% endblock %} </head> <body> <div id="content">{% block content %}{% endblock %}</div> <div id="footer"> {% block footer %} © Copyright 2011 by <a href="http://domain.invalid/">you</a>. {% endblock %} </div> </body></html>
Monday, September 16, 13
SintaxisHerencia
<!DOCTYPE html><html> <head> {% block head %} <link rel="stylesheet" href="style.css" /> <title>{% block title %}{% endblock %} - My Webpage</title> {% endblock %} </head> <body> <div id="content">{% block content %}{% endblock %}</div> <div id="footer"> {% block footer %} © Copyright 2011 by <a href="http://domain.invalid/">you</a>. {% endblock %} </div> </body></html>
{% extends "base.html" %}
{% block title %}Index{% endblock %}{% block content %} <h1>Index</h1> <p class="important"> Welcome to my awesome homepage. </p>{% endblock %}
Monday, September 16, 13
Twig vs. PHPfor - foreach
{% for number in 1..10 %} * {{ number }}{% endfor %}
{% for item in items %} * {{ item.name }}{% endfor %}
Monday, September 16, 13
Twig vs. PHPfor - foreach
{% for item in items %} {{ loop.index }} {{ item.name }}{% endfor %}
loop.indexloop.firstloop.length
loop.revindexloop.last
loop.parent
Monday, September 16, 13
Twig vs. PHPif
{% if condicion %} Si{% elseif condicion2 %} Puede ser...{% else %} No{% endif %}
Monday, September 16, 13
Twig vs. PHPinclude - require
{% include “template.html” %}
{% include “template.html” with {“foo”: “bar”} %}
{% include “template.html” only %}
{% include “template.html” ignore missing %}
{% include [“template.html”, “template2.html”] %}
Monday, September 16, 13
Twig vs. PHPfunction
{% macro saludo(nombre, apellido) %} <div>Hola {{ nombre }} {{ apellido }}</div>{% endmacro %}
Monday, September 16, 13
Twig vs. PHPfunction
{% macro saludo(nombre, apellido) %} <div>Hola {{ nombre }} {{ apellido }}</div>{% endmacro %}
{% import "macros.html" as macros %}
Monday, September 16, 13
Uso básicoEnvironment
$twig = new Twig_Environment($loader, $options);
$template = $twig->loadTemplate(“tpl.html”);
$twig->render(“tpl.html”, array(“var” => true));
Monday, September 16, 13
Uso básicoEnvironment
$twig = new Twig_Environment($loader, array( “debug” => false, “charset” => “utf-8”, “base_template_class” => “Twig_Template”, “cache” => “/path/to/cache”, “auto_reload” => false, “strict_variables” => false, “autoescape” => true, “optimizations” => -1));
Monday, September 16, 13
Uso básicoLoaders
$loader = new Twig_Loader_Filesystem($dir);$loader = new Twig_Loader_Filesystem(array( $dir1, $dir2));
$twig = new Twig_Environment($loader, $options);
Monday, September 16, 13
Uso básicoLoaders
$loader = new Twig_Loader_String();$twig = new Twig_Environment($loader, $options);
$twig->render(“Hola {{ nombre }}”, array( “nombre” => “Ismael”));
Monday, September 16, 13
Uso básicoLoaders
$loader = new Twig_Loader_Array(array( “template1.html” => “Hello {{ name }}”, “template2.html” => “Hola {{ nombre }}”));
$twig = new Twig_Environment($loader, $options);
Monday, September 16, 13
Extendiendo TwigExtensiones
Clases PHP o paquetes que agregan funcionaliades al motor de templates.
Monday, September 16, 13
Extendiendo TwigExtensiones
• Twig_Extension_Core: Agrega todas las funcionalidades principales de Twig
• Twig_Extension_Escaper: Agrega funcionalidades de escape de salida
• Twig_Extension_Sandbox: Habilita el modo sandbox
• Twig_Extension_Optimizer: Optimiza el Árbol abstracto de sintaxis
Monday, September 16, 13
$twig = new Twig_Environment($loader, $options);
$twig->addExtension(new Extension());
Extendiendo TwigExtensiones
Monday, September 16, 13
class Mi_Extension extends Twig_Extension{ function getFilters() {}
function getTests() {}
function getFunctions() {}
function getGlobals() {}
function getName() {}}
Extendiendo Twig
Monday, September 16, 13
class Mi_Extension extends Twig_Extension{ public function getFilters() {
return array(new Twig_SimpleFilter(“json”, “json_encode”),“metodo” => new Twig_Filter_Method($this, “miMetodo”)
); }
public function miMetodo() { // ... }}
Extendiendo TwigFiltros
Monday, September 16, 13
class Mi_Extension extends Twig_Extension{ public function getTests() {
return array(“numero” => new Twig_Test_Method($this, “esNumero”)
); }
public function esNumero($valor) { return is_numeric($valor); }}
Extendiendo TwigTests
Monday, September 16, 13
class Mi_Extension extends Twig_Extension{ public function getFunctions() {
return array( new Twig_SimpleFunction(“dump”, “var_dump”),
“funcion” => new Twig_Function_Method($this, “miMetodo”));
}
public function miMetodo() { // ... }}
Extendiendo TwigFunciones
Monday, September 16, 13
class Mi_Extension extends Twig_Extension{ public function getGlobals() {
return array(“nombre” => “Ismael”,“apellido” => “Ambrosi”
); }}
Extendiendo TwigGlobals
Monday, September 16, 13