1. container /base with the header, navigation,footer, sidebars, etc.. 2. content template
now, if i request : www.example.com/userinfo/1 it should return the normal html response if i request www.example.com/userinfo/1.json it should return the content template, without including the container/base, but adding additional infos to the json response
Anybody have done something similar with symfony2? What's the best way to do this? My first idea was to create a second container/base template for json responses and then in the content templates to do something like
(pseudocode:)
if (isXmlHttpRequest()){ {{ extends base.html.twig}} {else} {{ extends base.json.twig }}
would this be a good practice or is there another, better way for this?
This way if you call for whatever or whatever.html, you will use
whatever.html.twig, and if you call for whatever.json, you will use
whatever.json.twig. You must pass the $_format parameter to your controller
action, unless you use the @Route and @Template annotations.
Best,
Gergely
On Oct 12, 2012 10:17 AM, "Daniel Ragtek" <daniel.fat...@gmail.com> wrote:
> Is there another (cleaner) way to change the response depending on the
> request?
> I've found this codesnippet
> http://stackoverflow.com/questions/12364855/how-to-render-a-view-file... which
> would cause IMO too much ugly conditions in the controller actions and many
> extra templates
> What i want to do is to have only 2 templates
> 1. container /base with the header, navigation,footer, sidebars, etc..
> 2. content template
> now, if i request : www.example.com/userinfo/1 it should return the
> normal html response
> if i request www.example.com/userinfo/1.json it should return the content
> template, without including the container/base, but adding additional infos
> to the json response
> Anybody have done something similar with symfony2?
> What's the best way to do this? My first idea was to create a second
> container/base template for json responses and then in the content
> templates to do something like
> would this be a good practice or is there another, better way for this?
> thx
> --
> 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 "Symfony2" group.
> To post to this group, send email to symfony2@googlegroups.com
> To unsubscribe from this group, send email to
> symfony2+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/symfony2?hl=en
> This way if you call for whatever or whatever.html, you will use > whatever.html.twig, and if you call for whatever.json, you will use > whatever.json.twig. You must pass the $_format parameter to your controller > action, unless you use the @Route and @Template annotations.
> Best, > Gergely > On Oct 12, 2012 10:17 AM, "Daniel Ragtek" <daniel...@gmail.com<javascript:>> > wrote:
>> Is there another (cleaner) way to change the response depending on the >> request? >> I've found this codesnippet >> http://stackoverflow.com/questions/12364855/how-to-render-a-view-file... which >> would cause IMO too much ugly conditions in the controller actions and many >> extra templates
>> What i want to do is to have only 2 templates
>> 1. container /base with the header, navigation,footer, sidebars, etc.. >> 2. content template
>> now, if i request : www.example.com/userinfo/1 it should return the >> normal html response >> if i request www.example.com/userinfo/1.json it should return the >> content template, without including the container/base, but adding >> additional infos to the json response
>> Anybody have done something similar with symfony2? >> What's the best way to do this? My first idea was to create a second >> container/base template for json responses and then in the content >> templates to do something like
>> would this be a good practice or is there another, better way for this?
>> thx
>> -- >> 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 "Symfony2" group. >> To post to this group, send email to symf...@googlegroups.com<javascript:> >> To unsubscribe from this group, send email to >> symfony2+u...@googlegroups.com <javascript:> >> For more options, visit this group at >> http://groups.google.com/group/symfony2?hl=en
Yes, it is. I don't know of any method to set it globally, but that doesn't
mean it's impossible ;)
On Oct 12, 2012 12:13 PM, "Daniel Ragtek" <daniel.fat...@gmail.com> wrote:
>> This way if you call for whatever or whatever.html, you will use
>> whatever.html.twig, and if you call for whatever.json, you will use
>> whatever.json.twig. You must pass the $_format parameter to your controller
>> action, unless you use the @Route and @Template annotations.
>> Best,
>> Gergely
>> On Oct 12, 2012 10:17 AM, "Daniel Ragtek" <daniel...@gmail.com> wrote:
>>> Is there another (cleaner) way to change the response depending on the
>>> request?
>>> I've found this codesnippet http://**stackoverflow.com/questions/**
>>> 12364855/how-to-render-a-view-**file-as-json-object-in-**
>>> symfony2-for-ajax-requests<http://stackoverflow.com/questions/12364855/how-to-render-a-view-file...>
>>> **which would cause IMO too much ugly conditions in the controller
>>> actions and many extra templates
>>> What i want to do is to have only 2 templates
>>> 1. container /base with the header, navigation,footer, sidebars, etc..
>>> 2. content template
>>> Anybody have done something similar with symfony2?
>>> What's the best way to do this? My first idea was to create a second
>>> container/base template for json responses and then in the content
>>> templates to do something like
>>> would this be a good practice or is there another, better way for this?
>>> thx
>>> --
>>> 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 "Symfony2" group.
>>> To post to this group, send email to symf...@googlegroups.com
>>> To unsubscribe from this group, send email to
>>> symfony2+u...@**googlegroups.com
>>> For more options, visit this group at
>>> http://groups.google.com/**group/symfony2?hl=en<http://groups.google.com/group/symfony2?hl=en>
>> --
> 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 "Symfony2" group.
> To post to this group, send email to symfony2@googlegroups.com
> To unsubscribe from this group, send email to
> symfony2+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/symfony2?hl=en
thx. then i'll search the documentation for this^^
Just once again to explain what i really want:
i want to be able to make requests via ajax to the same page url (e.g. blog/1) if i make a normal requests i'm getting the base,html.twig template which includes post.html.twig if i add &getJsonTemplate=1 to the url via my ajax request, i want to get a json response, which is including some own variables + the rendered post.html.twig template which will be inserted into the dom via my javascript logic (e.g. as overlay, or replacement of the content)
and i want to avoid to have too many templates and i want to avoid to have in each method: if ( getJsonTemplate){
} On Friday, October 12, 2012 12:21:14 PM UTC+2, Gergely Polonkai wrote:
> Yes, it is. I don't know of any method to set it globally, but that > doesn't mean it's impossible ;) > On Oct 12, 2012 12:13 PM, "Daniel Ragtek" <daniel...@gmail.com<javascript:>> > wrote:
>> Thx, but that's only for the /whatever route,right? >> Am i able to set this global for all the routes?
>> On Friday, October 12, 2012 11:51:57 AM UTC+2, Gergely Polonkai wrote:
>>> I'm currently not at my machine, so maybe I mistype something, but >>> basically you should do this:
>>> This way if you call for whatever or whatever.html, you will use >>> whatever.html.twig, and if you call for whatever.json, you will use >>> whatever.json.twig. You must pass the $_format parameter to your controller >>> action, unless you use the @Route and @Template annotations.
>>> Best, >>> Gergely >>> On Oct 12, 2012 10:17 AM, "Daniel Ragtek" <daniel...@gmail.com> wrote:
>>>> Is there another (cleaner) way to change the response depending on the >>>> request? >>>> I've found this codesnippet http://**stackoverflow.com/questions/** >>>> 12364855/how-to-render-a-view-**file-as-json-object-in-** >>>> symfony2-for-ajax-requests<http://stackoverflow.com/questions/12364855/how-to-render-a-view-file...> >>>> **which would cause IMO too much ugly conditions in the controller >>>> actions and many extra templates
>>>> What i want to do is to have only 2 templates
>>>> 1. container /base with the header, navigation,footer, sidebars, etc.. >>>> 2. content template
>>>> Anybody have done something similar with symfony2? >>>> What's the best way to do this? My first idea was to create a second >>>> container/base template for json responses and then in the content >>>> templates to do something like
>>>> would this be a good practice or is there another, better way for this?
>>>> thx
>>>> -- >>>> 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 "Symfony2" group. >>>> To post to this group, send email to symf...@googlegroups.com >>>> To unsubscribe from this group, send email to >>>> symfony2+u...@**googlegroups.com >>>> For more options, visit this group at >>>> http://groups.google.com/**group/symfony2?hl=en<http://groups.google.com/group/symfony2?hl=en>
>>> -- >> 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 "Symfony2" group. >> To post to this group, send email to symf...@googlegroups.com<javascript:> >> To unsubscribe from this group, send email to >> symfony2+u...@googlegroups.com <javascript:> >> For more options, visit this group at >> http://groups.google.com/group/symfony2?hl=en
To add some finer details I missed in my previous letter, you should know
that the Content-Type of the response depends on the _format variable,
which has the value 'html' by default.
On Oct 12, 2012 12:37 PM, "Daniel Ragtek" <daniel.fat...@gmail.com> wrote:
> thx. then i'll search the documentation for this^^
> Just once again to explain what i really want:
> i want to be able to make requests via ajax to the same page url (e.g.
> blog/1)
> if i make a normal requests i'm getting the base,html.twig template which
> includes post.html.twig
> if i add &getJsonTemplate=1 to the url via my ajax request, i want to get
> a json response, which is including some own variables + the
> rendered post.html.twig template
> which will be inserted into the dom via my javascript logic (e.g. as
> overlay, or replacement of the content)
> and i want to avoid to have too many templates and i want to avoid to have
> in each method:
> if ( getJsonTemplate){
> On Friday, October 12, 2012 12:21:14 PM UTC+2, Gergely Polonkai wrote:
>> Yes, it is. I don't know of any method to set it globally, but that
>> doesn't mean it's impossible ;)
>> On Oct 12, 2012 12:13 PM, "Daniel Ragtek" <daniel...@gmail.com> wrote:
>>> Thx, but that's only for the /whatever route,right?
>>> Am i able to set this global for all the routes?
>>> On Friday, October 12, 2012 11:51:57 AM UTC+2, Gergely Polonkai wrote:
>>>> I'm currently not at my machine, so maybe I mistype something, but
>>>> basically you should do this:
>>>> This way if you call for whatever or whatever.html, you will use
>>>> whatever.html.twig, and if you call for whatever.json, you will use
>>>> whatever.json.twig. You must pass the $_format parameter to your controller
>>>> action, unless you use the @Route and @Template annotations.
>>>> Best,
>>>> Gergely
>>>> On Oct 12, 2012 10:17 AM, "Daniel Ragtek" <daniel...@gmail.com> wrote:
>>>>> Is there another (cleaner) way to change the response depending on the
>>>>> request?
>>>>> I've found this codesnippet http://**stackoverfl**ow.com/questions/**
>>>>> 12364855/how-**to-render-a-view-**file-as-json-**object-in-**
>>>>> symfony2-for-ajax-**requests<http://stackoverflow.com/questions/12364855/how-to-render-a-view-file...>
>>>>> **which would cause IMO too much ugly conditions in the controller
>>>>> actions and many extra templates
>>>>> What i want to do is to have only 2 templates
>>>>> 1. container /base with the header, navigation,footer, sidebars, etc..
>>>>> 2. content template
>>>>> Anybody have done something similar with symfony2?
>>>>> What's the best way to do this? My first idea was to create a second
>>>>> container/base template for json responses and then in the content
>>>>> templates to do something like
>>>>> would this be a good practice or is there another, better way for this?
>>>>> thx
>>>>> --
>>>>> 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 "Symfony2" group.
>>>>> To post to this group, send email to symf...@googlegroups.com
>>>>> To unsubscribe from this group, send email to
>>>>> symfony2+u...@**googlegroups.com
>>>>> For more options, visit this group at
>>>>> http://groups.google.com/**group**/symfony2?hl=en<http://groups.google.com/group/symfony2?hl=en>
>>>> --
>>> 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 "Symfony2" group.
>>> To post to this group, send email to symf...@googlegroups.com
>>> To unsubscribe from this group, send email to
>>> symfony2+u...@**googlegroups.com
>>> For more options, visit this group at
>>> http://groups.google.com/**group/symfony2?hl=en<http://groups.google.com/group/symfony2?hl=en>
>> --
> 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 "Symfony2" group.
> To post to this group, send email to symfony2@googlegroups.com
> To unsubscribe from this group, send email to
> symfony2+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/symfony2?hl=en
This event is not used by FrameworkBundle, but it can be used to implement a view sub-system. This event is called *only* if the Controller does *not* return a Response object. The purpose of the event is to allow some other return value to be converted into a Response.
This sounds "exactly" like i need it
i could check the request on this place.
if it's a normal request, it returns the normal request
if it's a "special request" wanting json as response, i could call my own services to get the additional values, create my json response and return it:)
anybody having experiance with this event? it seems really like a better solution to my problem:)
On Friday, October 12, 2012 10:17:14 AM UTC+2, Daniel Ragtek wrote:
> Is there another (cleaner) way to change the response depending on the > request?
> I've found this codesnippet > http://stackoverflow.com/questions/12364855/how-to-render-a-view-file... which > would cause IMO too much ugly conditions in the controller actions and many > extra templates
> What i want to do is to have only 2 templates
> 1. container /base with the header, navigation,footer, sidebars, etc.. > 2. content template
> now, if i request : www.example.com/userinfo/1 it should return the > normal html response
> if i request www.example.com/userinfo/1.json it should return the content > template, without including the container/base, but adding additional infos > to the json response
> Anybody have done something similar with symfony2?
> What's the best way to do this? My first idea was to create a second > container/base template for json responses and then in the content > templates to do something like
What you are saying is correct - you can convert your own custom responses (custom object, text, arrays, whatever) to a proper Symfony\Component\HttpFoundation\Response object with that event. Here's an example from my own work this past week:
> This event is not used by FrameworkBundle, but it can be used to > implement a view sub-system. This event is called *only* if the > Controller does *not* return a Response object. The purpose of the event > is to allow some other return value to be converted into a Response.
> This sounds "exactly" like i need it
> i could check the request on this place.
> if it's a normal request, it returns the normal request
> if it's a "special request" wanting json as response, i could call my own > services to get the additional values, create my json response and return > it:)
> anybody having experiance with this event? it seems really like a better > solution to my problem:)
> On Friday, October 12, 2012 10:17:14 AM UTC+2, Daniel Ragtek wrote:
>> Is there another (cleaner) way to change the response depending on the >> request?
>> I've found this codesnippet >> http://stackoverflow.com/questions/12364855/how-to-render-a-view-file... which >> would cause IMO too much ugly conditions in the controller actions and many >> extra templates
>> What i want to do is to have only 2 templates
>> 1. container /base with the header, navigation,footer, sidebars, etc.. >> 2. content template
>> now, if i request : www.example.com/userinfo/1 it should return the >> normal html response
>> if i request www.example.com/userinfo/1.json it should return the >> content template, without including the container/base, but adding >> additional infos to the json response
>> Anybody have done something similar with symfony2?
>> What's the best way to do this? My first idea was to create a second >> container/base template for json responses and then in the content >> templates to do something like