26. lib/twig/Extesion/Core.php
class Twig_Extension_Core extends Twig_Extension {
public function getTokenParsers() {
return array(
new Twig_TokenParser_For(),
new Twig_TokenParser_If(),
new Twig_TokenParser_Extends(),
new Twig_TokenParser_Include(),
new Twig_TokenParser_Block(),
// ...
);
}
public function getFilters() {
$filters = array(
'format' => new Twig_Filter_Function('sprintf'),
'replace' => new Twig_Filter_Function('strtr'),
'abs' => new Twig_Filter_Function('abs'),
27. {{ array|sort }}
/**
* Sorts an array.
*
* @param array $array An array
*/
function twig_sort_filter($array)
{
asort($array);
return $array;
}
31. class MiCoreExtension extends Twig_Extension_Core {
public function getFilters() {
return array_merge(
parent::getFilters(),
array( ... )
);
}
}
32. $twig = new Twig_Environment($loader, array( ... ));
$twig->addExtension(new MiCoreExtension());
33. class MiCoreExtension extends Twig_Extension_Core
{
public function getFilters()
{
return array_merge(parent::getFilters(), array(
'sort' => new Twig_Filter_Method($this, 'sortFilter')
));
}
public function sortFilter($array)
{
natcasesort($array);
return $array;
}
}
49. {% filter escape('js') %}
<script type="text/javascript">
var texto = "<p>Lorem ipsum dolor sit amet</p>";
alert(texto);
</script>
{% endfilter %}
{% filter escape('html') %}
<script type="text/javascript">
var texto = "<p>Lorem ipsum dolor sit amet</p>";
alert(texto);
</script>
{% endfilter %}
50. x3cscript typex3dx22textx2fjavascriptx22x3ex0a
var texto x3d x22x3cpx3eLorem ipsum dolor sit amet
x3cx2fpx3ex22x3bx0a alertx28textox29x3bx0a
x3cx2fscriptx3ex0a
<script type="text/javascript">
var texto = "<p>Lorem ipsum dolor sit
amet</p>";
alert(texto);
</script>
135. La oferta cuesta 25.78 euros
(30.42 con IVA) y es válida
hasta el 10/06/2012
136. La oferta cuesta 25.78 euros
(30.42 con IVA) y es válida
hasta el 10/06/2012
137. La oferta cuesta 25.78 euros (30.42 con IVA) y es
válida hasta el 10/06/2012
~
{{ 'La oferta cuesta ' ~ oferta.precio ~ ' euros (' ~
oferta.precio*1.18 ~ ' con IVA) y es válida hasta el ' ~
oferta.fechaExpiracion|date() }}
138. La oferta cuesta 25.78 euros (30.42 con IVA) y es
válida hasta el 10/06/2012
format()
{{ 'La oferta cuesta %.2f euros (%.2f con IVA) y es
válida hasta el %s'|format(oferta.precio,
oferta.precio*1.18, oferta.fechaExpiracion|date()) }}
139. La oferta cuesta 25.78 euros (30.42 con IVA) y es
válida hasta el 10/06/2012
replace()
{{ 'La oferta cuesta :precio euros (:total con IVA) y es
válida hasta el :fecha'|replace({ ':precio':
oferta.precio, ':total': oferta.precio*1.18, ':fecha':
oferta.fechaExpiracion|date() }) }}
140. La oferta cuesta 25.78 euros (30.42 con IVA) y es
válida hasta el 10/06/2012
{{ "La oferta cuesta #{oferta.precio}
euros (#{oferta.precio*1.18} con IVA)
y es válida hasta el
#{oferta.fechaExpiracion|date()}" }}
169. $twig = new Twig_Environment($loader, array(..));
try {
$twig->parse($twig->tokenize($plantilla));
echo "[OK] La sintaxis de la plantilla es correcta";
} catch (Twig_Error_Syntax $e) {
echo "[ERROR] La plantilla tiene errores de sintaxis";
}