Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
View Object
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Daniel Ragtek  
View profile  
 More options Oct 12 2012, 4:17 am
From: Daniel Ragtek <daniel.fat...@gmail.com>
Date: Fri, 12 Oct 2012 01:17:14 -0700 (PDT)
Local: Fri, Oct 12 2012 4:17 am
Subject: View Object

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

(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?

thx


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gergely Polonkai  
View profile  
 More options Oct 12 2012, 5:51 am
From: Gergely Polonkai <pol...@w00d5t0ck.info>
Date: Fri, 12 Oct 2012 11:51:49 +0200
Local: Fri, Oct 12 2012 5:51 am
Subject: Re: [Symfony2] View Object

I'm currently not at my machine, so maybe I mistype something, but
basically you should do this:

Route_name:
    pattern: /whatever.{_format}
    defaults:
        _controller: AcmeDemoBundle:test
        _format: html
    requirements:
        _format: (html|json)

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:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Ragtek  
View profile  
 More options Oct 12 2012, 6:13 am
From: Daniel Ragtek <daniel.fat...@gmail.com>
Date: Fri, 12 Oct 2012 03:13:57 -0700 (PDT)
Local: Fri, Oct 12 2012 6:13 am
Subject: Re: [Symfony2] View Object

Thx, but that's only for the /whatever route,right?
Am i able to set this global for all the routes?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gergely Polonkai  
View profile  
 More options Oct 12 2012, 6:21 am
From: Gergely Polonkai <pol...@w00d5t0ck.info>
Date: Fri, 12 Oct 2012 12:21:10 +0200
Local: Fri, Oct 12 2012 6:21 am
Subject: Re: [Symfony2] View Object

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:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Ragtek  
View profile  
 More options Oct 12 2012, 6:37 am
From: Daniel Ragtek <daniel.fat...@gmail.com>
Date: Fri, 12 Oct 2012 03:37:42 -0700 (PDT)
Local: Fri, Oct 12 2012 6:37 am
Subject: Re: [Symfony2] View Object

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){

return jsonTemplate

}

else {
return htmlTemplate


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gergely Polonkai  
View profile  
 More options Oct 12 2012, 6:48 am
From: Gergely Polonkai <pol...@w00d5t0ck.info>
Date: Fri, 12 Oct 2012 12:48:35 +0200
Local: Fri, Oct 12 2012 6:48 am
Subject: Re: [Symfony2] View Object

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:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Ragtek  
View profile  
 More options Oct 13 2012, 12:10 pm
From: Daniel Ragtek <daniel.fat...@gmail.com>
Date: Sat, 13 Oct 2012 09:10:53 -0700 (PDT)
Local: Sat, Oct 13 2012 12:10 pm
Subject: Re: View Object

Anybody having experiance with
http://symfony.com/doc/2.0/book/internals.html#kernel-request-event ?

kernel.view Event¶<http://symfony.com/doc/2.0/book/internals.html#kernel-view-event>

*Event Class*: GetResponseForControllerResultEvent<http://api.symfony.com/2.0/Symfony/Component/HttpKernel/Event/GetResp...>

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:)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adrian Schneider  
View profile  
 More options Oct 13 2012, 2:20 pm
From: Adrian Schneider <siradr...@gmail.com>
Date: Sat, 13 Oct 2012 11:20:46 -0700 (PDT)
Local: Sat, Oct 13 2012 2:20 pm
Subject: Re: View Object

Hey Daniel,

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:

https://github.com/Courtyard/ForumBundle/blob/master/EventListener/Co...

Hope this helps!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »