Location: PHPKode > projects > Open Power Template > docs/Opt/syntax.expressions.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>Expressions - 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>Expressions</h2>
		<p class="generated">@ 02.09.2010</p>
		<p class="location"><a href="index.html"><strong>User manual</strong></a> &raquo; <a href="syntax.html">Template syntax</a> &raquo; <a href="syntax.expressions.html">Expressions</a></p>
	</div>
	
	<div id="content"><dl class="location"><dt><a href="syntax.html">3. Template syntax</a><br/>3.5. Expressions</dt><dd class="prev">3.4. Entities<br/><a href="syntax.entities.html">&laquo; Previous</a></dd><dd class="next">3.5.1. Variables<br/><a href="syntax.expressions.variables.html">Next &raquo;</a></dd></dl>	<h1>3.5. Expressions</h1><h4>Table of Contents</h4><ul class="toc"><li><a href="syntax.expressions.variables.html">3.5.1. Variables</a></li><li><a href="syntax.expressions.values.html">3.5.2. Values</a></li><li><a href="syntax.expressions.operators.html">3.5.3. Operators</a></li><li><a href="syntax.expressions.functions.html">3.5.4. Functions</a></li><li><a href="syntax.expressions.backticks.html">3.5.5. Backticks</a></li><li><a href="syntax.expressions.objects.html">3.5.6. Objects</a></li><li><a href="syntax.expressions.escaping.html">3.5.7. HTML escaping</a></li></ul><p>So far, we have discussed the static template elements, that are rewritten to the output. Now we are going to describe something that adds more dynamics to the templates - the expressions. The expression role is to produce some value, especially to display it somewhere. An expression can be put in the static text or as a tag attribute value. The following exaple shows the valid and invalid ways to do so:</p>

<pre class="xml"><span style="color: #808080; font-style: italic;">&lt;!-- good --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Expression as a part of static text: {$variable}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/para<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- bad --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p</span> <span style="color: #66cc66;">&#123;</span>$variable<span style="color: #66cc66;">&#125;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>The expression must not be placed in that way.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- bad --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;{$variable}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>The expression must not be placed in that way.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- good --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p</span> <span style="color: #000066;">parse:class</span>=<span style="color: #ff0000;">&quot;$variable&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Expression as an attribute value.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- good --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;opt:if</span> <span style="color: #000066;">test</span>=<span style="color: #ff0000;">&quot;$variable&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Expression as an OPT instruction attribute value.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/opt:if<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre>

<p>The details concerning OPT instructions will be explained later.</p>

<p>If you are familiar with PHP expression system, you should have no problems with understanding the OPT one. We did not reinvent the wheel, but simply used the existing conventions and patterns in order to make it more readable. Most of the rules and techniques that applied in PHP, are also available in OPT and all you have to do is to pay attention on the operator symbols which may differ.</p>

<p>Open Power Template provides a built-in expression parser that parses the expressions and reports the errors. If the expression is invalid, the compiler will generate an exception during the compilation which looks like this: <code>Unexpected token OPCODE_XXX (xxx) in expression (expression)</code>. It informs that the compiler came across the specified token (we see the type: <code>OPCODE_XXX</code> and the exact value), but it must not be used in that place. Moreover, it shows the full expression so that you could locate it in the template.</p>

<h2>OPT expressions and JavaScript</h2>

<p>A common problem among many beginning OPT programmers is how to generate some JavaScript code, using the values from OPT variables. As we already know, OPT expressions are very similar to PHP, so generally we can use exactly the same techniques, like in embedding some SQL code within PHP. Below, we can see an example:</p>

<pre class="xml"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">parse:onclick</span>=<span style="color: #ff0000;">&quot;'javascript:someFunction(\''~$optVariable~'\');'&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
...
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre>

<p>Tilde is the string concatenation operator in OPT. The JavaScript code is enclosed within quotes as a string, and we concatenate the OPT variable value to it. Once we execute such template, we will get a correct JavaScript code.</p>

<p>In order to generate bigger blocks of JavaScript code within <code>&lt;script&gt;</code> tag, we must use <a href="syntax.instructions.literal.html" title="3.7.14. opt:literal">opt:literal</a> instruction. You can find more examples in the documentation page of this instruction.</p>

<h2>Forward compatibility notes</h2>

<p>The current way of handling expressions within XML disallows using it in attributes that already have a namespace. This problem is going to be fixed in the incoming Open Power Template 2.1 version, however - it requires to break the backward compatibility. In the new version, the expression engine will be selected within the attribute value:</p>

<pre class="xml"><span style="color: #808080; font-style: italic;">&lt;!-- OPT 2.0 --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">parse:class</span>=<span style="color: #ff0000;">&quot;$foo&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> ... <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #808080; font-style: italic;">&lt;!-- OPT 2.1 --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;parse:$foo&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> ... <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre>

<p>In order to simplify the migration, the new version will be equipped with the migration module which will allow either to compile the old code from OPT 2.0 or convert the existing templates to the newer syntax automatically.</p>
<dl class="location location-bottom"><dt>3.5. Expressions<br/><a href="syntax.html">3. Template syntax</a></dt><dd class="prev"><a href="syntax.entities.html">&laquo; Previous</a><br/>3.4. Entities</dd><dd class="next"><a href="syntax.expressions.variables.html">Next &raquo;</a><br/>3.5.1. Variables</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