User Tools

Site Tools


symfony:symfony_tutorial_notes:creating_and_using_templates

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
symfony:symfony_tutorial_notes:creating_and_using_templates [2015/09/16 21:03] – [Template Formats] mithatsymfony: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 ''app/Resources/views/''.   * Templates typically live in ''app/Resources/views/''.
-  * Third-party bundle templates live in ''path/to/bundle/Resources/views/''. (Command line generator puts them here too it seems; also seems not a best practice.)+  * Third-party bundle templates live in ''path/to/bundle/Resources/views/''. (The command line generator puts them here too it seems, which I believe is no longer a best practice.)
  
 <code twig> <code twig>
Line 47: Line 47:
 ==== Including other Templates ==== ==== Including other Templates ====
 <code twig> <code twig>
-    {% for article in articles %} +{% for article in articles %} 
-        {{ include('article/article_details.html.twig', { 'article': article }) }} +    {{ include('article/article_details.html.twig', { 'article': article }) }} 
-    {% endfor %}+{% endfor %}
 </code> </code>
  
Line 118: Line 118:
 Consider using [[http://symfony.com/doc/current/cookbook/assetic/asset_management.html|Assetic]]. Consider using [[http://symfony.com/doc/current/cookbook/assetic/asset_management.html|Assetic]].
  
-With Twig, assuming you have parent:+With Twig, assuming you have the parent:
  
 <code twig> <code twig>
Line 130: Line 130:
 </code> </code>
  
-in 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 'base.html.twig' %} {% extends 'base.html.twig' %}
Line 152: Line 152:
  
 ===== Configuring and Using the templating Service ===== ===== Configuring and Using the templating Service =====
-TODO+<code php> 
 +return $this->render('article/index.html.twig'); 
 +</code>
  
 +is equivalent to:
 +
 +<code php>
 +use Symfony\Component\HttpFoundation\Response;
 +
 +$engine = $this->container->get('templating');
 +$content = $engine->render('article/index.html.twig');
 +
 +return $response = new Response($content);
 +</code>
 +
 +It's preconfigured out of the box. Further config is in:
 +
 +<file yaml app/config/config.yml>
 +framework:
 +    # ...
 +    templating: { engines: ['twig'] }
 +</file>
 ===== Overriding Bundle Templates ===== ===== Overriding Bundle Templates =====
-TODO+[[http://symfony.com/doc/current/book/templating.html#overriding-bundle-templates|The source]].
  
 ==== Overriding Core Templates ==== ==== Overriding Core Templates ====
-TODO+[[http://symfony.com/doc/current/book/templating.html#overriding-core-templates|The source]].
  
 ===== 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's base layout.
  
-===== Output Escaping ===== +  - Create an ''app/Resources/views/base.html.twig'' file that contains the main layout for your application. Internally, this template is called ''base.html.twig''. 
-TODO+  - Create a template for each "section" of your site. <file php app/Resources/views/blog/layout.html.twig>{% extends 'base.html.twig' %} 
 +{% block body %} 
 +    <h1>Blog Application</h1> 
 +    {% block content %}{% endblock %} 
 +{% endblock %}</file> 
 +  - Create individual templates for each page and make each extend the appropriate section template.<file php app/Resources/views/blog/index.html.twig>{% extends 'blog/layout.html.twig' %} 
 +{% block content %} 
 +    {% for entry in blog_entries %} 
 +        <h2>{{ entry.title }}</h2> 
 +        <p>{{ entry.body }}</p> 
 +    {% endfor %} 
 +{% endblock %}</file>
  
 +===== Output Escaping =====
 ==== 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 }}</code>
 +
 +You can also disable output escaping inside a ''{% block %}'' area or for an entire template. See [[http://twig.sensiolabs.org/doc/api.html#escaper-extension|Output Escaping]] in the Twig documentation.
 ==== Output Escaping in PHP ==== ==== Output Escaping in PHP ====
-TODO+In PHP, output escaping is not automatic, meaning you'll need to manually ''escape()'' where necessary: 
 + 
 +<code php> 
 +Hello <?php echo $view->escape($name) ?>  // HTML escapting by default 
 +var myMsg = 'Hello <?php echo $view->escape($name, 'js') ?>'; // Specify context 
 +</code>
  
 ===== Debugging ===== ===== Debugging =====
-TODO+''dump()'' is your friend. 
 + 
 +In PHP: 
 + 
 +<file php src/AppBundle/Controller/ArticleController.php> 
 +namespace AppBundle\Controller; 
 +class ArticleController extends Controller 
 +
 +    public function recentListAction() 
 +    { 
 +        $articles = ...; 
 +        dump($articles); 
 +        // ... 
 +    } 
 +
 +</file> 
 + 
 +In Twig: 
 + 
 +<file twig app/Resources/views/article/recent_list.html.twig> 
 +{{ dump(articles) }} 
 +{% for article in articles %} 
 +    <a href="/article/{{ article.slug }}"> 
 +        {{ article.title }} 
 +    </a> 
 +{% endfor %} 
 +</file> 
 + 
 +To ''dump()'' in Twig, Twig's ''debug'' setting (in ''config.yml'') must be ''true'' (which is the case for ''dev'' but not ''prod'').
  
 ===== Syntax Checking ===== ===== Syntax Checking =====
Line 186: Line 254:
  
 ===== Template Formats ===== ===== Template Formats =====
-Twig can render __any__ format.+Twig can render *any** format.
   * e.g.: XML ''article/index.xml.twig''.   * e.g.: XML ''article/index.xml.twig''.
  
symfony/symfony_tutorial_notes/creating_and_using_templates.1442437385.txt.gz · Last modified: 2015/09/16 21:03 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki