Building an ExpressionEngine Site - Chapter 16

In which we fully implement the Weblog section, learn about global variables and passing variables in variables. 

Author’s Note: My apologies - I accidentally deleted this entry while working on chapter 17.  I was able to retrieve the article text from my rss feed, but lost all the original comments.

Yes, finally, I’m back with another chapter in the Building an ExpressionEngine Site series.  It’s been a couple of weeks since our last installment - but with the holidays plus negotiating for my new job with EllisLab and other projects going on, this one took awhile.  Also - and this is a good heads up for those of you new to implementing sites on EE, weblogs will always take longer than other sections of the site.  Why?  Simply because they require more work - more templates and more formatting. 

However, as always with EE, there are a number of ways to implement a weblog with EE.  Let me explain at a high level the approach I’m taking here and you can decide if it meets your needs or not. 

Weblog Implementation Overview
Most weblogs will require a number of templates - however, depending on your requirements and expectations of traffic to a weblog you can make some choices that will save you some time and trouble on the front end.

Weblogs being, well, weblogs, they usually have the following needs:

  • An index template that comes up by default, and shows the last x number of posts with the newest at the top.  If there are longer posts, often this template only shows part of the post and has a “read more” link of some sort, which leads to the…
  • Single-view template that shows an entire post
  • Comments template where users can respond
  • Permalink template which places this post at a specific URL that won’t change as new content gets added to the weblog
  • Category Index - if the weblog posts are categorized, this view shows the latest posts assigned to the desired category
  • Archive - most often this is broken down by month
  • RSS or Atom feed

Quite a list. 

However - especially for weblogs like we’re building that are part of a business site (rather than really being the entire site) we can repurpose one template to serve multiple needs.  In this tutorial we’ll accomplish the weblog implementation with three main templates and a few new embedded templates.  The three main templates will be:

  • Weblog Index.  This will serve as the default index view, the category view and the archive view
  • Weblog Comments.  This will serve as the single-view, comments, and permalink template.
  • RSS feed.

Working Model
Here’s a link to the working model weblog that this tutorial covers: http://www.boyink.com/splaat/building_series/chapter_16_weblog/.  Play around with it, and note where all the links go.

If your blog is busier, has deep archives, or you want a different category view, you might consider dedicated templates for those purposes.  I prefer to start simple so for most clients just getting into blogs will start with this approach and modify it later based on actual need. 

Onto the build!

Create Data Structures
To begin, you’ll first need to get the EE data structures in place.  You’ll need:

  • A new weblog.  In my case and in all the sample templates I’ll be providing this is called “bees_weblog”.  Make sure commenting is enabled.
  • A fieldset.  I’m using the default field set that installs with EE so will have fields of body, summary, and extended.
  • A category group.  I created a weblog-specific category group called “bees_weblog” with categories of Company News, Industry News, and Ramblings.  I entered short descriptions for each.
  • Remember to assign the new category group and field group to the new weblog.

Do Some Data Entry
Go ahead and make some entries in your new weblog - at least one per category.  Make sure to use all the fields so you’ll know if your templates function correctly.  Oh - and also post a new section heading type thing into your miscellaneous_content weblog.  Make sure it has a url_title of “weblog”.

Load the New Main Templates
Here are the new templates you’ll need.  Let’s get these and the new embedded templates loaded up and working, then I’ll go through and hit the highlights.  I’ve done a bit of higher-level coding in these both to save you some time tweaking them and to demonstrate the use of EE variables.  Here are the three main templates.  These should go in the same template group as the other main templates in your site:

  • Weblog Index (saved as chapter_16_weblog in my example site): chapter_16_weblog.txt
  • Weblog Comments (saved as chapter_16_comments in my example site):chapter_16_comments.txt
  • RSS (saved as rss_2 in my example site). Note that this feed has been modified so that it will display HTML. rss_2.txt

RSS Template
Make sure when you create the new template for the RSS feed, you choose “RSS Page” in the “Template Type” drop-down.  If you forget while creating the template, just click the “preferences” link for the template group you put it in, and change it there.

Load the New Embedded Templates
The weblog index and weblog comments template share some right-column elements, so I’ve pulled these out and saved them as embedded templates.  Here are the ones you’ll need.  Save them into the template group holding the rest of your embedded templates:

Grab the Updated Stylesheet
We’ve several new content types now, and I had to create entries in the stylesheet for them.  Make sure to grab the latest version: http://www.boyink.com/splaat?building_series/stylesheet/

Update Main Navigation
Remember to update the embedded template that contains your main navigation.  Mine now looks like this:

<div id="menu">
    <
div class="submit">
        <
ul>
            <
li><a{if '{embed:my_location}'=="home"} class="selected"{/if} href="/splaat/building_series/chapter_16_index/" ><span>Home</span></a></li>
            <
li><a{if '{embed:my_location}'=="about"} class="selected"{/if} href="/splaat/building_series/chapter_16_about/"><span>About</span></a></li>
            <
li><a{if '{embed:my_location}'=="products"} class="selected"{/if} href="/splaat/building_series/chapter_16_products/"><span>Products</span></a></li>
            <
li><a{if '{embed:my_location}'=="services"} class="selected"{/if} href="/splaat/building_series/chapter_16_services/"><span>Services</span></a></li>
            <
li><a{if '{embed:my_location}'=="weblog"} class="selected"{/if} href="/splaat/building_series/chapter_16_weblog/"><span>Weblog</span></a></li>
            <
li><a href="#"><span>Contact</span></a></li>
        </
ul>
    </
div>
</
div>

Specifying Names with Global Variables
OK, with those new templates loaded you’ll need to make and name or pathing changes required for using them in your environment.  If you’ve been following along with this series, you’ll have done this several times, changing the code of the embeds to reflect the name of your specific template group holding your embedded templates.

But don’t do that yet.  I’ve made this easier, and want to explain how.  First open the weblog_index template.  See where it has the following code:

{assign_variable:my_weblog="bees_weblog"}
{assign_variable
:my_template_group="building_series"}
{assign_variable
:my_embedded_template_group="building_series_ch16_embeds"}
{assign_variable
:my_index_template="chapter_16_weblog"}
{assign_variable
:my_comments_template="chapter_16_comments"}

What’s that all about?

These are Dynamically Assigned Global Variables, although don’t let the “Global” world fool you.  Like the docs state: “Variables created this way are available for use anywhere within that template.”

Essentially you can think of these as “text-replacement” variables.  As EE parses the template, it will store the text you specify in the “assign_variable” statement, and then anywhere it runs into that variable name in the rest of the code template, EE will swap out the variable name for the text.  For example, we’ve specified “building_series_ch16_embeds” as the value for “my_embedded_template_group”.  Later in the template, when EE finds:

{embed="{my_embedded_template_group}/logo_title"}

EE will substitute the text we specified and see that line of code as:

{embed="building_series_ch16_embeds/logo_title"}

Got it?

Cool.

Why did I use them?  Two reasons:

  • First, to make it easier for you to move these templates into your environment.  Rather than having to edit several lines of code, now you can just make the change in one spot and the rest of the template will use that updated variable value.
  • To get you thinking about more advanced coding methods like this.  Depending on your specific EE project, using things like Global Variables can make it easier to get your work done by centralizing templates that do the same thing but for different areas of your site.

So - take a moment now and edit the values of those variables in both the chapter_16_weblog and chapter_16_comments templates where appropriate. Once saved, the templates should load and function properly.

Building Links Using a Combination of Variables
With the global variables in place, now it’s possible to build links using a combination of true Global Variables, template Global Variables, and content from a weblog entry.  Consider this link:

<a href="{homepage}/{my_template_group}/{my_comments_template}/{url_title}">Read More...</a>

Passing Global Variables to Embedded Templates
OK, take a deep breath.  This will be the most complex part of this chapter, and possibly this entire tutorial series. Go do some carbo-loading if need be.  Ready?  OK. 

Here’s the situation.  The parent weblog index and weblog comments templates know some things that the embedded templates also need to know.  The embedded templates are building the navigation to the category and archive views of our weblog, which will use the index template for presentation.  The parent templates know the name of the weblog needed, the name of the index template and what template group it’s in, because we’ve specified those items as global variables.  But we have two embedded templates that will also need that information. 

What to do?  Well, we could just copy our “assign_variable” code from the parent templates to the embedded templates.  But that’s not very elegant, and increases the number of places to make edits if those values change.

The other alternative is to combine Global Variables with Embed variables.  This way, the variable is specified in the parent template, then passed into the embedded template for use there. 

Now just stop and think about this for a minute.  What we’ll end up with is an embedded template that builds, for example, links to weblog categories.  But the weblog name of where the categories come from along with the location of the templates it needs to link to are variables.  What’s this mean?  You now have a little “category-link-builder” that you could re-use across different parts of a site, even when the weblog is different or the templates it’s linking to is different.  I’ve used this approach here on Boyink.com—I have four main weblogs (the business blog, Jeep, Bantam, and Photos).  On each of them I wanted category navigation for the bottom of the page. With this approach I have one embedded template that builds that category nav, and I just send it the necessary weblog name, and template names it needs to link to.  Then, for example, when I wanted to pull category descriptions in addition to the category name, all I had to do was edit that one embedded template’s code, and voila - all four weblogs instantly see the update.

So how to do this?  The trick to remember is that in order to pass a Global Variable to an embedded template you have to change it’s name on the way.  For example, in our weblog index template we have:

{assign_variable:my_weblog="bees_weblog"}

While passing it to the embedded template (category navigation builder), the variable gets its name changed to “the_weblog”:

{embed="{my_embedded_template_group}/weblog_categories" the_index_template="{my_index_template}" the_template_group="{my_template_group}" the_weblog="{my_weblog}"}

Then in the embedded template, the new name is used:

{exp:weblog:categories weblog="{embed:the_weblog}" style="linear"}

Also note that formatting with quotes etc. is very picky and particular while doing this.

OK- breathe easy. The hardest part is over.

Again, to summarize, with these templates you should be able to load them up, edit the global variables to reflect what you’ve named both the templates and your template groups (make sure to edit both the weblog_index and weblog_comments) and once saved, the weblog should function.

The Comments Form
Let’s take a look at the comments form now.  You might be wondering why, since I have only one weblog single view/permalink/comments template why I choose to embed the part of the page that actually displays the comments (the embedded template called “weblog_entry_comments").  Mainly it makes the parent comments template a bit shorter/cleaner, and it sets you up well for use with multiple weblogs.  Ever since building Boyink.com in this fashion - where I wanted to have a centralized comments form for use by four different weblogs - I’ve been doing it this way.  Comments are a bit of a pain to layout and style, so only doing it once for multiple weblogs is a nice way to go.

Lesse...what else is there with this Chapter.  Oh yea…

Highlighted Category Nav
Just for fun I used URL Segments along with conditionals to apply an active class to the currently viewed category.  You can see it action here.

Let’s look at that code:

<h3>Weblog Categories</h3>
<
div class="lcontent">
    <
ul>
         
{!-- Conditional on the list item applies an active class to the currently viewed category. --}
        {exp
:weblog:categories weblog="{embed:the_weblog}" style="linear"}
            
<li {if segment_4==category_url_title}class="active_cat"{/if}>
                <
a href="{homepage}/{embed:the_template_group}/{embed:the_index_template}/category/{category_url_title}/">{category_name}</a>
            </
li>
        
{/exp:weblog:categories}
        
         {
!-- This conditional will only show when a category has been selected, and allows a user to return to the default of showing all posts on the weblog --}
        {if segment_3
=="category"}    
            
<li><a href="{homepage}/{embed:the_template_group}/{embed:the_index_template}/">All News Items</a></li>
        
{/if}
    
</ul>
</
div>


All this does is compare the 4th url segment (which displays the category url title for the currently viewed category) to the category_url_title of each category returned by the weblog:categories tag.  When the condition is true, the class of “active_cat” is applied to that category name.  If your URL structure is different you may need to change what segment the code looks at.

Also - I’ve used another conditional to display an “All News Items” link under the category list.  This will only display of a category has been choosen.  Not a big deal - it just flags the user that they are looking at a category view and provides a link that’s a bit more clear than remembering to click the “weblog” main navigation item.

Monthly Archives
Aside from our trickery with the variables, the archive links are built using a straightforward implementation of the weblog:month_links tag.

RSS Template
The RSS template is also a straightforward copy/tweak of the template that EE installs with.  As mentioned above I did make the changes required for it to display HTML.

Zzzzzzzz
Still with me?  My eyes are bugging out a bit reading this.  I have a feeling I may need to re-visit this chapter, but since it’s been a while and since this is such a big chapter I’m going to put it live.  You can let me know in the comments if there’s something I didn’t explain clearly or if the templates didn’t load and work as expected.

Next up - barring the need to re-vamp this chapter - I’ll work on the Contact Us page.  That should be really quick and easy compared to this one.  Past that all we’ve got is the Search and Search results to get going.

Back to Chapter 15.
On to Chapter 17.

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
Robert Scanlon
December 31, 2007

Hi Mike,

Happy New Year!

I just wanted to say thanks - I’ve been following along all the tutorials - what a magnificent job!

I’m new to EE, but have built many a static site and a few on WordPress. Following your tutorials has opened my eyes to the amazing power of EE - this has confirmed my purchase was a good one.

I have also purchased the forum module (but done nothing with any of it yet - just getting familiar with the default EE theme and the admin system - a steep learning curve!). I’m wondering if you’ll have time somewhere in 2008 to explore adding a forum section, and for example using categories to group similar posts/blog/comments/products/services all on the one themed page(s) etc, as well as maybe “finishing” what Jambor-ee started and extending to a tutorial on community generated content?

If so, I for one would be prepared to make a donation if you’d like to put a button up on your site somewhere - so we can all benefit?

If others are interested, they could second my comments here.

In any case, even if you go no further, I’ve gained immense value as an EE newbie from the lessons you have taught me so many many thanks and may the karma come around sooner rather than later!

Cheers,

Robert

2
(Author)
January 01, 2008

Hi Robert -

Thanks so much for the comment.  I’m glad to know the series has been helpful to you, no worries as I’ve already benefited from it in many ways.

Also - I have other plans in regard to EE and training, but must remain tight-lipped for now..wink

3

January 02, 2008

the rss.txt doesn’t load, Mike

4
(Author)
January 02, 2008

Strange...does for me.

Try it here:
http://www.boyink.com/images/blog/rss_2.txt

5
Leroy Campbell
January 09, 2008

Hi again, Mike. Do you know what would cause the “...server unexpectedly dropped the connection...” error? The weblog comments page works fine when I’m logged in. As soon as I log out, I get this error every time I attempt to open an article from the weblog index.

6
(Author)
January 09, 2008

Got me there Leroy - I’d suggest a post in the support forums.

7
(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.

8
Bruce
February 06, 2008

Mike

I’m no technical expert but I did get the gist so far so I don’t know if you need much rewriting.

One thing I did differently was not to put any text into the ‘extended’ field of the weblog – which means that my weblog shows the full post without a ‘read more’ link.

Anyway, easy to solve by editing my posts but out of interest, does the ‘extended’ tag not show by default? I can’t find any detail on that. I was expecting us to use the character or word limit plugin.

Just to point out to others that this shows up as a conditional in the weblog template:

{if extended}Read More...{/if}

Thanks.

9
(Author)
February 06, 2008

Hey Bruce -

The notion here is that you control if the read more link shows by putting text in the extended field.

You can show the contents of the extended text field by default by just removing the conditional.

Or you could look into using a plug-in instead.  I don’t prefer that method as I don’t want a read more link if there’s only a few words more than whatever limit I coded in.

10
Forrest
March 04, 2008

Mike,

I am on the home stretch and this has been a great lesson. Many thanks for the time you put into creating this.

I am stuck on the weblog postings. The header and posting information show up, as does the sidebar content, but instead of body of the entry, I am getting { summary } and { body } showing up. Any clue?

Thanks,

Forrest

11
(Author)
March 04, 2008

Sounds like you have those tags after the weblog:entries tag is getting closed.

12
Forrest
March 04, 2008

Mike,

Here is the code:

{exp:weblog:entries weblog="{my_weblog}" orderby="date" sort="desc" limit="15" disable="trackbacks"}
<div class="entry">
<h3>{title}</h3>
{summary}
{body}
{if extended}Read More...{/if}

<div class="posted">
Posted by {author} on {entry_date format=’%m/%d’} at {entry_date format=’%h:%i %A’} in

{categories}
{category_name}
{/categories}

{if allow_comments}
({comment_total}) Comments
{/if}

Permalink
</div>

{paginate}
<div class="paginate">
<span class="pagecount">Page {current_page} of {total_pages} pages</span> {pagination_links}
</div>
{/paginate}
</div>
{/exp:weblog:entries}

13
Forrest
March 04, 2008

Problem solved. I had the wrong Field Group assigned to the weblog. Thanks for all the assistance.

Forrest

14
Forrest
March 05, 2008

Mike,

This tutorial has been a remarkable resource and I appreciate you making it available.

I have done the weblog and everything works with one exception. When I click on the category links on the right, all of the weblog entries show up regardless of their category. The month selectors work properly but not the category ones. Any suggestions?

Thanks,

Forrest

15

March 24, 2008

Mike,

These tutorials have been an invaluable asset for me and I thank you for putting them together.

I seem to be having a problem with this chapter though. It all works properly except for the fact that when posting comments, I’m not getting the input your name and email boxes and the captcha link. On the weblog administration for the weblog, I have it set to enable captcha for comment posting but I still am not seeing it. Any ideas as to why this wouldn’t be showing up?

16
(Author)
March 25, 2008

Sam -

My first guess is that its because you are logged in, and the comments form is designed to only require those fields from a non logged in user.

I find it helpful to have two browsers going when stying comments forms- so you can be logged in on one and logged out in the other .

17
Rokker
April 09, 2008

hey Michael..

just working through the tutorial, and was pretty pleased with myself at how it’s going..... but ran into a dead end, and after spinning in circles and going goofy from code, i thought i’d seek the pro’s to help.

mostly everything from chapter 16 is working fine, except for the category titles don’t pop in above the posts, and the category links on the right return the same thing, without showing the category title as well.

oh, and the comments entry form doesn’t seem to show up. i worked through a few of the probs i had, but can’t seem to find where i went wrong.

any suggestions? confused in Wisconsin
Rokker

18
Rokker
April 09, 2008

here are two examples to my tutorial

http://www.ilovenakashima.com/index.php/tutorial/weblog/

http://www.ilovenakashima.com/index.php/tutorial/weblog/category/company_news/

let me know if i should post more than these links to properly diagnose

19
(Author)
April 09, 2008

Rokker -

The category issue is, I think, because you’re trying to use keywords in categories while EE hasn’t been configured to do that.  Under CP Home › Admin › Weblog Administration › Global Weblog Preferences make sure it’s set to “use category URL titles in links”.

Not sure on the comments entry form - try viewing while not being logged in, make sure comments are enabled at both the weblog and post level, if you’ve embedded the comment form make sure you’re passing it the weblog name.

20
Rokker
April 09, 2008

hey Michael!

thanx! just the prod i needed! sure enough, you were right on the “use category URL titles in links” wink

and turned out in the comments template name i’d forgotten to put “weblog” in the title for the “weblog_entry_comments” template, and kept zipping right past that referal in the comments template.

i will continue on with this amazing tutorial!

thanx again!!

21

May 11, 2008

How can I display the most recent comments in my sidebar.

  Commenting is no longer allowed on this entry.

GetBoyinked

Subscribe to Boyink.com