Location: PHPKode > projects > SithTemplate > SithTemplate-1.1/docs/html/03_context_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: 03_context.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>03_context.php</h1>  </div>
</div>
<div class="contents">
<p>An example showing how to create and use template context.</p>
<div class="fragment"><pre class="fragment">&lt;?php
require_once <span class="stringliteral">&#39;SithTemplate.php&#39;</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">// Context array is passed as first argument to Template::render, or as second</span>
<span class="comment">// argument to TemplateEnviron::render.</span>
$tpl = $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://{{ foo }} &#39;</span>);

echo $tpl-&gt;render(array(<span class="stringliteral">&#39;foo&#39;</span> =&gt; <span class="stringliteral">&#39;first&#39;</span>),  $environ);
echo $tpl-&gt;render(array(<span class="stringliteral">&#39;foo&#39;</span> =&gt; <span class="stringliteral">&#39;second&#39;</span>), $environ);
<span class="comment">// Will produce: &quot;first second &quot;</span>

<span class="comment">// Above is the simplest variable expression. To access nested elements, slightly more</span>
<span class="comment">// complex syntax is required, presented below, with equivalent PHP code:</span>
<span class="comment">//</span>
<span class="comment">// - accessing a named array element</span>
<span class="comment">//   {{ foo.bar }} is equivalent to $context[&#39;foo&#39;][&#39;bar&#39;]</span>
<span class="comment">// - accessing a numeric array index</span>
<span class="comment">//   {{ foo.42 }} is equivalent to $context[&#39;foo&#39;][42]</span>
<span class="comment">// - accessing a named or numeric array index, using value of another variable as key</span>
<span class="comment">//   {{ foo.[bar] }} is equivalent to $context[&#39;foo&#39;][$context[&#39;bar&#39;]]</span>
<span class="comment">//</span>
<span class="comment">// Same syntax rules applies to object properties - you just use -&gt; operator instead of ., e.g.</span>
<span class="comment">// {{ foo-&gt;bar }}.</span>
<span class="comment">//</span>
<span class="comment">// This syntax allows you to create very complex constructs, like:</span>
<span class="comment">//  {{ [one-&gt;[two]].three-&gt;four }} which is equivalent to</span>
<span class="comment">//  $context[ $context[&#39;one&#39;]-&gt;{$context[&#39;two&#39;]} ][&#39;three&#39;]-&gt;four</span>
<span class="comment">//</span>
<span class="comment">// SithTemplate by default generates code to check whether variable really exists in the context</span>
<span class="comment">// before it is used, which triggers E_USER_WARNING if it doesn&#39;t. This can interfere with &quot;optional&quot;</span>
<span class="comment">// variables (e.g. ones used with &#39;default&#39; filter). You can tell compiler to omit this code, by prefixing</span>
<span class="comment">// entire expression with @ sign:</span>
<span class="comment">//  {{ @non-existant-variable }}</span>

<span class="comment">// Filter chains are built with pipe operator. Filter arguments are comma-separated, passed after colon.</span>
<span class="comment">//  {{ variable|filter1|filter2:variable2,&quot;foo&quot; }}</span>
<span class="comment">// is roughly equivalent (if filters were simply functions) to</span>
<span class="comment">//  filter2(filter1($context[&#39;variable&#39;]), $context[&#39;variable2&#39;], &#39;foo&#39;)</span>
</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