Skip to content

Updated templating/* articles to Symfony 4 #8734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions templating.rst
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ configuration:
/**
* @Route("/", name="welcome")
*/
public function indexAction()
public function index()
{
// ...
}
Expand All @@ -586,19 +586,19 @@ configuration:
# config/routes.yaml
welcome:
path: /
defaults: { _controller: AppBundle:Welcome:index }
controller: App\Controller\WelcomeController::index

.. code-block:: xml

<!-- config/routes.yaml -->
<!-- config/routes.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="welcome" path="/">
<default key="_controller">AppBundle:Welcome:index</default>
<default key="_controller">App\Controller\WelcomeController::index</default>
</route>
</routes>

Expand All @@ -610,7 +610,7 @@ configuration:

$collection = new RouteCollection();
$collection->add('welcome', new Route('/', array(
'_controller' => 'AppBundle:Welcome:index',
'_controller' => 'App\Controller\WelcomeController::index',
)));

return $collection;
Expand Down Expand Up @@ -644,7 +644,7 @@ route:
/**
* @Route("/article/{slug}", name="article_show")
*/
public function showAction($slug)
public function show($slug)
{
// ...
}
Expand All @@ -654,8 +654,8 @@ route:

# config/routes.yaml
article_show:
path: /article/{slug}
defaults: { _controller: AppBundle:Article:show }
path: /article/{slug}
controller: App\Controller\ArticleController::show

.. code-block:: xml

Expand All @@ -667,7 +667,7 @@ route:
http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="article_show" path="/article/{slug}">
<default key="_controller">AppBundle:Article:show</default>
<default key="_controller">App\Controller\ArticleController::show</default>
</route>
</routes>

Expand All @@ -679,7 +679,7 @@ route:

$collection = new RouteCollection();
$collection->add('article_show', new Route('/article/{slug}', array(
'_controller' => 'AppBundle:Article:show',
'_controller' => 'App\Controller\ArticleController::show',
)));

return $collection;
Expand Down Expand Up @@ -791,10 +791,10 @@ advantage of Symfony's template inheritance.
.. tip::

This section will teach you the philosophy behind including stylesheet
and JavaScript assets in Symfony. Symfony is also compatible with another
library, called Assetic, which follows this philosophy but allows you to do
much more interesting things with those assets. For more information on
using Assetic see :doc:`/frontend/assetic/asset_management`.
and JavaScript assets in Symfony. If you are interested in compiling and
creating those assets, check out the :doc:`Webpack Encore documentation </frontend>`
a tool that seamlessly integrates Webpack and other modern JavaScript tools
into Symfony applications.

Start by adding two blocks to your base template that will hold your assets:
one called ``stylesheets`` inside the ``head`` tag and another called ``javascripts``
Expand Down Expand Up @@ -878,7 +878,7 @@ to add to the parent block's content (and not actually *replace* it), you
should use the ``parent()`` Twig function to include everything from the ``stylesheets``
block of the base template.

You can also include assets located in your bundles' ``Resources/public`` folder.
You can also include assets located in your bundles' ``Resources/public/`` folder.
You will need to run the ``php bin/console assets:install target [--symlink]``
command, which copies (or symlinks) files into the correct location. (target
is by default the "web/" directory of your application).
Expand Down
118 changes: 39 additions & 79 deletions templating/PHP.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ your application configuration file:

.. code-block:: yaml

# app/config/config.yml
# config/packages/framework.yaml
framework:
# ...
templating:
engines: ['twig', 'php']

.. code-block:: xml

<!-- app/config/config.xml -->
<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand All @@ -53,6 +53,7 @@ your application configuration file:

.. code-block:: php

// config/packages/framework.php
$container->loadFromExtension('framework', array(
// ...
'templating' => array(
Expand All @@ -67,28 +68,12 @@ below renders the ``index.html.php`` template::
// src/Controller/HelloController.php

// ...
public function indexAction($name)
public function index($name)
{
return $this->render(
'AppBundle:Hello:index.html.php',
array('name' => $name)
);
}

You can also use the `@Template`_ shortcut to render the default
``AppBundle:Hello:index.html.php`` template::

// src/Controller/HelloController.php
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

// ...

/**
* @Template(engine="php")
*/
public function indexAction($name)
{
return array('name' => $name);
// template is stored in src/Resources/views/hello/index.html.php
return $this->render('hello/index.html.php', array(
'name' => $name
));
}

.. caution::
Expand All @@ -98,24 +83,24 @@ You can also use the `@Template`_ shortcut to render the default
the ``@`` notation for Twig namespaces will no longer be supported for the
``render()`` method::

public function indexAction()
public function index()
{
// ...

// namespaced templates will no longer work in controllers
$this->render('@App/Default/index.html.twig');
$this->render('@SomeNamespace/hello/index.html.twig');

// you must use the traditional template notation
$this->render('AppBundle:Default:index.html.twig');
$this->render('hello/index.html.twig');
}

.. code-block:: twig

{# inside a Twig template, namespaced templates work as expected #}
{{ include('@App/Default/index.html.twig') }}
{{ include('@SomeNamespace/hello/index.html.twig') }}

{# traditional template notation will also work #}
{{ include('AppBundle:Default:index.html.twig') }}
{{ include('hello/index.html.twig') }}


.. index::
Expand All @@ -134,36 +119,29 @@ the ``extend()`` call:

.. code-block:: html+php

<!-- templates/Hello/index.html.php -->
<?php $view->extend('AppBundle::layout.html.php') ?>
<!-- src/Resources/views/hello/index.html.php -->
<?php $view->extend('layout.html.php') ?>

Hello <?php echo $name ?>!

The ``AppBundle::layout.html.php`` notation sounds familiar, doesn't it? It
is the same notation used to reference a template. The ``::`` part simply
means that the controller element is empty, so the corresponding file is
directly stored under ``views/``.

Now, have a look at the ``layout.html.php`` file:

.. code-block:: html+php

<!-- templates/layout.html.php -->
<?php $view->extend('::base.html.php') ?>
<!-- src/Resources/views/layout.html.php -->
<?php $view->extend('base.html.php') ?>

<h1>Hello Application</h1>

<?php $view['slots']->output('_content') ?>

The layout is itself decorated by another one (``::base.html.php``). Symfony
The layout is itself decorated by another one (``base.html.php``). Symfony
supports multiple decoration levels: a layout can itself be decorated by
another one. When the bundle part of the template name is empty, views are
looked for in the ``templates/`` directory. This directory stores
global views for your entire project:
another one:

.. code-block:: html+php

<!-- templates/base.html.php -->
<!-- src/Resources/views/base.html.php -->
<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -196,8 +174,8 @@ decorating the template. In the ``index.html.php`` template, define a

.. code-block:: html+php

<!-- templates/Hello/index.html.php -->
<?php $view->extend('AppBundle::layout.html.php') ?>
<!-- src/Resources/views/hello/index.html.php -->
<?php $view->extend('layout.html.php') ?>

<?php $view['slots']->set('title', 'Hello World Application') ?>

Expand All @@ -207,7 +185,7 @@ The base layout already has the code to output the title in the header:

.. code-block:: html+php

<!-- templates/base.html.php -->
<!-- src/Resources/views/base.html.php -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php $view['slots']->output('title', 'Hello Application') ?></title>
Expand Down Expand Up @@ -238,17 +216,17 @@ Create a ``hello.html.php`` template:

.. code-block:: html+php

<!-- templates/Hello/hello.html.php -->
<!-- src/Resources/views/hello/hello.html.php -->
Hello <?php echo $name ?>!

And change the ``index.html.php`` template to include it:

.. code-block:: html+php

<!-- templates/Hello/index.html.php -->
<?php $view->extend('AppBundle::layout.html.php') ?>
<!-- src/Resources/views/hello/index.html.php -->
<?php $view->extend('layout.html.php') ?>

<?php echo $view->render('AppBundle:Hello:hello.html.php', array('name' => $name)) ?>
<?php echo $view->render('hello/hello.html.php', array('name' => $name)) ?>

The ``render()`` method evaluates and returns the content of another template
(this is the exact same method as the one used in the controller).
Expand All @@ -268,35 +246,17 @@ If you create a ``fancy`` action, and want to include it into the

.. code-block:: html+php

<!-- templates/Hello/index.html.php -->
<!-- src/Resources/views/hello/index.html.php -->
<?php echo $view['actions']->render(
new \Symfony\Component\HttpKernel\Controller\ControllerReference('AppBundle:Hello:fancy', array(
'name' => $name,
'color' => 'green',
))
new \Symfony\Component\HttpKernel\Controller\ControllerReference(
'App\Controller\HelloController::fancy',
array(
'name' => $name,
'color' => 'green',
)
)
) ?>

Here, the ``AppBundle:Hello:fancy`` string refers to the ``fancy`` action of the
``Hello`` controller::

// src/Controller/HelloController.php

class HelloController extends Controller
{
public function fancyAction($name, $color)
{
// create some object, based on the $color variable
$object = ...;

return $this->render('AppBundle:Hello:fancy.html.php', array(
'name' => $name,
'object' => $object
));
}

// ...
}

But where is the ``$view['actions']`` array element defined? Like
``$view['slots']``, it's called a template helper, and the next section tells
you more about those.
Expand Down Expand Up @@ -332,10 +292,10 @@ pattern:

.. code-block:: yaml

# src/Resources/config/routing.yml
hello: # The route name
path: /hello/{name}
defaults: { _controller: AppBundle:Hello:index }
# config/routes.yaml
hello:
path: /hello/{name}
controller: App\Controller\HelloController::index

Using Assets: Images, JavaScripts and Stylesheets
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
4 changes: 2 additions & 2 deletions templating/debug.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
How to Dump Debug Information in Twig Templates
===============================================

When using PHP, you can use the
When using PHP templates, you can use the
:ref:`dump() function from the VarDumper component <components-var-dumper-dump>`
if you need to quickly find the value of a variable passed. This is useful,
for example, inside your controller::
Expand All @@ -19,7 +19,7 @@ for example, inside your controller::

class ArticleController extends Controller
{
public function recentListAction()
public function recentList()
{
$articles = ...;
dump($articles);
Expand Down
6 changes: 3 additions & 3 deletions templating/embedding_controllers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ articles::

class ArticleController extends Controller
{
public function recentArticlesAction($max = 3)
public function recentArticles($max = 3)
{
// make a database call or other logic
// to get the "$max" most recent articles
Expand Down Expand Up @@ -68,7 +68,7 @@ The ``recent_list`` template is perfectly straightforward:
you'll learn how to do this correctly.

To include the controller, you'll need to refer to it using the standard
string syntax for controllers (i.e. **controllerPath**::**action**):
string syntax for controllers (i.e. **controllerNamespace**::**action**):

.. configuration-block::

Expand Down Expand Up @@ -98,4 +98,4 @@ string syntax for controllers (i.e. **controllerPath**::**action**):
) ?>
</div>

The result of an embedded controler can also be :doc:`cached </http_cache/esi>`
The result of an embedded controller can also be :doc:`cached </http_cache/esi>`
4 changes: 2 additions & 2 deletions templating/formats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pattern is to do the following::
/**
* @Route("/{slug}")
*/
public function showAction(Request $request, $slug)
public function show(Request $request, $slug)
{
// retrieve the article based on $slug
$article = ...;
Expand All @@ -53,7 +53,7 @@ special ``_format`` placeholder in your route definition::
/**
* @Route("/{slug}.{_format}", defaults={"_format": "html"})
*/
public function showAction(Request $request, $slug)
public function show(Request $request, $slug)
{
// ...
}
Expand Down
Loading