02 - Chunking up the Main Template into a Collection of Embedded Templates
In this chapter we start what I’ve been calling the “chunking up” of the template, where we take chunks of the HTML code from the master template, save them off as embedded EE templates, and add the embed code back to the master EE template.
Why Embedded Templates?
For those new to ExpressionEngine and embedding templates, you might first want to peruse the help documentation on embedded templates (although that page doesn’t talk much about the benefits of embedding templates).
The overall goal of the chunking up process is to only store common/reusable elements in one place in EE - both for ease of constructing the site and ease of future changes or maintenance.
This way when you want to add a new section to your site and need to add it to the main navigation, you only need make that change in one EE template. Since that template is embedded into the rest of the site, your change is immediately reflected across the entire site. If you have experience with server includes, it’s a very similar design approach.
What to Embed?
Before delving into the code, though, we should step back and take a look at the current page and what we’re intending to do. Sometimes I can get carried away with the chunking up and end up with a site that’s more complex than it needs to be. The big question is what elements (both from a visual and coding perspective) would benefit from being embedded? Starting visually, the obvious candidates are anything that’s sure to be repeated as we move through the site:
- The company name / logo
- The main navigation
- The search box
- The footer
We can add the Latest News and Latest Products blocks to that list—as we can then promote them on multiple pages throughout the site. The Register Now box is interesting - but the way it’s positioned in the middle of other content will make it difficult to embed. Let’s just lose that box as there will be no need for it on this site.
Looking at the source code on the HTML, we’ll also grab the HTML header including meta tags, the link to the stylesheet, and page title. I know—the page title needs to be different with each page. In a future chapter I’ll show you how to keep the page title stored in the embedded template, but also have each page get a unique title.
Chunking Plan
Note: In the attached files for this chapter you’ll find a copy of the index HTML with comments added to denote where the code will be chunked up. If you open the file in a text or HTML editor and peruse the code, you’ll note the large amount of content that isn’t being pulled out and embedded. That’s the core content of the page and, in the next chapter of this series, will get pulled out of the template and stored in an EE weblog.
I’m sure different EE developers do this differently. I prefer to keep the skeleton of the page’s essential structure in the parent template. I don’t like having div tags getting opened in one template and closed in another as it has made it hard to track down mis-matches or validation errors.
Create a New Template Group
I like to keep all my embedded templates in their own group, so that my main groups hold the parent templates. Here are the steps:
- Enter the EE Control Panel.
- Click Templates tab.
- Click Create a New Template Group button on the upper right portion of the screen.
- Choose a name. I’ve called mine “embeds”.
- Click Submit.
On to the Chunking!
I like to do this step incrementally - so my basic workflow is:
- With the full index template open, copy the section of code I want to embed.
- Create a new template in my “embeds” group.
- Choose None- Create an Empty Template option, name the template, and click Submit.
- Click on the new template title to edit it.
- Paste in the section of code.
- Adjust any formatting.
- Click Update.
- Don’t leave this screen just yet - up in the title area just over the editable text area EE provides the name of the template along with the Template Group name - for example; “embeds/html_header”.
- Select and copy this to the clipboard.
- Return to the master site/index template in edit mode.
- Select the same portion of code that you copied earlier.
- Do a paste, so that chunk of code is replaced with the name and Template Group of your new embedded template - again the example “embeds/html_header”.
- Add the EE embedding code, so “embeds/html_header” becomes embed=“embeds/html_header”.
- Save the master template by clicking Update.
- Click View Rendered Template button to ensure that the master template remains unchanged visually. Sometimes I’ve grabbed one line of code too many or too little and this is where I catch those mistakes.
- Repeat the above process for each section of code in the chunking plan file.
After Chunking
You should have an “embeds” Template Group with the following templates:
- footer
- html_header
- latest_news
- latest_products
- logo_title
- main_nav
- search_section_intros
There are text versions of each of these templates in the attached files.
You should now also have a drastically simplified site/index template, with all the repeatable elements pulled out and embedded back in. Here’s what I have:
Template Name: site/index
{embed="embeds/html_header"}
<body>
<div id="content">
{embed="embeds/logo_title"}
{embed="embeds/main_nav"}
{embed="embeds/search_section_intros"}
<div id="maincontent">
<div id="right_side">
{embed="embeds/latest_news"}
{embed="embeds/latest_products"}
</div><!-- close right_side -->
<div id="left_side">
<h2 class="underline">Abous us</h2>
<img src="{site_url}/images/interface/img.jpg" alt="image" />
<p><strong>Lorem ipsum dolor</strong> sit amet, consectetuer adipiscing elit. Morbi elementum, ipsum nec auctor condimentum, magna odio aliquet elit, non bibendum lorem ligula at nisi. Proin sapien. In hac <a href="#" title="">habitasse platea dictumst</a>. Nunc non magna. Duis pharetra laoreet dolor. Mauris tristique interdum turpis. Praesent sed pede. Aenean porttitor ultrices tortor. Aenean a purus. Praesent eu sapien.</p>
<p>Nulla posuere, sapien sit amet euismod suscipit, magna diam sodales magna, nec semper eros tortor et nulla. Nunc nec mauris. Aenean tincidunt. Nam sem. Etiam quis justo. Donec pellentesque pellentesque tellus. <strong>Integer bibendum</strong> massa sed diam. In in elit. Ut blandit arcu sed ligula. Aenean interdum. Suspendisse adipiscing suscipit nisl. In non leo et justo posuere placerat. In tincidunt cursus turpis. Maecenas suscipit tempor ante. Vestibulum pretium aliquam metus.</p>
<blockquote><p class="mpnull">Sapien sit amet euismod suscipit, magna diam sodales magna, nec semper eros tortor et nulla. Nunc nec mauris. Aenean tincidunt. Nam sem. Etiam quis justo. Donec pellentesque pellentesque tellus. <strong>Integer bibendum</strong> massa sed diam. In in elit.</p></blockquote>
<p>Nulla posuere, sapien sit amet euismod suscipit, magna diam sodales magna, nec semper eros tortor et nulla. Nunc nec mauris. Aenean tincidunt. Nam sem. Etiam quis justo. Donec pellentesque pellentesque tellus. <strong>Integer bibendum</strong> massa sed diam. </p>
<div class="textbox">
<h2><a href="#" title="">Register now!</a></h2>
<p>Nulla posuere, sapien sit amet euismod suscipit, magna diam sodales magna, nec semper eros tortor et nulla. Nunc nec mauris.</p>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi <a href="#" title="">elementum</a>, ipsum nec auctor condimentum, magna odio aliquet elit, non bibendum lorem ligula at nisi. Proin sapien. In hac habitasse platea dictumst.</p>
</div>
<h2>Our Assets</h2>
<p>In hac habitasse platea dictumst. Nunc non magna. Duis pharetra laoreet dolor. Mauris tristique interdum turpis. Praesent sed pede. Aenean porttitor ultrices tortor. Aenean a purus. Praesent eu sapien.</p>
<p>In hac habitasse platea dictumst. Nunc non magna. Duis pharetra laoreet dolor.</p>
<ul>
<li>In hac <strong>habitasse</strong> platea dictumst</li>
<li>Lorem ipsum dolor <a href="#" title="">sit amet</a></li>
<li>habitasse platea dictumst</li>
<li>sapien sit amet euismod</li>
</ul>
</div><!-- close left side div -->
</div><!-- close main content div -->
{embed="embeds/footer"}
</div> <!-- close content div -->
</body>
</html>
The Results
Once you have all these template changes made and saved, you should be able to load the rendered version of the site/index template and have it appear exactly as it did at the end of Chapter 1.
Category Navigation





by David Siembab
Date: Sunday, November 30th, 2008
Comment: #1
nice thanks a lot man. I thought it would be simple. Thanks again