diff --git a/best_practices/templates.rst b/best_practices/templates.rst index d47c6a1c48d..402f4fd2898 100644 --- a/best_practices/templates.rst +++ b/best_practices/templates.rst @@ -51,6 +51,10 @@ Another advantage is that centralizing your templates simplifies the work of your designers. They don't need to look for templates in lots of directories scattered through lots of bundles. +.. best-practice:: + + Use lowercased snake_case for directory and template names. + Twig Extensions --------------- diff --git a/book/controller.rst b/book/controller.rst index 942d5a5baab..06124fcadc0 100644 --- a/book/controller.rst +++ b/book/controller.rst @@ -471,14 +471,14 @@ If you're serving HTML, you'll want to render a template. The ``render()`` method renders a template **and** puts that content into a ``Response`` object for you:: - // renders app/Resources/views/Hello/index.html.twig - return $this->render('Hello/index.html.twig', array('name' => $name)); + // renders app/Resources/views/hello/index.html.twig + return $this->render('hello/index.html.twig', array('name' => $name)); You can also put templates in deeper sub-directories. Just try to avoid creating unnecessarily deep structures:: - // renders app/Resources/views/Hello/Greetings/index.html.twig - return $this->render('Hello/Greetings/index.html.twig', array('name' => $name)); + // renders app/Resources/views/hello/greetings/index.html.twig + return $this->render('hello/greetings/index.html.twig', array('name' => $name)); The Symfony templating engine is explained in great detail in the :doc:`Templating ` chapter. diff --git a/book/forms.rst b/book/forms.rst index 48093e75efb..b53384d779e 100644 --- a/book/forms.rst +++ b/book/forms.rst @@ -96,7 +96,7 @@ from inside a controller:: ->add('save', 'submit', array('label' => 'Create Task')) ->getForm(); - return $this->render('Default/new.html.twig', array( + return $this->render('default/new.html.twig', array( 'form' => $form->createView(), )); } @@ -144,14 +144,14 @@ helper functions: .. code-block:: html+jinja - {# app/Resources/views/Default/new.html.twig #} + {# app/Resources/views/default/new.html.twig #} {{ form_start(form) }} {{ form_widget(form) }} {{ form_end(form) }} .. code-block:: html+php - + start($form) ?> widget($form) ?> end($form) ?> @@ -442,12 +442,12 @@ corresponding errors printed out with the form. .. code-block:: html+jinja - {# app/Resources/views/Default/new.html.twig #} + {# app/Resources/views/default/new.html.twig #} {{ form(form, {'attr': {'novalidate': 'novalidate'}}) }} .. code-block:: html+php - + form($form, array( 'attr' => array('novalidate' => 'novalidate'), )) ?> @@ -784,7 +784,7 @@ of code. Of course, you'll usually need much more flexibility when rendering: .. code-block:: html+jinja - {# app/Resources/views/Default/new.html.twig #} + {# app/Resources/views/default/new.html.twig #} {{ form_start(form) }} {{ form_errors(form) }} @@ -794,7 +794,7 @@ of code. Of course, you'll usually need much more flexibility when rendering: .. code-block:: html+php - + start($form) ?> errors($form) ?> @@ -1002,12 +1002,12 @@ to the ``form()`` or the ``form_start()`` helper: .. code-block:: html+jinja - {# app/Resources/views/Default/new.html.twig #} + {# app/Resources/views/default/new.html.twig #} {{ form_start(form, {'action': path('target_route'), 'method': 'GET'}) }} .. code-block:: html+php - + start($form, array( 'action' => $view['router']->generate('target_route'), 'method' => 'GET', @@ -1437,7 +1437,7 @@ do this, create a new template file that will store the new markup: .. code-block:: html+jinja - {# app/Resources/views/Form/fields.html.twig #} + {# app/Resources/views/form/fields.html.twig #} {% block form_row %} {% spaceless %}
@@ -1450,7 +1450,7 @@ do this, create a new template file that will store the new markup: .. code-block:: html+php - +
label($form, $label) ?> errors($form) ?> @@ -1466,19 +1466,19 @@ renders the form: .. code-block:: html+jinja - {# app/Resources/views/Default/new.html.twig #} - {% form_theme form 'Form/fields.html.twig' %} + {# app/Resources/views/default/new.html.twig #} + {% form_theme form 'form/fields.html.twig' %} - {% form_theme form 'Form/fields.html.twig' 'Form/fields2.html.twig' %} + {% form_theme form 'form/fields.html.twig' 'Form/fields2.html.twig' %} {# ... render the form #} .. code-block:: html+php - - setTheme($form, array('Form')) ?> + + setTheme($form, array('form')) ?> - setTheme($form, array('Form', 'Form2')) ?> + setTheme($form, array('form', 'form2')) ?> @@ -1606,7 +1606,7 @@ file: twig: form: resources: - - 'Form/fields.html.twig' + - 'form/fields.html.twig' # ... .. code-block:: xml @@ -1621,7 +1621,7 @@ file: - Form/fields.html.twig + form/fields.html.twig @@ -1633,7 +1633,7 @@ file: $container->loadFromExtension('twig', array( 'form' => array( 'resources' => array( - 'Form/fields.html.twig', + 'form/fields.html.twig', ), ), // ... diff --git a/book/from_flat_php_to_symfony2.rst b/book/from_flat_php_to_symfony2.rst index 364546977c9..37bd6d8ecd0 100644 --- a/book/from_flat_php_to_symfony2.rst +++ b/book/from_flat_php_to_symfony2.rst @@ -710,7 +710,7 @@ for example, the list template written in Twig: .. code-block:: html+jinja - {# app/Resources/views/Blog/list.html.twig #} + {# app/Resources/views/blog/list.html.twig #} {% extends "layout.html.twig" %} {% block title %}List of Posts{% endblock %} diff --git a/book/http_cache.rst b/book/http_cache.rst index d01f999877e..f550c5cdb23 100644 --- a/book/http_cache.rst +++ b/book/http_cache.rst @@ -732,7 +732,7 @@ exposing a simple and efficient pattern:: $comments = ...; // or render a template with the $response you've already started - return $this->render('Article/show.html.twig', array( + return $this->render('article/show.html.twig', array( 'article' => $article, 'comments' => $comments ), $response); diff --git a/book/templating.rst b/book/templating.rst index 4b003e8bad6..f30c2356ee1 100644 --- a/book/templating.rst +++ b/book/templating.rst @@ -263,7 +263,7 @@ A child template might look like this: .. code-block:: html+jinja - {# app/Resources/views/Blog/index.html.twig #} + {# app/Resources/views/blog/index.html.twig #} {% extends 'base.html.twig' %} {% block title %}My cool blog posts{% endblock %} @@ -277,7 +277,7 @@ A child template might look like this: .. code-block:: html+php - + extend('base.html.php') ?> set('title', 'My cool blog posts') ?> @@ -399,8 +399,8 @@ Most of the templates you'll use live in the ``app/Resources/views/`` directory. The path you'll use will be relative to this directory. For example, to render/extend ``app/Resources/views/base.html.twig``, you'll use the ``base.html.twig`` path and to render/extend -``app/Resources/views/Blog/index.html.twig``, you'll use the -``Blog/index.html.twig`` path. +``app/Resources/views/blog/index.html.twig``, you'll use the +``blog/index.html.twig`` path. .. _template-referencing-in-bundle: @@ -453,9 +453,9 @@ Every template name also has two extensions that specify the *format* and ======================== ====== ====== Filename Format Engine ======================== ====== ====== -``Blog/index.html.twig`` HTML Twig -``Blog/index.html.php`` HTML PHP -``Blog/index.css.twig`` CSS Twig +``blog/index.html.twig`` HTML Twig +``blog/index.html.php`` HTML PHP +``blog/index.css.twig`` CSS Twig ======================== ====== ====== By default, any Symfony template can be written in either Twig or PHP, and @@ -518,7 +518,7 @@ template. First, create the template that you'll need to reuse. .. code-block:: html+jinja - {# app/Resources/views/Article/articleDetails.html.twig #} + {# app/Resources/views/article/article_details.html.twig #}

{{ article.title }}

@@ -528,7 +528,7 @@ template. First, create the template that you'll need to reuse. .. code-block:: html+php - +

getTitle() ?>

@@ -542,20 +542,20 @@ Including this template from any other template is simple: .. code-block:: html+jinja - {# app/Resources/views/Article/list.html.twig #} + {# app/Resources/views/article/list.html.twig #} {% extends 'layout.html.twig' %} {% block body %}

Recent Articles

{% for article in articles %} - {{ include('Article/articleDetails.html.twig', { 'article': article }) }} + {{ include('article/article_details.html.twig', { 'article': article }) }} {% endfor %} {% endblock %} .. code-block:: html+php - + extend('layout.html.php') ?> start('body') ?> @@ -563,17 +563,17 @@ Including this template from any other template is simple: render( - 'Article/articleDetails.html.php', + 'Article/article_details.html.php', array('article' => $article) ) ?> stop() ?> The template is included using the ``{{ include() }}`` function. Notice that the -template name follows the same typical convention. The ``articleDetails.html.twig`` +template name follows the same typical convention. The ``article_details.html.twig`` template uses an ``article`` variable, which we pass to it. In this case, you could avoid doing this entirely, as all of the variables available in -``list.html.twig`` are also available in ``articleDetails.html.twig`` (unless +``list.html.twig`` are also available in ``article_details.html.twig`` (unless you set `with_context`_ to false). .. tip:: @@ -617,7 +617,7 @@ articles:: $articles = ...; return $this->render( - 'Article/recentList.html.twig', + 'article/recent_list.html.twig', array('articles' => $articles) ); } @@ -629,7 +629,7 @@ The ``recentList`` template is perfectly straightforward: .. code-block:: html+jinja - {# app/Resources/views/Article/recentList.html.twig #} + {# app/Resources/views/article/recent_list.html.twig #} {% for article in articles %} {{ article.title }} @@ -638,7 +638,7 @@ The ``recentList`` template is perfectly straightforward: .. code-block:: html+php - + getTitle() ?> @@ -812,7 +812,7 @@ any global default template that is defined): .. code-block:: jinja {{ render_hinclude(controller('...'), { - 'default': 'Default/content.html.twig' + 'default': 'default/content.html.twig' }) }} .. code-block:: php @@ -821,7 +821,7 @@ any global default template that is defined): new ControllerReference('...'), array( 'renderer' => 'hinclude', - 'default' => 'Default/content.html.twig', + 'default' => 'default/content.html.twig', ) ) ?> @@ -957,7 +957,7 @@ correctly: .. code-block:: html+jinja - {# app/Resources/views/Article/recentList.html.twig #} + {# app/Resources/views/article/recent_list.html.twig #} {% for article in articles %} {{ article.title }} @@ -966,7 +966,7 @@ correctly: .. code-block:: html+php - + getRequestFormat(); - return $this->render('Blog/index.'.$format.'.twig'); + return $this->render('article/index.'.$format.'.twig'); } The ``getRequestFormat`` on the ``Request`` object defaults to ``html``, @@ -1579,7 +1579,7 @@ their use is not mandatory. The ``Response`` object returned by a controller can be created with or without the use of a template:: // creates a Response object whose content is the rendered template - $response = $this->render('Article/index.html.twig'); + $response = $this->render('article/index.html.twig'); // creates a Response object whose content is simple text $response = new Response('response content'); diff --git a/book/validation.rst b/book/validation.rst index 4797d160ebb..92958ee094c 100644 --- a/book/validation.rst +++ b/book/validation.rst @@ -166,7 +166,7 @@ will appear. You could also pass the collection of errors into a template:: if (count($errors) > 0) { - return $this->render('Author/validation.html.twig', array( + return $this->render('author/validation.html.twig', array( 'errors' => $errors, )); } @@ -177,7 +177,7 @@ Inside the template, you can output the list of errors exactly as needed: .. code-block:: html+jinja - {# app/Resources/views/Author/validation.html.twig #} + {# app/Resources/views/author/validation.html.twig #}

The author has the following errors

    {% for error in errors %} @@ -187,7 +187,7 @@ Inside the template, you can output the list of errors exactly as needed: .. code-block:: html+php - +

    The author has the following errors

      @@ -235,7 +235,7 @@ workflow looks like the following from inside a controller:: return $this->redirect($this->generateUrl(...)); } - return $this->render('Author/form.html.twig', array( + return $this->render('author/form.html.twig', array( 'form' => $form->createView(), )); }