Location: PHPKode > projects > SithTemplate > SithTemplate-1.1/docs/html/00_hello_8php-example.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>SithTemplate: 00_hello.php</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.7.2 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<h1>00_hello.php</h1>  </div>
</div>
<div class="contents">
<p>An "hello world" example, showing how to create template environment with default settings, template object using string I/O, and render it.</p>
<div class="fragment"><pre class="fragment">&lt;?php
require_once <span class="stringliteral">&#39;SithTemplate.php&#39;</span>;

<span class="comment">// 1. We create environment</span>
$environ  = <span class="keyword">new</span> <a name="_a0"></a><a class="code" href="class_template_environ.html" title="Template environment - library&amp;#39;s end-user API.">TemplateEnviron</a>;
<span class="comment">// 2. Next, we create template object</span>
<span class="comment">// Library will take care of the (re)compilation.</span>
<span class="comment">// SithTemplate 1.1 introduced unified I/O system,</span>
<span class="comment">// which allows you to easily inline small templates in your PHP code.</span>
$template = $environ-&gt;<a name="a1"></a><a class="code" href="class_template_environ.html#a83a8ca46797584a7a175198532b220e7" title="Returns template instance.">get</a>(<span class="stringliteral">&#39;string://Hello world&#39;</span>);
<span class="comment">// 3. Finally, we render and display previously created template</span>
<span class="comment">// You may notice that display/fetch APIs are gone, replaced by</span>
<span class="comment">// generic ones - you need to display template output by yourself.</span>
<span class="comment">//</span>
<span class="comment">// You can also see that environment object is passed back to the template -</span>
<span class="comment">// it is used in several places, like {% include %}-generated code, but passing</span>
<span class="comment">// it here, and not during construction, keeps template object more lightweight</span>
<span class="comment">// and independent, as it doesn&#39;t carry reference to original environment.</span>
<span class="comment">// It also eliminates possibility of circular reference, when template object</span>
<span class="comment">// is stored in environ&#39;s internal cache.</span>
echo $template-&gt;render(array(), $environ);

<span class="comment">// If you don&#39;t want to cache the template object on your own, you can use</span>
<span class="comment">// chained calls to cachedGet and render:</span>
$environ-&gt;cachedGet(<span class="stringliteral">&#39;string://Other&#39;</span>)-&gt;render(array(), $environ);

<span class="comment">// If you don&#39;t need the object at all, you can call TemplateEnviron::render instead.</span>
<span class="comment">// This call is the same as the chained call above, just shorter and less explicit.</span>
$environ-&gt;render(<span class="stringliteral">&#39;string://Other&#39;</span>, array());
</pre></div> </div>
</div>
<hr class="footer"/><address class="footer"><small>Generated on Fri Jan 14 2011 20:08:36 for SithTemplate by&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.2 </small></address>
</body>
</html>
Return current item: SithTemplate