Your SlideShare is downloading. ×
0
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Empezando con Twig
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Empezando con Twig

117

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
117
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Twig http://twig.sensiolabs.org/ Monday, September 16, 13
  • 2. “The flexible, fast, and secure template engine for PHP” Monday, September 16, 13
  • 3. Requerimientos Monday, September 16, 13
  • 4. Requerimientos PHP >= 5.2.4 Monday, September 16, 13
  • 5. Características Monday, September 16, 13
  • 6. 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
  • 7. Conciso Monday, September 16, 13
  • 8. Conciso <?php echo $variable ?> <?= $variable ?> <?php echo htmlspeciachars($variable) ?> <?php echo strtolower($variable) ?> Monday, September 16, 13
  • 9. <?php echo $variable ?> <?= $variable ?> {{ variable }} <?php echo htmlspeciachars($variable) ?> {{ variable|escape }} {{ variable|e }} <?php echo strtolower($variable) ?> {{ variable|lower }} Conciso Monday, September 16, 13
  • 10. Template Oriented Sintax Monday, September 16, 13
  • 11. Template Oriented Sintax if (0 == count($items)) { echo “No se han encontrado items.” } foreach ($items as $item) { echo “*” . $item; } Monday, September 16, 13
  • 12. 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
  • 13. Completo Monday, September 16, 13
  • 14. Completo • Herencia múltiple • Bloques • Escape automático • Inclusión Monday, September 16, 13
  • 15. Fácil de aprender Monday, September 16, 13
  • 16. Seguro Monday, September 16, 13
  • 17. Seguro {% autoescape true %} {{ var }} {{ var|raw }} {# escape inhabilitado #} {{ var|escape }} {# escape simple #} {% endautoescape %} {{ include('page.html', sandboxed = true) }} Monday, September 16, 13
  • 18. Mensajes de error descriptivos Monday, September 16, 13
  • 19. Rápido Monday, September 16, 13
  • 20. Sintaxis Monday, September 16, 13
  • 21. Sintaxis Tags {% block %}{% endblock %} {% for item in items %}{% endfor %} {% if condicion %}{% endif %} {% include “template.html” %} Monday, September 16, 13
  • 22. Sintaxis Filtros {{ variable|filtro }} {{ variable|filtro(true) }} {{ variable|filtro(“texto”) }} Monday, September 16, 13
  • 23. Sintaxis Funciones {{ function(variable) }} {{ function(variable, true) }} {{ function(variable, “texto”) }} Monday, September 16, 13
  • 24. Sintaxis Tests {% if variable is numero %} ... {% endif %} {% if variable is numero(...) %} ... {% endif %} Monday, September 16, 13
  • 25. Sintaxis Herencia <!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 %} &copy; Copyright 2011 by <a href="http://domain.invalid/">you</a>. {% endblock %} </div> </body> </html> Monday, September 16, 13
  • 26. Sintaxis Herencia <!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 %} &copy; 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
  • 27. ¿Como funciona? Monday, September 16, 13
  • 28. ¿Como funciona? 1.Carga 1.Lexer 2.Parseo 3.Conversión 2.Evaluación Monday, September 16, 13
  • 29. Twig vs. PHP Monday, September 16, 13
  • 30. Twig vs. PHP for - foreach {% for number in 1..10 %} * {{ number }} {% endfor %} {% for item in items %} * {{ item.name }} {% endfor %} Monday, September 16, 13
  • 31. Twig vs. PHP for - foreach {% for item in items %} {{ loop.index }} {{ item.name }} {% endfor %} loop.index loop.first loop.length loop.revindex loop.last loop.parent Monday, September 16, 13
  • 32. Twig vs. PHP if {% if condicion %} Si {% elseif condicion2 %} Puede ser... {% else %} No {% endif %} Monday, September 16, 13
  • 33. Twig vs. PHP include - 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
  • 34. Twig vs. PHP function {% macro saludo(nombre, apellido) %} <div>Hola {{ nombre }} {{ apellido }}</div> {% endmacro %} Monday, September 16, 13
  • 35. Twig vs. PHP function {% macro saludo(nombre, apellido) %} <div>Hola {{ nombre }} {{ apellido }}</div> {% endmacro %} {% import "macros.html" as macros %} Monday, September 16, 13
  • 36. Twig vs. PHP dump {{ dump(variable) }} {{ dump() }} Monday, September 16, 13
  • 37. Uso básico Monday, September 16, 13
  • 38. Uso básico Environment $twig = new Twig_Environment($loader, $options); Monday, September 16, 13
  • 39. Uso básico Environment $twig = new Twig_Environment($loader, $options); $template = $twig->loadTemplate(“tpl.html”); $twig->render(“tpl.html”, array(“var” => true)); Monday, September 16, 13
  • 40. Uso básico Environment $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
  • 41. Uso básico Loaders $loader = new Twig_Loader_Filesystem($dir); $loader = new Twig_Loader_Filesystem(array( $dir1, $dir2 )); $twig = new Twig_Environment($loader, $options); Monday, September 16, 13
  • 42. Uso básico Loaders $loader = new Twig_Loader_String(); $twig = new Twig_Environment($loader, $options); $twig->render(“Hola {{ nombre }}”, array( “nombre” => “Ismael” )); Monday, September 16, 13
  • 43. Uso básico Loaders $loader = new Twig_Loader_Array(array( “template1.html” => “Hello {{ name }}”, “template2.html” => “Hola {{ nombre }}” )); $twig = new Twig_Environment($loader, $options); Monday, September 16, 13
  • 44. Extendiendo Twig Monday, September 16, 13
  • 45. Extendiendo Twig Extensiones Clases PHP o paquetes que agregan funcionaliades al motor de templates. Monday, September 16, 13
  • 46. Extendiendo Twig Extensiones • 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
  • 47. $twig = new Twig_Environment($loader, $options); $twig->addExtension(new Extension()); Extendiendo Twig Extensiones Monday, September 16, 13
  • 48. class Mi_Extension extends Twig_Extension { function getFilters() {} function getTests() {} function getFunctions() {} function getGlobals() {} function getName() {} } Extendiendo Twig Monday, September 16, 13
  • 49. 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 Twig Filtros Monday, September 16, 13
  • 50. 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 Twig Tests Monday, September 16, 13
  • 51. 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 Twig Funciones Monday, September 16, 13
  • 52. class Mi_Extension extends Twig_Extension { public function getGlobals() { return array( “nombre” => “Ismael”, “apellido” => “Ambrosi” ); } } Extendiendo Twig Globals Monday, September 16, 13
  • 53. ¿Preguntas? Monday, September 16, 13
  • 54. ¡Gracias! Monday, September 16, 13

×