Andrew Wigglesworth

- my personal website

Path: Home / Blog / Persistent variables in eZ Publish

Persistent variables in eZ Publish

Added on: January 13, 2011 in software | howto | web |

Variables called in the eZ Publish content module do not survive outside the module and cannot be used in the page layout templates … except, that is, in the almost mythical form of the "persistent variable".

On using eZ Publish 4.4

I haven't gone into how eZ Publish uses templates or the override system. There's plenty of documentation at http://doc.ez.no covering all those things.

Why "almost mythical"?

eZ Publish has lots of documentation, not always particularly descriptive, but it's there. Information about persistent variables is frustratingly gnomic. Actually, it doesn't really even get as far as being gnomic. A lot of experimentation led to the following understanding of persistent variables.

These are instructions for using a persistent variable to customise the description meta tag in the html header. It's a simple thing to do, and when you see how to do this then with further understanding of eZ Publish you'll see how useful persistent variables are.

So, what does this all mean? Well, what we'll be doing is setting a persistent variable in the article template (article.tpl) and then calling it eventually into pagepagelayout.tpl. The contents of the variable will be a text box attribute (not an XML box attribute) added to the article class.

I'll use the identifier "metadescription" for the new attribute. I also set it as a required attribute so that I didn't forget to fill it out.

In article.tpl place the following:

{set scope=global persistent_variable=hash('meta_description',
                             $node.object.data_map.meta_description.content)}

This will set a persistent variable called (not surprisingly) metadescription.

Visit the pagehead.tpl file and at least do the following; add the following line after the foreach loops that create meta tags from the information entered into the site design page "look and feel" settings:

<meta name="description"
content="{$module_result.content_info.persistent_variable.description}. 
Some default text for pages that have no persistent meta_description variable" />

Of course you'll have to delete the description meta tag that is in the "look and feel" settings or you'll end up with two of them.

You can go further (as I did) and remove the standard meta tag code and write your own routines utilising persistent variables, and other template functions to create whatever meta tags you wish … and not just meta tags of course.

A word on setting the persistent variables. I found that they all had to be set at once, ie in a single expression. The following is the syntax.

{set scope=global persistent_variable=hash('variable_name1', $variable_content1,
'variable_name2', $variable_content2,
'variable_name3', $variable_content3)}

You can set more than three of course.

Andrew Wigglesworth

Musician, web mangler and GNU/Linux user.

I use Jabber for XMPP instant messaging.
andrew-wigglesworth@jabber.org

I use the nick ecadre on the Freenode IRC network.

There are all sorts of bit and bobs here, some computer stuff, politics, a journal, music recordings and a gig bookings list.