Location: PHPKode > projects > Open Power Template > docs/Opt/extending.instructions.generating-php.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="pl">
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<meta name="robots" content="all" />

	<title>Generating PHP code - Open Power Template</title>
	
	<link rel="stylesheet" type="text/css" href="design/generic.css" media="all"  />
	<link rel="stylesheet" type="text/css" href="design/print.css" media="print" />
	<!--[if lte IE 6]><link rel="stylesheet" href="design/ie.css" type="text/css" /><![endif]-->	
	<!--[if IE 7]><link rel="stylesheet" href="design/ie7.css" type="text/css" /><![endif]-->
</head>
<body>

<div id="wrap">
	<div id="header">
		<h1>Open Power Template 2.0</h1>
		<h2>Generating PHP code</h2>
		<p class="generated">@ 02.09.2010</p>
		<p class="location"><a href="index.html"><strong>User manual</strong></a> &raquo; <a href="extending.html">Extending OPT</a> &raquo; <a href="extending.instructions.html">New instructions</a> &raquo; <a href="extending.instructions.generating-php.html">Generating PHP code</a></p>
	</div>
	
	<div id="content"><dl class="location"><dt><a href="extending.instructions.html">5.8. New instructions</a><br/>5.8.2. Generating PHP code</dt><dd class="prev">5.8.1. Processor overview<br/><a href="extending.instructions.processor-overview.html">&laquo; Previous</a></dd><dd class="next">5.8.3. Parsing attributes<br/><a href="extending.instructions.parsing-attributes.html">Next &raquo;</a></dd></dl>	<h1>5.8.2. Generating PHP code</h1><p>OPT instructions do not generate the PHP code linearly. Instead, the code snippets are appended or prepended to different buffers associated with each node in the XML tree. There are many buffers available for a single node, inserted into different parts of the tag.</p>

<h2>Available code buffers</h2>

<h3>Code buffers in XML tags</h3>

<p>The locations of the code buffers around the XML tags are shown below:</p>

<blockquote>
  <p><code>&lt;tag&gt;...&lt;/tag&gt;</code> node:</p>
  
  <p><img src="media/code_buffers_1.png" alt="Location of the code buffers around the XML tag" title="Location of the code buffers around the XML tag" /></p>
</blockquote>

<!-- ... -->

<blockquote>
  <p><code>&lt;tag /&gt;</code> node:</p>
  
  <p><img src="media/code_buffers_2.png" alt="Location of the code buffers around the single XML tag" title="Location of the code buffers around the single XML tag" /></p>
</blockquote>

<p>The <code>TAG_AFTER</code> and <code>TAG_BEFORE</code> buffers wrap the whole node and are present in both tag types. We may paste there the PHP code snippets that affect the whole tag, for example pack it in a conditional block or a loop. It is also possible to wrap either opening or closing tag. The content buffers are available only, if the tag <em>is not empty</em>, that is - it contains any children or the <code>TAG_CONTENT</code> buffer is set. This buffer has a priority over the XML content when rendering.</p>

<blockquote class="warning">
  <p>Note that the single tags use different buffer set and it is important to know, what tag we are trying to add the snippets to.</p>
</blockquote>

<p>It is possible to replace the output tag name with <code>TAG_NAME</code> buffer. However, unlike to other buffers, it may store only one snippet at once.</p>

<p>The code buffers around the attribute list are also available:</p>

<p><img src="media/code_buffers_3.png" alt="Location of the code buffers around XML attributes" title="Location of the code buffers around XML attributes" /></p>

<h3>Code buffers around the attributes</h3>

<p>The <code>TAG_ATTRIBUTES_AFTER</code> and <code>TAG_ATTRIBUTES_BEFORE</code> wrap the whole attribute list which may be used to control, whether to display them or not. The <code>TAG_BEGINNING_ATTRIBUTES</code> and <code>TAG_ENDING_ATTRIBUTES</code> should be used to generate new, dynamic attributes either before or after the XML-defined ones.</p>

<p>A single attribute uses the following buffers:</p>

<ol>
<li><code>ATTRIBUTE_BEGIN</code>, <code>ATTRIBUTE_END</code> - executed before and after the attribute.</li>
<li><code>ATTRIBUTE_NAME</code> - attribute name replacement (maximum one snippet allowed).</li>
<li><code>ATTRIBUTE_VALUE</code> - attribute value replacement.</li>
</ol>

<h3>Code buffers around other nodes</h3>

<p><code>Opt_Xml_Cdata</code>, <code>Opt_Xml_Text</code>, <code>Opt_Xml_Expression</code> and <code>Opt_Xml_Root</code> are wrapped with <code>TAG_BEFORE</code> and <code>TAG_AFTER</code> buffers only.</p>

<h2>Adding a snippet to the buffer</h2>

<p>Each node is equipped with two methods that allow to add new PHP code snippets to the buffers:</p>

<pre class="php"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> processNode<span style="color: #009900;">&#40;</span>Opt_Xml_Node <span style="color: #000088;">$node</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addAfter</span><span style="color: #009900;">&#40;</span>Opt_Xml_Buffer<span style="color: #339933;">::</span><span style="color: #004000;">TAG_BEFORE</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' if(condition) { '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addBefore</span><span style="color: #009900;">&#40;</span>Opt_Xml_Buffer<span style="color: #339933;">::</span><span style="color: #004000;">TAG_AFTER</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' } '</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_process<span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end processNode();</span></pre>

<p><code>addAfter()</code> appends the snippet to the end of the buffer, whereas <code>addBefore()</code> prepends it to the beginning of the buffer.</p>

<blockquote class="help">
  <p>To enclose a node in curly brackets, you should use both of the functions, as it is show in the sample code above. <code>addBefore(Opt_Xml_Buffer::TAG_BEFORE)</code> and <code>addAfter(Opt_Xml_Buffer::TAG_AFTER)</code> wrap the existing snippets in the curly brackets, too, whereas the opposite setting causes the newly inserted block to <em>be wrapped</em> in the existing snippets.</p>
</blockquote>

<p>It is possible to fill the buffers in any of the nodes composing the XML tree at any moment of instruction processing.</p>

<h2>Other buffer operations</h2>

<p>Sometimes, it is necessary to copy the existing buffer from one node to another. OPT provides <code>copyBuffer()</code> method here:</p>

<pre class="php"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> processNode<span style="color: #009900;">&#40;</span>Opt_Xml_Node <span style="color: #000088;">$node</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Let $subnode be any other node.</span>
&nbsp;
    <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">copyBuffer</span><span style="color: #009900;">&#40;</span>Opt_Xml_Buffer<span style="color: #339933;">::</span><span style="color: #004000;">TAG_CONTENT</span><span style="color: #339933;">,</span> <span style="color: #000088;">$subnode</span><span style="color: #339933;">,</span> Opt_Xml_Buffer<span style="color: #339933;">::</span><span style="color: #004000;">TAG_SINGLE_AFTER</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_process<span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end processNode();</span></pre>

<p>In the example above, we copy the content of <code>TAG_SINGLE_AFTER</code> buffer in <code>$subnode</code> to the <code>TAG_CONTENT</code> buffer in <code>$node</code>. The current buffer size can be obtained with <code>bufferSize(bufferID)</code> method, and clearing the buffer - with <code>clear()</code> (without an argument - clears all the buffers in the node).</p>
<dl class="location location-bottom"><dt>5.8.2. Generating PHP code<br/><a href="extending.instructions.html">5.8. New instructions</a></dt><dd class="prev"><a href="extending.instructions.processor-overview.html">&laquo; Previous</a><br/>5.8.1. Processor overview</dd><dd class="next"><a href="extending.instructions.parsing-attributes.html">Next &raquo;</a><br/>5.8.3. Parsing attributes</dd></dl>		</div>
	
	<div id="footer">
		<p>Copyright &copy; <a href="http://www.invenzzia.org/">Invenzzia Group 2008-2009</a></p>
		<p>Available under the terms of license: <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License 1.2</a></p>
		<p>Generated by <strong>TypeFriendly 0.1.4</strong> by <a href="http://www.invenzzia.org/">Invenzzia</a></p>
	</div>
</div>

</body>
</html>
Return current item: Open Power Template