Location: PHPKode > projects > Open Power Template > docs/Opt/api.opt-compiler-processor._extract-attributes.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>_extractAttributes() - 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>_extractAttributes()</h2>
		<p class="generated">@ 02.09.2010</p>
		<p class="location"><a href="index.html"><strong>User manual</strong></a> &raquo; <a href="api.html">API Reference</a> &raquo; <a href="api.opt-compiler-processor.html">Opt_Compiler_Processor</a> &raquo; <a href="api.opt-compiler-processor._extract-attributes.html">Opt_Compiler_Processor::_extractAttributes()</a></p>
	</div>
	
	<div id="content"><dl class="location"><dt><a href="api.opt-compiler-processor.html">7.7. Opt_Compiler_Processor class</a><br/>7.7.5. _extractAttributes()</dt><dd class="prev">7.7.4. _enqueue()<br/><a href="api.opt-compiler-processor._enqueue.html">&laquo; Previous</a></dd><dd class="next">7.7.6. _process()<br/><a href="api.opt-compiler-processor._process.html">Next &raquo;</a></dd></dl>	<h1>7.7.5. _extractAttributes()</h1><div class="tf_reference"><table><tr><th>Status</th><td>final protected</td></tr><tr><th>Reference</th><td><code>array _extractAttributes(Opt_Xml_Element $tag, Array &$attributes)</code></td></tr></table><hr/></div><p>This method simplifies the instruction tag attribute validation. As the first parameter, we pass the <code>Opt_Xml_Element</code> node we want to check, and the second one is a list of acceptable attributes and their definitions. It is passed by reference, because OPT replaces it with an associative array of attribute values.</p>

<p><code>$attributes</code> is an associative array, where the index means the attribute name, and the value is a three-element array:</p>

<ol>
<li>Is the attribute required or optional: <code>Opt_Xml_Attribute::REQUIRED</code> or <code>Opt_Xml_Attribute::OPTIONAL</code>.</li>
<li>Attribute type (described below).</li>
<li>The default value for the attribute, if it is optional and the template designer has not specified it.</li>
</ol>

<p>The available attribute types are divided in two groups. The first one are the attributes that their value must be known at the stage of compilation and it cannot be set with a template variable or the script. In this category, we can find:</p>

<ol>
<li><code>Opt_Xml_Attribute::ID</code> - an identifier</li>
<li><code>Opt_Xml_Attribute::HARD_STRING</code> - a string</li>
<li><code>Opt_Xml_Attribute::NUMBER</code> - a number</li>
</ol>

<p>In the second group, there are the types that send the attribute value to the expression parser and return a valid PHP expression that may be placed somewhere in the instruction output code. Note that the template designer may always choose one of the types by changing the namespace, so they are only a suggestion, which one is the default one, if the namespace is not defined.</p>

<ol>
<li><code>Opt_Xml_Attribute::STRING</code> - compile the value as a PHP string by default.</li>
<li><code>Opt_Xml_Attribute::EXPRESSION</code> - compile the value as an expression by default.</li>
<li><code>Opt_Xml_Attribute::ASSIGN_EXPR</code> - compile the value as a PHP string by default. The assignment operator is allowed.</li>
</ol>

<p>An example:</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;">$attr</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
        <span style="color: #666666; font-style: italic;">// 'foo' is required</span>
        <span style="color: #0000ff;">'foo'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>Opt_Xml_Attribute<span style="color: #339933;">::</span><span style="color: #004000;">REQUIRED</span><span style="color: #339933;">,</span> Opt_Xml_Attribute<span style="color: #339933;">::</span><span style="color: #004000;">EXPRESSION</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #666666; font-style: italic;">// 'bar' is optional and its default value is &quot;bar&quot;</span>
        <span style="color: #0000ff;">'bar'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>Opt_Xml_Attribute<span style="color: #339933;">::</span><span style="color: #004000;">OPTIONAL</span><span style="color: #339933;">,</span> Opt_Xml_Attribute<span style="color: #339933;">::</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'bar'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_extractAttributes<span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">,</span> <span style="color: #000088;">$attr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Mark that we do not generate any PHP code for this node.</span>
    <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'nophp'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Generate the output code for this tag.</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_BEFORE</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'The compiled expression is: '</span><span style="color: #339933;">.</span><span style="color: #000088;">$attr</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'foo'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;br/&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$attr</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'bar'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'bar'</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;">addBefore</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;">'The optional attribute has been set&lt;br/&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end processNode();</span></pre>

<p>The method supports also undefined number of attributes. If we specify a special rule called <code>__UNKNOWN__</code>, all the attributes that are not declared in the array, are matched against it. The values of those attributes are <strong>returned</strong> by the method as an associative array. If <code>__UNKNOWN__</code> is required, the template designer must specify at least one custom attribute.</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;">$attr</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
        <span style="color: #666666; font-style: italic;">// Definiujemy atrybut wymagany</span>
        <span style="color: #0000ff;">'foo'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>Opt_Xml_Attribute<span style="color: #339933;">::</span><span style="color: #004000;">REQUIRED</span><span style="color: #339933;">,</span> Opt_Xml_Attribute<span style="color: #339933;">::</span><span style="color: #004000;">EXPRESSION</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #666666; font-style: italic;">// Definiujemy atrybut opcjonalny z domyślną wartością &quot;bar&quot;</span>
        <span style="color: #0000ff;">'__UNKNOWN__'</span> <span style="color: #339933;">=&gt;</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>Opt_Xml_Attribute<span style="color: #339933;">::</span><span style="color: #004000;">OPTIONAL</span><span style="color: #339933;">,</span> Opt_Xml_Attribute<span style="color: #339933;">::</span><span style="color: #004000;">ID</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$other</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_extractAttributes<span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">,</span> <span style="color: #000088;">$attr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'nophp'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <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_BEFORE</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'The compiled expression is: '</span><span style="color: #339933;">.</span><span style="color: #000088;">$attr</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'foo'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;br/&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Wyświetl nadmiarowe atrybuty</span>
    <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$other</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</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;">addBefore</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: #000088;">$name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">': '</span><span style="color: #339933;">.</span><span style="color: #000088;">$value</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;br/&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end processNode();</span></pre>
<h4>See also:</h4><ul><li><a href="api.opt-compiler-processor.process-node.html">7.7.12. Opt_Compiler_Processor::processNode()</a></li></ul><dl class="location location-bottom"><dt>7.7.5. _extractAttributes()<br/><a href="api.opt-compiler-processor.html">7.7. Opt_Compiler_Processor class</a></dt><dd class="prev"><a href="api.opt-compiler-processor._enqueue.html">&laquo; Previous</a><br/>7.7.4. _enqueue()</dd><dd class="next"><a href="api.opt-compiler-processor._process.html">Next &raquo;</a><br/>7.7.6. _process()</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