John, let me give it another go!
Googl analytics is still my best example.
I want every single page to have the basic analytics snippet.
I really don't want to have this in every single view- it'd be a big
job to go through all the existing views and add the call to render
the element (not very DRY), it makes my views more cluttered, and it
introduces the risk that pages are deployed without the snippet,
placing the burden on the developers to know about these snippets that
have to be placed in every view they create!
However, some pages need to completely override the default version in
the layout.
For example, analytics doesn't auto track 40x/50x HTTP responses, so I
want to change the analytics code for specific views.
So right now, my layout renders the analytics element, passing it a
variable form viewVars if necessary.
Then, my error page for 404s etc sets this variable to a custom value
("error-404") so we can track how often our users hit error pages.
This technique is DRY (no unnecessary repetition of the same snippet),
employs convention over config (the convention is analytics is handled
in the layout, unless you're dealing with a special case), and there's
no risk of analytics not being included on a page.
None of the suggestions so far seem as elegant as this...?
On Apr 8, 11:34 am, John Andersen <[email protected]> wrote:
> I am still confused about why you want to have something in the
> layout, that changes depending on the view!
> If the view knows what to present, why does it have to inform layout
> about it! The view just invokes the correct element to present.
> It sounds to me that you are making it too complicated - try to
> simplify it!
>
> Mayby you can explain more about why you have made the decision to
> implement it that way, maybe with an example and code?
> Enjoy,
> John
>
> On Apr 8, 11:47 am, the_woodsman <[email protected]> wrote:
>
> > Thanks for all your suggestions everyone.
>
> > So if Cake doesn't have a cleaner way to do this, how would I make one
> > myself?
>
> > The ideal would be to mimic the way vars are passed into views from
> > controllers, i.e in the view, setForLayout('varName', $varValue), and
> > then $varName just appears in the layout.
> > If i had ot use a method in the layout, like $varName = fromView
> > ('varName'), that wouldn't be too bad either.
>
> > But how/where would this best be done, without hacking the core?
>
> > On Apr 8, 12:49 am, brian <[email protected]> wrote:
>
> > > On Tue, Apr 7, 2009 at 4:07 PM, the_woodsman <[email protected]>
> > > wrote:
>
> > > > Thanks Brian - but I don't understand how this addresses the issue...
>
> > > > Are you saying I can render the element twice, without bothering with
> > > > an if statement, and it will only get echo'd once?
>
> > > Sorry, I wrote it twice to point out that it can be used either way.
> > > Not both at once ;-)
>
> > > > And the real question - how do I get the value 'foo' into the layout
> > > > from the view!? $this->viewVars is how I'm doing it currently...
>
> > > Right, I was thinking that the element would be included in the
> > > layout, not the view. But, of course, passing the var to the element
> > > will only work if called in the view.
>
> > > Looks like $viewVars is the way to proceed.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" 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/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---