<p><a href="http://expressionengine.com/">Expression Engine</a> has become a popular choice to power many sites. I have just begun experimenting with it, and I have been very impressed. One thing that I have been interested in determining was to figure out if I could set an id on the body like I do when building other sites. I have already written one article showing how to do this in <a href="//trevor-davis.com/blog/tutorial/set-a-body-id/">PHP and WordPress</a>, but this will show how to do it dynamically in Expression Engine.</p>

<h2>Why are Body Ids Useful?</h2>
<p>Body ids can be useful for styling sections differently, displaying certain parts of a dynamic template, displaying current navigation states, and many other things.</p>

<h3>A Simple Example</h3>
<p>Let’s say that I want to have my font size be 14 pixels, <strong>except on the homepage</strong>. It would be as easy as this:</p>

<div class="codeblock"><code>
body { font-size: 14px; }<br />body#home { font-size: 12px; } 

</code></div>

<p><strong>Pretty useful.</strong> By using body ids, we now have a unique hook for each section of the website.</p>

<h2>Why not Body Classes?</h2>
<p>I do utilize body classes as well. I typically use the body id to indicate the section, then other various items for the body classes.</p>

<h3>For Example</h3>
<p>For my own blog, I have some pages that I want to have a left gutter on it so that I can push images, quotes, etc. into the column. I also have another class that indicates when it is a blog article page. So, I can just continue to use multiple classes on the body to help me control the width and layout of the page.</p>

<p>Here is what the body element looks like on a blog article on my site:</p>

<div class="codeblock"><code>
&lt;body id="blog" class="subpage leftGutter article"&gt; 

</code></div>

<p>The <strong>subpage class</strong> just indicates that it is not the homepage. Not completely necessary since I already have a specific id on the homepage, but it can sometimes become useful.</p>

<p>The <strong>leftGutter class</strong> is used to push the content area to the right and make it narrower. Then, I can pull elements into the left-hand gutter as well.</p>

<p>The <strong>article class</strong> is useful because some of the markup for the blog entry information is the same as the homepage and archive pages. Using this class, I can be more specific with my styles to change the way that these elements are displayed on the blog article page.</p>

<p>I always try and add classes and id attributes as high up in the tree as possible. This let’s me utilize descendant selectors in the CSS without having to add additional classes and ids lower down.</p>

<h2>Setting the Body Id in Expression Engine</h2>
<p>I want to have complete control over the body id, so I want to be able to have it set dynamically and also be able to set it statically.</p>

<p>So my thought behind this is that first, I want to check to see if the body id was set statically. When I say statically, I mean passing the variable in as an attribute on my embed statement. Next, I will then set the body id dynamically.</p>

<h3>Setting the Id Statically</h3>
<p>In order to make changes to the overall website structure simpler, I have a header template the sits in the includes template group. So in all of the other template groups, I embed the header on the page:</p>

<div class="codeblock"><code>
{embed="includes/header"} 

</code></div>

<p>Then, if for some reason I want to specifically pass in a body id, I can just change it to this:</p>

<pre><code>{embed="includes/header" <strong>body_id="contact-us"</strong>}</code></pre>

<p>So the first step in the header is to check to see if the <strong>embed:body_id</strong> variable is set:</p>
<div class="codeblock"><code>
&lt;body<br />{if embed:body_id}<br /> id="{embed:body_id}"<br />{/if}<br />&gt; 

</code></div>
<p class="note"><em>Note: Line wrapping added for readability.</em></p>

<h3>Dynamically Setting the Id</h3>
<p>If we are not statically setting the body id, we want to then do it dynamically. My thinking is that I will take advantage of Expression Engine’s <a href="http://expressionengine.com/docs/templates/globals/url_segments.html">URL segments</a> to determine which section we are in.</p>

<p>Here is an example of URL segments using the following URL:</p>

<div class="codeblock"><code>
http://ee.trevor-davis.com/index.php/about/me/education/ 

</code></div>

<ul><li>Segment 1 = <strong>about</strong></li>
 <li>Segment 2 = <strong>me</strong></li>
 <li>Segment 3 = <strong>education</strong></li>
</ul><p>Then, we can reference each segment like so:</p>

<ul><li>Segment 1 = <strong>{segment_1}</strong></li>
 <li>Segment 2 = <strong>{segment_2}</strong></li>
 <li>Segment 3 = <strong>{segment_3}</strong></li>
</ul><p>So basically what we want to do is assign the body id the value of <strong>{segment_1}</strong> if it is not empty. So continuing on our example:</p>

<div class="codeblock"><code>
&lt;body<br />{if embed:body_id}<br /> id="{embed:body_id}"<br />{if:elseif segment_1}<br /> id="{segment_1}"<br />{/if}<br />&gt; 

</code></div>

<p class="note"><em>Note: Line wrapping added for readability.</em></p>

<p><strong>But wait, what about the homepage?</strong> The homepage will have nothing for the value of <strong>{segment_1}</strong>, so we can just put in an else statement:</p>

<div class="codeblock"><code>
&lt;body<br />{if embed:body_id}<br /> id="{embed:body_id}"<br />{if:elseif segment_1}<br /> id="{segment_1}"<br />{if:else}<br /> id="home"<br />{/if}<br />&gt; 

</code></div>

<p class="note"><em>Note: Line wrapping added for readability.</em></p>

<p>Here is the line of code without any line wrapping:</p>
<div class="codeblock"><code>
&lt;body{if embed:body_id} id="{embed:body_id}"{if:elseif segment_1} id="{segment_1}"{if:else} id="home"{/if}&gt; 

</code></div>

<p>I created a <a href="http://ee.trevor-davis.com/index.php/">demo Expression Engine site</a> to test this functionality.</p>

<p><a href="http://ee.trevor-davis.com/index.php/" class="more-link">View Demo Site</a></p>

<h2>Walking Through the Demo Site</h2>
<p>Since we have body ids for each section and an id on each list item in the navigation, I have created the current state for the navigation with a few simple line of CSS:</p>
<div class="codeblock"><code>
body#home ul#primaryNav li#homeNav a,<br />body#about ul#primaryNav li#aboutNav a,<br />body#services ul#primaryNav li#servicesNav a,<br />body#contact-us ul#primaryNav li#contactNav a {<br /> background: #B3602D;<br /> border-top-color: #B3602D;<br /> color: #fff;<br />} 

</code></div>
<p class="note"><em>Note: Line wrapping added for readability.</em></p>

<p>I also want to show the sidebar on all of the subpages, so I just need 1 more conditional:</p>
<div class="codeblock"><code>
{if embed:body_id || segment_1}<br /> &lt;div id="sidebar"&gt;<br /> &lt;/div&gt;<br />{/if} 

</code></div>

<p>This is checking to see if {embed:body_id} or {segment_1} are not empty, and displaying the sidebar if one or both are set.</p>

<p>These are just a few examples of using a body id in Expression Engine, but the possibilities are endless.</p>

<h2>One More Thing</h2>
<p>Another thing that I was trying to figure out was how to create a variable that I could reuse in multiple embed files. So what I want to do is in my conditional statement, determine that the body id should be and assign it to a variable. Then, I want to be able to access that variable in the footer without have to use the same conditional statement.</p>

<p><strong>Is this possible?</strong> I was messing around with the <strong>{assign_variable}</strong> tag, but that didn’t seem to do the job because it can only be used in the same template. <strong>Anyone know how to do this?</strong> I’m relatively new to Expression Engine, so I am still learning. </p>