Skip to content

Commit

Permalink
feature #5001 Best practices template names (WouterJ)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.3 branch.

Discussion
----------

Best practices template names

@javiereguiluz @weaverryan wanted your opinion on this.

Commits
-------

2b65304 Use snake_case for template names
  • Loading branch information
weaverryan committed Mar 13, 2015
2 parents 24f773c + 2b65304 commit 9846d97
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 62 deletions.
4 changes: 4 additions & 0 deletions best_practices/templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------

Expand Down
8 changes: 4 additions & 4 deletions book/controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 </book/templating>` chapter.
Expand Down
40 changes: 20 additions & 20 deletions book/forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
));
}
Expand Down Expand Up @@ -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

<!-- app/Resources/views/Default/new.html.php -->
<!-- app/Resources/views/default/new.html.php -->
<?php echo $view['form']->start($form) ?>
<?php echo $view['form']->widget($form) ?>
<?php echo $view['form']->end($form) ?>
Expand Down Expand Up @@ -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

<!-- app/Resources/views/Default/new.html.php -->
<!-- app/Resources/views/default/new.html.php -->
<?php echo $view['form']->form($form, array(
'attr' => array('novalidate' => 'novalidate'),
)) ?>
Expand Down Expand Up @@ -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) }}

Expand All @@ -794,7 +794,7 @@ of code. Of course, you'll usually need much more flexibility when rendering:

.. code-block:: html+php

<!-- app/Resources/views/Default/newAction.html.php -->
<!-- app/Resources/views/default/newAction.html.php -->
<?php echo $view['form']->start($form) ?>
<?php echo $view['form']->errors($form) ?>

Expand Down Expand Up @@ -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

<!-- app/Resources/views/Default/newAction.html.php -->
<!-- app/Resources/views/default/newAction.html.php -->
<?php echo $view['form']->start($form, array(
'action' => $view['router']->generate('target_route'),
'method' => 'GET',
Expand Down Expand Up @@ -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 %}
<div class="form_row">
Expand All @@ -1450,7 +1450,7 @@ do this, create a new template file that will store the new markup:

.. code-block:: html+php

<!-- app/Resources/views/Form/form_row.html.php -->
<!-- app/Resources/views/form/form_row.html.php -->
<div class="form_row">
<?php echo $view['form']->label($form, $label) ?>
<?php echo $view['form']->errors($form) ?>
Expand All @@ -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

<!-- app/Resources/views/Default/new.html.php -->
<?php $view['form']->setTheme($form, array('Form')) ?>
<!-- app/Resources/views/default/new.html.php -->
<?php $view['form']->setTheme($form, array('form')) ?>

<?php $view['form']->setTheme($form, array('Form', 'Form2')) ?>
<?php $view['form']->setTheme($form, array('form', 'form2')) ?>

<!-- ... render the form -->

Expand Down Expand Up @@ -1606,7 +1606,7 @@ file:
twig:
form:
resources:
- 'Form/fields.html.twig'
- 'form/fields.html.twig'
# ...
.. code-block:: xml
Expand All @@ -1621,7 +1621,7 @@ file:
<twig:config>
<twig:form>
<twig:resource>Form/fields.html.twig</twig:resource>
<twig:resource>form/fields.html.twig</twig:resource>
</twig:form>
<!-- ... -->
</twig:config>
Expand All @@ -1633,7 +1633,7 @@ file:
$container->loadFromExtension('twig', array(
'form' => array(
'resources' => array(
'Form/fields.html.twig',
'form/fields.html.twig',
),
),
// ...
Expand Down
2 changes: 1 addition & 1 deletion book/from_flat_php_to_symfony2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
Loading

0 comments on commit 9846d97

Please sign in to comment.