<p>I guess it is time to come back from my little hiatus to actually write a blog post. I recently moved, so I have been busy with getting settled and working on freelance projects as well. If you <a href="http://twitter.com/davist11">follow me on Twitter</a>, you may have noticed that I have recently become obsessed with ExpressionEngine. I completed a <a href="//trevor-davis.com/work/ecfc/">project</a> at work using it, and I realized that I could have done something much more efficiently.</p>

<p><a href="http://ee.trevor-davis.com/newsletter/" class="more-link">View Demo</a></p>
<p>I had to create an “online newsletter” that had issues that were made up of multiple articles. I ended up creating two separate weblogs: one for each issue, and one for individual articles. In the issues weblog, there was a <a href="http://brandon-kelly.com/playa">Playa</a> custom field which could be used to pick the individual articles to be published in that issue. But, another one of Brandon Kelly’s fabulous extensions, <a href="http://brandon-kelly.com/fieldframe">FieldFrame</a>, seems to be a much better choice for creating this functionality.</p>
<p>So, download, install, &amp; enable the <a href="http://brandon-kelly.com/fieldframe">FieldFrame Extension</a>, and let’s be on our way.</p>
<h2>Creating the Custom Field Group</h2>
<p>First, let’s create a field group named <strong>Newsletter</strong>. Now, we just need to create one custom field with the field name of <strong>newsletter_articles</strong>. Select <strong>FF Matrix</strong> as the field type.</p>

<p>We want to have two cells in the FF Matrix: <strong>articles_title</strong> &amp; <strong>articles_body</strong>. The cell types should be text and textarea, respectively. Then, adjust the sizes as you see fit.</p>

<p><img src="//trevor-davis.com/images/content/2009/08/newsletter-ff.png" alt="FF Matrix Configuration" width="570" height="282" class="frame pullLeft" /></p>

<h2>Creating the Weblog</h2>
<p>Now that we have the custom field group setup, we need to create a new weblog called <strong>newsletter</strong> and make sure to assign our <strong>Newsletter field group</strong> to it. Now that the newsletter weblog is created, we can start adding newsletters.</p>

<p><img src="//trevor-davis.com/images/content/2009/08/newletter-entry.png" alt="Newsletter Entry Form" width="570" height="304" class="frame pullLeft" /></p>

<p>First, fill in the main title field, and then add in one or more articles. Now, let’s get onto coding the output template.</p>

<h2>Creating the Template</h2>
<p>Let’s start by creating a new template group called <strong>newsletter</strong>. We have two different pages that we want to create: the page listing all of the newsletter entries, and the individual newsletter entry displaying all of the articles. But, we can do this with only one template.</p>

<p>So, open up the index template in the newsletter template group, and we will start by setting up the structure of the template:</p>

<div class="codeblock"><code>
{assign_variable:weblog="newsletter"}<br />{embed="newsletter/.header" title="{if segment_2}<br /> {!--Display individual newsletter entry title here--}<br />{if:else}<br /> Newsletters<br />{/if}<br />"}<br /><br />{if segment_2}<br /> {!--Display full individual newsletter entry with 1 or more articles--}<br />{if:else}<br /> {!--Display list of newsletters entered--}<br />{/if}<br /><br />{embed="newsletter/.footer"} 

</code></div>

<p>We start by creating a variable to store the name of our weblog. Then, I created a simple header and footer to store some of the HTML that frames the page. I am passing in the title value in an embed variable to the header and displaying that in the browser title.</p>

<p>If the segment_2 value is not null, we know that we are on an individual newsletter page, so we will display that title. If the segment_2 value is null, then we are on the main newsletter listing page, so we just display Newsletters.</p>

<p>We are using the same conditional again for the content on the page. We either show the individual newsletter entry with the articles, or we show the list of newsletter entered. So let’s build out the code a little more. First, let’s finish displaying the title:</p>

<div class="codeblock"><code>
{embed="newsletter/.header" title="{if segment_2}<br /> {exp:weblog:entries weblog="{weblog}" limit="1" disable="categories|category_fields|custom_fields|member_data|pagination|trackbacks"}<br />  {title} : {entry_date format="%F %j, %Y"}<br /> {/exp:weblog:entries}<br />{if:else}<br /> Newsletters<br />{/if}<br />"} 

</code></div>
<p>Pretty simple stuff here, just displaying the title and entry date of the newsletter entry. Next, let’s tackle displaying the single newsletter entry view:</p>

<div class="codeblock"><code>
{if segment_2}<br /> {exp:weblog:entries weblog="{weblog}" limit="1" disable="categories|category_fields|custom_fields|member_data|pagination|trackbacks"}<br />  &lt;h1&gt;{title}: {entry_date format="%F %j, %Y"}&lt;/h1&gt;<br /><br />  {newsletter_articles}<br />   &lt;div class="entry"&gt;<br />    &lt;h2&gt;{articles_title}&lt;/h2&gt;<br />    {articles_body}<br />   &lt;/div&gt;<br />  {/newsletter_articles}<br /> {/exp:weblog:entries}<br /> &lt;p&gt;&lt;a href="{path="newsletter"}"&gt;&amp;laquo; Back to Newsletters&lt;/a&gt;&lt;/p&gt; 

</code></div>

<p>The beginning should look pretty normal: just using the standard exp:weblog:entries tag to pull in the individual entry. We first display the title and entry date. Then, the next part is where we are taking advantage of the <a href="http://brandon-kelly.com/fieldframe/docs/ff-matrix">FieldFrame Matrix</a>. We are basically just looping through all of the <strong>newsletter_articles</strong> and displaying them in divs.</p>

<p>Finally, let’s finish up by displaying the list of newsletter entries:</p>
<div class="codeblock"><code>
{if:else}<br /> {exp:weblog:entries weblog="{weblog}" disable="categories|category_fields|custom_fields|member_data|pagination|trackbacks"}<br />  {if count == 1}<br />   &lt;ul&gt;<br />  {/if}<br /><br />   &lt;li&gt;&lt;a href="{url_title_path="newsletter"}"&gt;{title}: {entry_date format="%F %j, %Y"}&lt;/a&gt;&lt;/li&gt;<br />  {if count == total_results}<br />   &lt;/ul&gt;<br />  {/if}<br /> {/exp:weblog:entries}<br />{/if} 

</code></div>

<p>Simple stuff again: just looping through all newsletter entries and displaying them in a list. The two conditional statements are so you don’t end up with any empty unordered lists when there are no entries.</p>

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

<p>I wish I would have realized this would have been a more efficient way to build a newsletter in EE for the project; but, this is why always learning is so important.</p>