<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHP Software Developing &#187; template</title>
	<atom:link href="http://www.phpdeveloping.co.za/tag/template/feed" rel="self" type="application/rss+xml" />
	<link>http://www.phpdeveloping.co.za</link>
	<description>for the love of PHP Development</description>
	<lastBuildDate>Tue, 29 Sep 2009 15:38:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Quick and dirty template system</title>
		<link>http://www.phpdeveloping.co.za/strings/quick-and-dirty-template-system.html</link>
		<comments>http://www.phpdeveloping.co.za/strings/quick-and-dirty-template-system.html#comments</comments>
		<pubDate>Mon, 27 Jul 2009 07:41:12 +0000</pubDate>
		<dc:creator>chris</dc:creator>
				<category><![CDATA[Strings]]></category>
		<category><![CDATA[Templates]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[str_replace]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://www.phpdeveloping.co.za/?p=84</guid>
		<description><![CDATA[Before I knew about Smarty, I developed me own way of having HTML templates for the projects I developed.  Admittedly, I still use it even though Smarty is so much more powerful.  When I don&#8217;t need the versatility of Smarty, I just use my own method.
I started off by having a template directory [...]]]></description>
			<content:encoded><![CDATA[<p>Before I knew about <a href="http://www.smarty.net">Smarty</a>, I developed me own way of having HTML templates for the projects I developed.  Admittedly, I still use it even though Smarty is so much more powerful.  When I don&#8217;t need the versatility of Smarty, I just use my own method.</p>
<p>I started off by having a template directory with HTML files in it.  These files consist of all the HTML code I want, and has placeholders to where content will be.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;body&gt;
&lt;div&gt;%header%&lt;/div&gt;
&lt;div&gt;
  &lt;div&gt;%menu%&lt;/div&gt;
  &lt;div&gt;%content%&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;%footer%&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>As you can see, I have 4 place holders there, %header%, %menu%, %content% and %footer.</p>
<p>Now all I have to do is use str_replace to replace the place holders with content.</p>
<p>Here is an example of doing that</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;template.html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$menu</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;ul&gt;&lt;li&gt;Option 1&lt;/li&gt;&lt;li&gt;Option 2&lt;/li&gt;&lt;/ul&gt;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;h1&gt;This is the Header&lt;/h1&gt;&lt;br /&gt;%menu%&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;p&gt;This is some content.&lt;/p&gt;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$footer</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;small&gt;This is the footer&lt;/small&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%header%&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$header</span><span style="color: #339933;">,</span><span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;%menu%&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$menu</span><span style="color: #339933;">,</span><span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%c</span>ontent%&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$content</span><span style="color: #339933;">,</span><span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$html</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%f</span>ooter%&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$footer</span><span style="color: #339933;">,</span><span style="color: #000088;">$html</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$html</span><span style="color: #339933;">;</span></pre></div></div>

<p>As you can see, I make use of the placeholder within another placeholder as well.  So the %menu% is also replace within the $header variable.  Just note if you want to use it like that, you need to order the str_replace so that it will get replaced.</p>
<p>The resulting HTML will be:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;body&gt;
&lt;div&gt;&lt;h1&gt;This is the Header&lt;/h1&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Option 1&lt;/li&gt;&lt;li&gt;Option 2&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;
&lt;div&gt;
  &lt;div&gt;&lt;ul&gt;&lt;li&gt;Option 1&lt;/li&gt;&lt;li&gt;Option 2&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;
  &lt;div&gt;&lt;p&gt;This is some content.&lt;/p&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;&lt;small&gt;This is the footer&lt;/small&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.phpdeveloping.co.za/strings/quick-and-dirty-template-system.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
