Building an ExpressionEngine Site - Chapter 02

Chunking up the main template into a a collection of embedded templates.

This is the second installment in a series covering one way to go about building a small business type website in ExpressionEngine.  In the first chapter we downloaded a free site template, uploaded the images to an EE site, modified the image paths in the CSS and HTML files, and finished by getting EE to serve up the static template.

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 docs - 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 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.  For now we’ll just leave that one be and come back to it later.

Looking at the source code on the HTML, we’ll also grab the HTML header including meta tags, the link to the styesheet, 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
So here then is a screenshot of the HTML, marked up to show our “chunking plan” visually, along with the name for each embedded template.  Click the smaller image for a large one (it’ll open in a new window) - just be forewarned it’s about 1MB:

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 a EE weblog.  Also note - 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 divs getting opened in one template in 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 the “Templates” tab
  • Click the “Create a New Template Group” button on the upper right portion of the screen
  • Choose a name.  I’ve called mine “building_series_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 “building_series_embeds” group.
  • Choose the empty template option, name it, 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; “building_series_embeds/html_header”.
  • Select and copy this to the clipboard.
  • Return to the parent template in the editing interface.
  • 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 “building_series_embeds/html_header”.
  • Add the EE embedding code, so “building_series_embeds/html_header” becomes {embed = “building_series_embeds/html_header"} (Note - remove the spaces around the equals sign, and ensure - if you copy and paste from this page - to replace the quote marks).
  • Save the parent template.
  • View the parent template to ensure that it 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 graphic.

The Results
OK - I’ve moved through and completed the above process for this template. Visually, the rendered page looks exactly the same.  Viewing the source code you’ll see that there are changes in the spacing and tabbing of the rendered code as I’ve tweaked each embedded template as I went along.  I also added in some HTML comments to help asscociate closing div tags with their opening partners. 

The most satisfaction from this chapter is looking at how the embedding process has really simplified the parent template.  Here is a text version of the template after completing this Chapter: chapter_2_index.txt.

Next Steps
In Chapter 3 we’ll at least begin the process of moving through the static content contained in our templates, implementing weblogs to store that content (so that it can be edited in a weblog entry rather than having to go into the template code), then edit each template to add the necessary EE code to pull the content back in from the new weblog entries.

On to Chapter 3.
Back to Chapter 2.

Learn ExpressionEngine® Fast

Visit Train-ee.com for the latest in ExpressionEngine training designed with one goal in mind - to get you up to speed on ExpressionEngine® as quickly as possible.

The latest Train-ee Products:

The latest from the Train-ee blog:

Comments

1
Birger Johan Nordølum
August 10, 2007

Is this the {embed=} feature with the Core version of EE? Or do I need to upgrade to a licence version?

2
(Author)
August 11, 2007

You can embed templates in EE Core.

3
Birger Johan Nordølum
August 11, 2007

I don’t get this to work. The template group is called site_embeds and I have template called test. The code I am running, is {embed = site_embeds/test}. I have inserted it into the index just for a test. But it will not show. I have tryed it with various groups and templates. No go what so ever.

Is there something you need to enable in the CP to make EE use embeds tags?

4
(Author)
August 11, 2007

There is no setting that need to be enabled.

Make sure you have quotes around the template group and template name, and no spaces around the equals sign - view my text file for formatting examples.

5
Simon Cox
August 11, 2007

Mike - another great tutorial. As before many thanks for doing this. As you pointed out while the EE help docs are very good at explaining what each function does, it is very hard to to get your head around the strategy of building your site in EE. I am sure most folk approach site building from a position of I need to do X, Y and Z rather than this system allows me to do A,B and C.

6
Simon Cox
August 11, 2007

Mike - I see the questions about embeds - why not add a screen shot of your template page with the embeds in them to compliment the final output image.

7
Birger Johan Nordølum
August 11, 2007

I did find out what the error was. Because I copied it from you, and the text in your article has formatted the first semi colon as a reversed one. {embed = “building_series_embeds/html_header"}

As you can see, “ and not a “ rasberry Problem solved.

8
Birger Johan Nordølum
August 11, 2007

Now the problem occured once more. With the correct semi colon :(

You can’t see the fix in the post above. The comment system format the two semi colons the same way.

Sorry for a double post.

9
Birger Johan Nordølum
August 11, 2007

You should add that you always need to copy the {assign_variable:my_weblog="default_site"}
{assign_variable:my_template_group="site"}

To every template you create. That was my problem.

10
(Author)
August 11, 2007

Birger -

Hmm..I’ll bet it’s the quotes that are the issue (and not semicolons as the embed calls dont use semi-colons at all).  Again please reference the linked text file for the actual formatting as it should be a cleaner source to copy/paste from.

There is no need to assign the default_site and my_template group variables at this stage of the game - nothing I’ve done so far in this tutorial requires those variables.

Those only get used—and even then only if you want to - when putting content into EE weblogs and using weblog:entries tags to pull the content back out.  I’ll cover that step in the next chapter of this series.

Simon—I thought the text version of the final parent template file was a better approach than a screen shot as it could be copied/pasted back into EE if need be.

11
Birger Johan Nordølum
August 11, 2007

This is the string:
{embed="site_embeds/html_header"}

This is the code:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>{exp:weblog:info weblog="{my_weblog}"}{blog_title}{/exp:weblog:info}</title>
<meta http-equiv="Content-Type" content="text/html; charset={charset}” />

<link rel=’stylesheet’ type=’text/css’ media=’all’ href=’{stylesheet={my_template_group}/site_css}’ />
<style type=’text/css’ media=’screen’>@import “{stylesheet={my_template_group}/site_css}”;</style>

<link rel="alternate" type="application/rss+xml" title="RSS" href="{path={my_template_group}/rss_2.0}" />
<link rel="alternate" type="application/atom+xml" title="Atom" href="{path={my_template_group}/atom}" />

</head>

When I view source with Firefox. All the above lines is added. But the EE tags is not outputting the values.

This is my site: http://mindtooth.net/

12
(Author)
August 11, 2007

OK - that code is going past where I’m at with this building series - as it’s building dynamic titles and links that I’ve not yet covered.

I’ll cover those items in the future installment, in the meantime I’d suggest posting in the EE forums.

13
Birger Johan Nordølum
August 11, 2007

Thank you :D Just posted on the EE forums. And I can’t wait untill you next part i the series.

14

August 13, 2007

Thank you for doing this incredibly informative series.
I look forward to the next episode.
Does anyone know of a workflow for chunking the css code when building a site template from scratch?
I’ve heard some people are separating their css by typography colors layout etc.
Again thanks for the ee tutorial.

15
Victor
August 14, 2007

Hey which program did you use to make that code ? seems very very useful and clean.

16

August 14, 2007

Jeremy - I think the structure of CSS files is a matter of personal prefs...mine seem to fall into the order of how I built the site..wink

Victor - which code are you talking about?

17
(Author)
August 14, 2007

Uhg.  Logged in as the wife again...rolleyes

18
Victor
August 14, 2007

I mean the picture you added (all those colors) that separate div’s

19
(Author)
August 14, 2007

Oh - well.  The source code is from the free template I’m using as the basis for the tutorials.  I did a view/source in Firefox and used SnagIt to get a full scrolling screen capture, then pulled it into Photoshop for the color highlighting and added text.

A bit labor intensive, but it seemed like the clearest way to communicate how the source code could be chunked up into separate EE templates.

20
Victor
August 14, 2007

ahh damn i thought there was a program that did this,
such a feature is super handy heh

21

August 14, 2007

Thanks alot this is exactly where I’m at with getting going on customizing. Following along is certainly going to be worthwhile for many a newbie, and experienced users alike.
Keep on, keepin’ on… wink

22
KJ
August 15, 2007

Great article, Mike. I would suggest taking this one step further…

In your example “building_series_embeds/html_header” is still accessible to the outside world by typing “www.domain.com/building_series_embeds/html_header” in a browser. Granted, the chances of someone visiting that template are slim, but I like to be safe. By adding a period (.) to the beginning of a template name, you make it impossible for someone on the outside to gain access to it (building_series_embeds/.html_header). To see what I’m talking about, follow this link http://www.chuise.com/static/ex-global-templates.gif.

An important thing to note is that you should not use this when naming a reusable resource that needs to be accessed by the browser, like stylesheets.

In the end, this isn’t protecting your markup or anything like that, what it’s really doing is preventing the accidental finding/viewing of a resource that you didn’t intend your users to see.

Hopefully someone finds that useful.

23
(Author)
August 15, 2007

Thanks KJ - I’m aware of the new ability to hide templates with 1.6 but as this is a tutorial for those just getting started with EE didn’t feel it was appropriate to cover that yet.

And having said that - I guess I just don’t see the harm in keeping those open, especially in this case where viewing the individual template might be handy.

24
Carlton Dickson
October 10, 2007

Enjoying these tutorials so far...by far the most useful one I have seen on the web so far.

The practical ones are the best...I decided to give myself a little challenge and use a different template.

Thanks again for these...I hope to reach the latest on by next week smile

25

October 21, 2007

on to chap. 3 after much chasing curly braces in chap. 1. finally got my pages rendering, and finished chap. 2.

Thanks, Michael.....

26
(Author)
October 22, 2007

Glad to hear it.

I have added the text version of the index template to this chapter - in hopes of a more “copyable” css link.

27
Rick Schellenberg
December 07, 2007

Great tutorials. Very helpful. Have done the first two and they have gone beautifully.

28

January 03, 2008

Great tutorials! I had a quick question about embedding the navigation links. How do you maintain the tabs so that the correct tab is “highlighted” or active for any given page?

29

January 03, 2008

I found the answer to this in a later tutorial. These tutorials are great. Thanks!

30
(Author)
January 15, 2008

FYI:  The Building an ExpressionEngine Site series is now available for purchase as an eBook on the newly-launched Train-ee.com.

31

February 05, 2008

I have done the first step of chunking. The styles have gone missing from the template after doing the html_header embed. My first thought it that I need to update the path to the CSS file, but that doesn’t seem to be working. I don’t see any other comments that point me to something that will help.

Any ideas?

32
(Author)
February 05, 2008

Charlie - it’d probably be easiest if you posted the link to your stylesheet here.

33

February 05, 2008

{embed = “TEST_embeds/html_header"}

<link rel="stylesheet" type="text/css" media="screen" href="{style sheet=site/TEST_css}” />

34

February 05, 2008

Sorry for the double…
I forgot to add, I added the spaces for formatting, just like you’ve done above.

35
(Author)
February 05, 2008

Hey Charlie -

The link looks ok.  Are you sure you have the css template set to the css template type?  It should have a different icon than the globe that indicates a web template.  You can also click “preferences” for the template group to see what template type it is.

36
(Author)
February 05, 2008

Wait..in..what you’ve posted, the embed call should be in the parent template, and the stylesheet link within html_header...is that how you have it?

37

February 05, 2008

Yes it is. I was justing posting all the relevant information. Sorry for not clarifying.

In TEST_html:
{embed = “TEST_embeds/html_header"}

In group TEST_embeds > html_header:
<link rel="stylesheet" type="text/css" media="screen" href="{style sheet=site/TEST_css}” />

Also, the link back to 2 at the bottom of chapter 3 doesn’t actually go to 2.

38
(Author)
February 05, 2008

OK - when you view the rendered page how does the link come through?  Can you copy/pasted it back into the browser to get the css template?

39

February 05, 2008

It comes through. It all the sudden started working after I did that. I have no idea why. Thanks for the help? Lol. I’m sure I’ll be back later, but just on a different chapter. Kudos so far.

40
Patrick Hollis
March 30, 2008

IMPORTANT—I see what some others mean...if you cut and paste, even after correcting for spaces, the embed command doesn’t seem to work.  If you type it out exactly, however, it does work.  I have no idea why, but I just spent half an hour figuring it out…

41

March 31, 2008

Regarding the screenshot of the HTML, marked up to show the “chunking plan”:

I want to make sure that my ‘chunks’ match up with yours—that I have the exact code needed in my embedded templates, but when I click on the larger version of the ‘chunking plan’ its still illegible. Is this illustration supposed to be legible? And/or, are there copies of the embeds around that I’m not seeing—like the index example you offer?

Thanks.

42

March 31, 2008

Please ignore my above comment. The problem was the way I was enlarging the image. I can see it now.

Thanks for this excellent tutorial!

43
Danna McCormick
April 08, 2008

Hi Mike,
Newbie here… I’m taking the EE plunge, and have followed your tutorials/book with great success - many thanks.  Question on the chunking… your template ends one chunk before beginning another. 

I’ve structured this site with a wrapper div, a simple table for the main content area and I’ve even used a nested table (horrors!) for the navigation bar.  The five pages of the site will be identical, with the only change being to the content div which is nested inside the wrapper div and the table.  So my structure would appear to need nested chunks. 
Will that work? 

If so, would I create one chunk for the wrapper (which is most of the page); an interior chunk for the navigation table; and then insert a weblog for the interior content div? 

Many thanks -

44
(Author)
April 08, 2008

Whooboy. 

You can nest embedded templates - so from a functional perspective you can go that route if need be.

Me - I don’t like nested embeds.  If you’re married to the table-based layout, what I’d probably do is leave the skeleton of the table in the template, and embed things within it like the navigation.

45
Danna McCormick
April 08, 2008

ok, I’m feeling the whooboy vibes. 

So from an EE perspective, is the problem with tables (which I can easily replace with divs), or the nesting structure? (which can be redesigned if that’s the direction I need to go).

46
(Author)
April 08, 2008

EE is layout-agnostic—so there’s really nothing from that perspective to worry about.

It’s more the consideration of nested embeds—which again will work, but down the road will be harder to maintain and/or reverse-engineer when the site needs work.

And....overall it’s just time to not use tables for layout, ya know?  wink

47

April 08, 2008

Oh the guilt, the shame! 
I feel compelled to say that I *rarely* use tables - in this case, a quick fix to prevent writing a hack.

Re-design is in the works to prevent nested embeds - thanks again for the direction.... see you in chapter 3, and 4, and 5…

48

April 12, 2008

Wow, this was awesome.  Got my client’s site converted over to EE templates first try!  I really appreciate the time it took you to put this together.

On to Chapter 3....

Name:
Email:
URL:
Comments:

Remember me?

Shoot me an email when someone responds?

Enter this word:

Here:

GetBoyinked

Subscribe to Boyink.com