symfony:symfony_tutorial_notes:creating_and_using_templates
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
symfony:symfony_tutorial_notes:creating_and_using_templates [2015/09/16 21:10] – [Output Escaping] mithat | symfony:symfony_tutorial_notes:creating_and_using_templates [2015/09/16 21:41] (current) – [Including Stylesheets and JavaScripts in Twig] mithat | ||
---|---|---|---|
Line 3: | Line 3: | ||
===== TL;DR ===== | ===== TL;DR ===== | ||
* Templates typically live in '' | * Templates typically live in '' | ||
- | * Third-party bundle templates live in '' | + | * Third-party bundle templates live in '' |
<code twig> | <code twig> | ||
Line 47: | Line 47: | ||
==== Including other Templates ==== | ==== Including other Templates ==== | ||
<code twig> | <code twig> | ||
- | | + | {% for article in articles %} |
- | {{ include(' | + | {{ include(' |
- | {% endfor %} | + | {% endfor %} |
</ | </ | ||
Line 118: | Line 118: | ||
Consider using [[http:// | Consider using [[http:// | ||
- | With Twig, assuming you have a parent: | + | With Twig, assuming you have the parent: |
<code twig> | <code twig> | ||
Line 130: | Line 130: | ||
</ | </ | ||
- | in a child you can add to the assets defined in the parent: | + | in its child you can add to the assets defined in the parent: |
<code twig> | <code twig> | ||
{% extends ' | {% extends ' | ||
Line 152: | Line 152: | ||
===== Configuring and Using the templating Service ===== | ===== Configuring and Using the templating Service ===== | ||
- | TODO | + | <code php> |
+ | return $this-> | ||
+ | </ | ||
+ | is equivalent to: | ||
+ | |||
+ | <code php> | ||
+ | use Symfony\Component\HttpFoundation\Response; | ||
+ | |||
+ | $engine = $this-> | ||
+ | $content = $engine-> | ||
+ | |||
+ | return $response = new Response($content); | ||
+ | </ | ||
+ | |||
+ | It's preconfigured out of the box. Further config is in: | ||
+ | |||
+ | <file yaml app/ | ||
+ | framework: | ||
+ | # ... | ||
+ | templating: { engines: [' | ||
+ | </ | ||
===== Overriding Bundle Templates ===== | ===== Overriding Bundle Templates ===== | ||
- | TODO | + | [[http:// |
==== Overriding Core Templates ==== | ==== Overriding Core Templates ==== | ||
- | TODO | + | [[http:// |
===== Three-level Inheritance ===== | ===== Three-level Inheritance ===== | ||
- | TODO | + | The three-template model is a best-practice method used by vendor bundles so that the base template for a bundle can be easily overridden to properly extend your application' |
+ | |||
+ | - Create an '' | ||
+ | - Create a template for each " | ||
+ | {% block body %} | ||
+ | < | ||
+ | {% block content %}{% endblock %} | ||
+ | {% endblock %}</ | ||
+ | - Create individual templates for each page and make each extend the appropriate section template.< | ||
+ | {% block content %} | ||
+ | {% for entry in blog_entries %} | ||
+ | < | ||
+ | <p>{{ entry.body }}</ | ||
+ | {% endfor %} | ||
+ | {% endblock %}</ | ||
===== Output Escaping ===== | ===== Output Escaping ===== | ||
- | Output escaping is on by default in Twig, so you're protected. In PHP, output escaping is not automatic, meaning you'll need to manually escape where necessary. | ||
==== Output Escaping in Twig ==== | ==== Output Escaping in Twig ==== | ||
- | TODO | + | Output escaping is on by default in Twig, so you're protected. By default, the output escaping assumes that content is being escaped for HTML output. To bypass escaping, use the raw filter: |
+ | <code twig>{{ article.body|raw }}</ | ||
+ | |||
+ | You can also disable output escaping inside a '' | ||
==== Output Escaping in PHP ==== | ==== Output Escaping in PHP ==== | ||
- | TODO | + | In PHP, output escaping is not automatic, meaning you'll need to manually '' |
+ | |||
+ | <code php> | ||
+ | Hello <?php echo $view-> | ||
+ | var myMsg = 'Hello <?php echo $view-> | ||
+ | </ | ||
===== Debugging ===== | ===== Debugging ===== | ||
Line 200: | Line 241: | ||
</ | </ | ||
- | To '' | + | To '' |
===== Syntax Checking ===== | ===== Syntax Checking ===== | ||
Line 213: | Line 254: | ||
===== Template Formats ===== | ===== Template Formats ===== | ||
- | Twig can render | + | Twig can render |
* e.g.: XML '' | * e.g.: XML '' | ||
symfony/symfony_tutorial_notes/creating_and_using_templates.1442437846.txt.gz · Last modified: 2015/09/16 21:10 by mithat