Skip to content

Commit

Permalink
Update tests and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Cript committed Feb 9, 2015
1 parent 560326e commit 229f4b3
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Resources/doc/2-the-view-layer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,63 @@ which adds several convenience methods:
}
}
If you need to pass more data in template, not for serialization, you can use ``setTemplateData`` method:

.. code-block:: php
<?php
use FOS\RestBundle\Controller\FOSRestController;
class UsersController extends FOSRestController
{
public function getCategoryAction($categorySlug)
{
$category = $this->get('category_manager')->getBySlug($categorySlug);
$products = ...; // get data, in this case list of products.
$templateData = array('category' => $category);
$view = $this->view($products, 200)
->setTemplate("MyBundle:Category:show.html.twig")
->setTemplateVar('products')
->setTemplateData($templateData);
;
return $this->handleView($view);
}
}
or it is possible to use lazy-loading:

.. code-block:: php
<?php
use FOS\RestBundle\Controller\FOSRestController;
class UsersController extends FOSRestController
{
public function getProductsAction($categorySlug)
{
$products = ...; // get data, in this case list of products.
$categoryManager = $this->get('category_manager');
$view = $this->view($products, 200)
->setTemplate("MyBundle:Category:show.html.twig")
->setTemplateVar('products')
->setTemplateData(function(ViewHandlerInterface $viewHandler, ViewInterface $view) use ($categoryManager, $categorySlug) {
$category = $categoryManager->getBySlug($categorySlug);
return array(
'category' => $category
)
});
;
return $this->handleView($view);
}
}
To simplify this even more: If you rely on the ``ViewResponseListener`` in
combination with SensioFrameworkExtraBundle you can even omit the calls to
``$this->handleView($view)`` and directly return the view objects. See chapter
Expand All @@ -75,6 +132,7 @@ There are several more methods on the ``View`` class, here is a list of all
the important ones for configuring the view:

* ``setData($data)`` - Set the object graph or list of objects to serialize.
* ``setTemplateData($templateData)`` - Set the template data array or anonymous function. Closure should return array.
* ``setHeader($name, $value)`` - Set a header to put on the HTTP response.
* ``setHeaders(array $headers)`` - Set multiple headers to put on the HTTP response.
* ``setSerializationContext($context)`` - Set the serialization context to use.
Expand Down
19 changes: 19 additions & 0 deletions Tests/View/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ public static function setDataDataProvider()
);
}

/**
* @dataProvider setTemplateDataDataProvider
*/
public function testSetTemplateData($templateData)
{
$view = new View();
$view->setTemplateData($templateData);
$this->assertEquals($templateData, $view->getTemplateData());
}

public static function setTemplateDataDataProvider()
{
return array(
'null as data' => array(null),
'array as data' => array(array('foo' => 'bar')),
'function as data' => array(function() {})
);
}

public function testSetEngine()
{
$view = new View();
Expand Down

0 comments on commit 229f4b3

Please sign in to comment.