ExpressionEngine: Houdini - making templates magical.
The basic idea behind our magic trick is getting the content in one place in a template into other templates or potentially some place else in the same template. How do we go about pulling off this feat? It is actually pretty simple. We will create an EE plugin that uses a static class variable to store data in so that it can be accessed across all instances of the plugin class. Wait… hold up… what? Static, class, instance? Don't worry we will explain this in a little bit and if you already understand them great.
First though let's look at the EE template code to see what the usage of our plugin is going to look like.
What if we wanted to display an entry's title in the <title>, but it exists in the .header embed. No problem. In the main template or other template that gets parsed before the .header embed (this is important as parse order has an effect on where you can set and get data with this approach):
And in the .header embed.
Or maybe, wouldn't it be nice if we did not have to have a second entries loop in the sidebar just to get the categories listed again for this entry? Oh, wait we don't!
And then in the .sidebar embed.
Pretty cool - eh? So, on to the PHP and static stuff.
To make a variable in a PHP class static all you need to do is use the “static" keyword.
What does it mean though? Basically, the value of the static variable is shared between all the instances of a class and not associated with only an individual instance. So, what is an instance? If a PHP class is like a blueprint of a house then an instance is an actual house that was built using that blueprint.
To create an instance of a class you use the keyword “new" followed by the class name.
This comes in handy when you know that in ExpressionEngine each time the template parser comes across one of our plugin tags it instantiates a new instance of our class. Since we need to share the data we capture with the exp:houdini:set and exp:houdini:store tags with the exp:houdini:get and exp:houdini:fetch tags we can use the awesome static variable to pass data between the different instances of our plugin class. This also relies on the fact the all embeds in the main template are parsed after the entire main template is parsed. This lets us capture data anywhere in the main template and have access to it in any embed in the main template.
Below is the static class variable used to store the captured data between instances of the plugin class.
We could do essentially the same thing with the $this->EE->session->cache, but this is not the ideal method to use. It relies on a global array where you can run into situations of naming collisions and other add-on code destroying your data. With the static class variable your data is scoped to your class and you have complete control over it. You can even set the access to protected and then no code outside of the code in your plugin will have any access to it.
For support and issues visit.