Skip to content

Commit

Permalink
Moved templates to app/Resources/views
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed Nov 5, 2014
1 parent 87b324a commit 0ef1c68
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 27 deletions.
2 changes: 2 additions & 0 deletions best_practices/templates.rst
Expand Up @@ -22,6 +22,8 @@ In addition, Twig is the only template format with guaranteed support in Symfony
3.0. As a matter of fact, PHP may be removed from the officially supported
template engines.

.. _best_practices-template-location:

Template Locations
------------------

Expand Down
42 changes: 15 additions & 27 deletions book/controller.rst
Expand Up @@ -539,57 +539,45 @@ content from the template can be used to create a ``Response`` object::

use Symfony\Component\HttpFoundation\Response;

$content = $this->renderView(
'AcmeHelloBundle:Hello:index.html.twig',
array('name' => $name)
);
$content = $this->renderView('Hello/index.html.twig', array('name' => $name));

return new Response($content);

This can even be done in just one step with the ``render()`` method, which
returns a ``Response`` object containing the content from the template::

return $this->render(
'AcmeHelloBundle:Hello:index.html.twig',
array('name' => $name)
);
return $this->render('Hello/index.html.twig', array('name' => $name));

In both cases, the ``Resources/views/Hello/index.html.twig`` template inside
the ``AcmeHelloBundle`` will be rendered.
In both cases, the ``app/Resources/views/Hello/index.html.twig`` template will
be rendered.

The Symfony templating engine is explained in great detail in the
:doc:`Templating </book/templating>` chapter.
.. sidebar:: Referencing Templates that Live inside the Bundle

.. tip::
You can also put templates in the ``Resources/views`` directory of a
bundle. You can then reference is with the
``BundleName:DirectoryName:FileName`` syntax. E.g.
``AppBundle:Hello:index.html.twig`` would refer to the template located in
``src/AppBundle/Resources/views/Hello/index.html.twig``.

You can even avoid calling the ``render`` method by using the ``@Template``
annotation. See the
:doc:`FrameworkExtraBundle documentation </bundles/SensioFrameworkExtraBundle/annotations/view>`
more details.
The Symfony templating engine is explained in great detail in the
:doc:`Templating </book/templating>` chapter.

.. tip::

The ``renderView`` method is a shortcut to direct use of the ``templating``
service. The ``templating`` service can also be used directly::

$templating = $this->get('templating');
$content = $templating->render(
'AcmeHelloBundle:Hello:index.html.twig',
array('name' => $name)
);
$content = $templating->render('Hello/index.html.twig', array('name' => $name));

.. note::

It is possible to render templates in deeper subdirectories as well, however
be careful to avoid the pitfall of making your directory structure unduly
elaborate::

$templating->render(
'AcmeHelloBundle:Hello/Greetings:index.html.twig',
array('name' => $name)
);
// index.html.twig found in Resources/views/Hello/Greetings
// is rendered.
$templating->render('Hello/Greetings/index.html.twig', array('name' => $name));
// renders app/Resources/views/Hello/Greetings/index.html.twig

.. index::
single: Controller; Accessing services
Expand Down

0 comments on commit 0ef1c68

Please sign in to comment.