Forums / Developer / Add to persistent variable array

Add to persistent variable array

Author Message

paul bolger

Wednesday 17 November 2010 10:42:29 pm

I'm using the ezgmaplocation extension to add a map to a page, but ran into a problem trying to simultaneously use the jqueryui accordion - the accordion wouldn't work properly with the link to the Google javascript where it was, so I had to move it up into the page head.

I found that setting the Javacript link as the persistent variable (from the ezgmaplocation template) was pretty easy but that wiped out the other values in the persistent variable array. I tried using the merge function to add it to the array, but have had no luck getting it to work. Is this the right approach, or would I be better off trying something else?

Paul Bolger

Carlos Revillo

Thursday 18 November 2010 11:51:59 am

hi. don't know if i'm missing something, but i think you can take a look to the ezpagedata_set operator...

paul bolger

Sunday 21 November 2010 3:01:30 am

Thanks Carlos, That looks promising, but there doesn't seem to be any documentation. An example of usage would be appreciated.

Paul Bolger

Carlos Revillo

Sunday 21 November 2010 4:25:01 am

Example is simple.

Having a node/view/full.tpl template or an override of it, or even an error page, you can do in that template something like

{ezpagedata_set( 'var1', 89 )}

you can get that variable in pagelayout like

{def $pagedata = ezpagedata()}
Var 1 is: {$pagedata.persistent_variable.var1}

Gaetano Giunta

Sunday 21 November 2010 5:14:52 am

Carlos example is fine. Otoh using merge and plain arrays to add more stuff to the persistent variable from templates should be fine too.

@Paul: can you show the exact code that you have and does not work?

Principal Consultant International Business
Member of the Community Project Board

paul bolger

Sunday 21 November 2010 5:31:59 pm

@Carlos: Thanks, that did it. Here's what I did (for the benefit of others).

My problem was that the js - the one with the key in it - link in ezgmaplocation was causing problems with other js on the page. Manually moving it around confirmed that moving it into the page head fixes the problem. But of course changing a line in one of the pagelayout templates to suit an extension is not good practice.

Initially I tried using the merge array function (@Gaetano: this is what I was talking about)

http://doc.ez.no/doc/eZ-Publish/Technical-manual/4.x/Reference/Template-operators/Arrays/merge

with no luck.

So here's what worked:

I added this to the ezgmaplocation.tpl (override, of course)

{def $mapkey=ezini('SiteSettings','GMapsKey')}
{def $jslinkstart='<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key='}
{def $jslinkend='" type="text/javascript"></script>'}
{def $googlemapslink=concat($jslinkstart,$mapkey,$jslinkend)}
{ezpagedata_set('script_list', array( $googlemapslink ))}

Then I added this to the beginning of page_head_script.tpl

{if and( is_set( $pagedata.persistent_variable.script_list ), $pagedata.persistent_variable.script_list|count() )}
{foreach $pagedata.persistent_variable.script_list as $extra_script}
{$extra_script}
{/foreach}

There are two drawbacks I can see with this. One is that a future change to page_head_script.tpl could cause problems (ie the overridden version wouldn't reflect the changes). and two, if one were to use this method across a few extensions they would all become interdependent - breaking or removing one would break the rest.

See this page for a bit more info.

http://share.ez.no/forums/developer/smart-loading-of-javascript

Paul Bolger