I will answer all questions one email at a time.
On 10/27/10 12:23 AM, Lukas Kahwe Smith wrote:
Hi,
I think the _format routing magic needs a bit of tweaking to be really useful.
Summary:
1) Make it possible to load index_content.twig (rather than
index_content.json.twig) during a .json request when using the _format route
magic with the following syntax:
{% include 'FooBundle:Default:index_content.twig' with ['_format': 'html'] %}
This is already possible:
{% include 'FooBundle:Default:index_content.html.twig' %}
To be fair, it was possible for all formats but HTML due to a bug I have
just fixed (http://github.com/fabpot/symfony/commit/314507).
2) Make it possible to define some format specific view logic (json_encode(),
generate pdf/image etc)
3) Also make sure that there is an automatic fallback in case the format
specific template is missing. This is important since for example there is no
form.json.twig/widget.json.twig bundled with Symfony2 and without them an error
is raised when Twig loads the form extension when _format is something else
than html.
Details for 1) and 2):
foo:
pattern: /foo.:_format
defaults: { _controller: fooDefault:indexAction, _format: html }
With this route if a controller calls the following:
return $this->render('FooBundle:Default:index.twig');
Then if the user calls "foo.html" then Symfony2 will look for an index.twig
template like the following:
{% extends "FooBundle::layout.twig" %}
{% block content %}
{% include "FooBundle:Default:index_content.twig" %}
{% endblock content %}
However if the user calls "foo.json" then Symfony2 will look for an
index.json.twig template and it will also automatically set the content type to
application/json. But here we have the problem 1) and 2):
{% extends "FooBundle::layout.php" %}
{% block content %}
{# Note that since php templates to not have access to twig blocks we have to
capture this as a slot #}
{{ _view.slots.start('content') }}
{% include 'FooBundle:Default:index_content.twig' %}
{{ _view.slots.stop() }}
{% endblock content %}
Neither can I load index_content.twig (as twig will automatically load
index_content.json.twig) nor can I extend a twig template with a PHP template.
The first I need to be able to get the actual HTML content I am trying to
return in the JSON response and the second is necessary so that I can call
json_encode()
Example layout.json.php:
<?php
echo json_encode(
array(
'html' => $view['slots']->output('content'),
'status' => 'success',
)
);
Obviously for 2) I could add some twig helper or special token so that I can also do
json_encode() inside twig, but it would be nice to be able to fallback to php in case its
necessary rather than having to bloat twig (especially when doing more complex things
like generating a PDF or image). Not sure if it would be wise to add support for PHP
sections inside a twig template as an alternative. A third alternative is to put this
json_encode() somewhere into the template engine maybe via some callback when using
something else than 'html' as the _format. Yet another approach is going with the Agavi
approach of having a "real" view layer inbetween the controller and the
templates.
For 1) I propose that in case a '_format' parameter is set in the include that
it overrides the current magic _format handling for finding a template
{% include 'FooBundle:Default:index_content.twig' with ['_format': 'html'] %}
regards,
Lukas Kahwe Smith
[email protected]
--
If you want to report a vulnerability issue on symfony, please send it to
security at symfony-project.com
You received this message because you are subscribed to the Google
Groups "symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-devs?hl=en