Main Menu

Development

ExpressionEngine: Houdini - making templates magical.

on November 03, 2015

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.


Git Repository/Source code

For support and issues visit.

Download »

Shopping for a new digital agency?

Contact Us

Strategy

How Nonprofits Can Better Engage Millennials

June 02, 2016 by Thomas Brewer

Development

ElasticSearch and Rails: Where did my records go?

February 09, 2016 by Thomas Brewer

Development

Do You Own Your Code?

March 01, 2017 by Thomas Brewer

x

Development

Application error messages, write good ones and make sure they are seen!

June 16, 2017 by Thomas Brewer

Strategy

Get Your YouTube Videos Seen With These 3 Search Optimization Tips

June 14, 2017 by Thomas Brewer

Strategy

Website Content Strategy and Generation Z

June 12, 2017 by Thomas Brewer

Strategy

The Importance of Having An SSL Certificate On Your Website

June 09, 2017 by Thomas Brewer

Strategy

Left+Right Builds Award Worthy Web Applications

June 08, 2017 by Monica Bohanon

Strategy

Google My Business for local SEO

June 06, 2017 by Thomas Brewer

Development

Do You Own Your Code?

March 01, 2017 by Thomas Brewer

News

Left+Right Hired by Fantasy Sports Startup

February 17, 2017 by Morgan Porter

News

Meet Our Newest Client: Asian & Pacific Islander American Scholarship Fund

December 07, 2016 by Morgan Porter

Strategy

Online Marketing Is A Lot Like Online Dating

September 08, 2016 by Terri Jones

News

Meet Our Newest Client: Rx Partnership

June 29, 2016 by Morgan Porter

Strategy

5 Reasons Your Content Should Live on Your Website

June 13, 2016 by Morgan Porter

News

Long Live RVA News

June 07, 2016 by Morgan Porter

Strategy

How Nonprofits Can Better Engage Millennials

June 02, 2016 by Thomas Brewer

Development

Why We Use Git

May 19, 2016 by Thomas Brewer

Development

ElasticSearch and Rails: Where did my records go?

February 09, 2016 by Thomas Brewer

Strategy

Why We Are Distributed

January 28, 2016 by Morgan Porter

Development

ExpressionEngine: Announcing Sherlock!

November 11, 2015 by Thomas Brewer

Development

ExpressionEngine: Houdini - making templates magical.

November 03, 2015 by Thomas Brewer

Development

Lazy Lion

October 29, 2015 by Thomas Brewer

Development

Rails ERD and Padrino

October 29, 2015 by Thomas Brewer

News

We're One of Richmond's Fastest Growing Companies

October 28, 2015

Strategy

The Importance of Responsive Design for Non-Profit Organizations

February 10, 2015

Contact Us