{"id":756,"date":"2012-09-12T00:24:39","date_gmt":"2012-09-12T00:24:39","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=756"},"modified":"2012-09-12T00:26:04","modified_gmt":"2012-09-12T00:26:04","slug":"layouts-templates","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/layouts-templates\/","title":{"rendered":"Structuring your Site &#8211; Layout vs. Templates"},"content":{"rendered":"<p>When I hear the word <em>layout<\/em>, what instantly comes to mind is a Photoshop file with design details for an upcoming web project. On the other hand, when I hear the word <em>template<\/em>, I think about a group of web files, grouped together to produce a layout.<\/p>\n<p>There are three basic levels of site structure:<\/p>\n<ol>\n<li><strong>Static.<\/strong> With static files, the site layout is part of each and every web file. This, arguably is the fastest performing method, but it presents a challenge when it comes time to update the site layout. That challenge usually outweighs any performance gained.\n<p>\t\tFor example, consider that your website contains 100 pages. Each page containing the HTML markup for your header, content, sidebar and footer.<\/p>\n<p>\t\tNow, consider that you would like to add a slogan next to your logo, in the site header. If you&#39;ve done this before, you know what a painful experience this is. You need to change the HTML markup in all 100 of those files.<\/p>\n<p>\t\tDepending on the nature of your update, you might be able to use your code editor&#39;s file search and replace function, but this isn&#39;t always easy or reliable.<br \/>\n\t\t&nbsp;<\/li>\n<li><strong>Templates.<\/strong> This is the middle ground of updatability and performance. WordPress are developers are familiar with this method.\n<p>\t\tIn a typical structure, you create template files for your header, footer and sidebar. Then you include those files on every page in your site.<\/p>\n<p>\t\tFor example, your <em>index.php<\/em> file might look like:<\/p>\n<p>\t\t<code>&lt;?php<br \/>\n\t\trequire(&#39;header.php&#39;);<br \/>\n\t\techo &#39;&lt;p&gt;This is my content.&lt;\/p&gt;&#39;;<br \/>\n\t\trequire(&#39;sidebar.php&#39;);<br \/>\n\t\techo &#39;&lt;ul&gt;&lt;li&gt;This is my context-aware sidebar item&lt;\/li&gt;&lt;\/ul&gt;&#39;;<br \/>\n\t\trequire(&#39;footer.php&#39;);<br \/>\n\t\t?&gt;<\/code><\/p>\n<p>\t\tThe <em>header.ph<\/em>p will contain your Doctype declaration, head tag, opening body tag, and logo and navigation markup. Your <em>sidebar.php<\/em> might contain context-aware side navigation. And, the <em>footer.php<\/em> would contain any less used navigational links, and your copyright.<\/p>\n<p>\t\tWhat could be better than this? Any time you want to update your logo file, add a slogan, change your sidebar or footer text, all you need to do is update the one file.<\/p>\n<p>\t\tBut, what happens when we want to wrap the entire site in another &lt;div&gt;, or change the context-aware sidebar to an ordered list (&lt;ol&gt;). This is doable with the <em>&quot;template \/ include&quot;<\/em> method above, but is very sloppy.<br \/>\n\t\t&nbsp;<\/li>\n<li><strong>Layout. <\/strong>Unless I&#39;m working in a framework built on the <em>&quot;template \/ include&quot;<\/em> method (such as WordPress), I always opt for the Layout method from the get-go.\n<p>\t\tThe Layout method offers the ultimate in flexibility, because your dynamic elements are stored in variables, and output by the layout file.<\/p>\n<p>\t\tThis, no doubt, needs an example. To produce the same results as with the template method <em>index.php<\/em> above, in the layout method, the index.php might look something like:<\/p>\n<p>\t\t<code>&lt;?php<br \/>\n\t\tob_start();<br \/>\n\t\techo &#39;&lt;p&gt;This is my content.&lt;\/p&gt;<br \/>\n\t\t$content = ob_get_contents();<br \/>\n\t\tob_end_clean();<\/p>\n<p>\t\tob_start();<br \/>\n\t\techo &#39;&lt;li&gt;This is my context-aware sidebar item&lt;\/li&gt;&#39;;<br \/>\n\t\t$sidebar = ob_get_contents();<br \/>\n\t\tob_end_clean();<\/p>\n<p>\t\tinclude(&#39;lay_default.php&#39;);<br \/>\n\t\t?&gt;<\/code><\/p>\n<p>\t\tYou&#39;ll notice two things here. First, that we did not wrap the &lt;li&gt; with a parent &lt;ul&gt; or &lt;ol&gt;. And, second, that we are storing these content elements to variables.<\/p>\n<p>\t\tThen, we are including a<em> lay_default.php<\/em> file. Now, how can this possibly produce the same results as above.<\/p>\n<p>\t\tLet&#39;s start by explaining what ob_start() does. Essentially, it creates an output buffer, allowing you to store all content created afterwards to a variable, instead of sending it directly to the output stream.<\/p>\n<p>\t\tMost languages offer a means to do this. In ColdFusion, for example, you would use the <code><tt>&lt;cfsavecontent&gt;<\/tt><\/code> tag.<\/p>\n<p>\t\tOur layout file might look like this:<\/p>\n<p>\t\t<code>&lt;!DOCTYPE HTML&gt;<br \/>\n\t\t&lt;head&gt;<br \/>\n\t\t....<br \/>\n\t\t&lt;\/head&gt;<br \/>\n\t\t&lt;body&gt;<br \/>\n\t\t&nbsp; &lt;!-- logo markup goes here --&gt;<br \/>\n\t\t&nbsp; &lt;!-- navigation here --&gt;<br \/>\n\t\t&nbsp; &lt;div id=&quot;content&quot;&gt;<br \/>\n\t\t&nbsp;&nbsp;&nbsp; &lt;?php echo $content; ?&gt;<br \/>\n\t\t&nbsp; &lt;\/div&gt;<br \/>\n\t\t&nbsp; &lt;div id=&quot;sidebar&quot;&gt;<br \/>\n\t\t&nbsp;&nbsp;&nbsp; &lt;ul&gt;<br \/>\n\t\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;?php echo $sidebar; ?&gt;<br \/>\n\t\t&nbsp;&nbsp;&nbsp; &lt;\/ul&gt;<br \/>\n\t\t&nbsp;&nbsp;&nbsp; &lt;!-- global sidebar items --&gt;<br \/>\n\t\t&nbsp; &lt;\/div&gt;<br \/>\n\t\t&nbsp; &lt;!-- footer stuff goes here --&gt;<br \/>\n\t\t&lt;\/body&gt;<br \/>\n\t\t&lt;\/html&gt;<br \/>\n\t\t<\/code><\/p>\n<p>Pretty great, right. Now, if we want to change that &lt;ul&gt; to an &lt;ol&gt; at any point, we only have to do it once, in the <em>lay_template.php<\/em> file! If we want to wrap the site in another &lt;div&gt;, same deal.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p>Essentially, we gain three things with the Layout method:<\/p>\n<ol>\n<li>Scalability &#8211; We can change the layout file at any time. If we want to take advantage of new HTML 5 elements, we can do so easily, in one place.<br \/>\n\t\t&nbsp;<\/li>\n<li>Performance &#8211; We&#39;re only opening one additional file, the layout file.<br \/>\n\t\t&nbsp;<\/li>\n<li>Simplicity &#8211; The number of template files needed is reduce, making your site easier to maintain.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>When I hear the word layout, what instantly comes to mind is a Photoshop file with design details for an upcoming web project. On the&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[34,16,41,4],"tags":[207,205,206],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/756"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=756"}],"version-history":[{"count":10,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/756\/revisions"}],"predecessor-version":[{"id":768,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/756\/revisions\/768"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}