Opt Instruction Expression
From Invenzzia wiki
| Opt_Instruction_Expression | |
| Project | Open Power Template 2 |
| Type | Instruction |
| Author | Tomasz Jędrzejewski |
| Website | http://www.invenzzia.org/ |
| License | New BSD License |
| Status | No information |
The instruction adds the opt:expression tag. It allows you to write a group of OPT expressions without the curly brackets that will be executed one after another. The new line works as an expression separator.
Source code
Below you can find a source code of the instruction. You can install it either as a OPT plugin or manually.
<?php
/*
* An extra instruction that adds the "opt:expression" tag to OPT.
*
* Author: Tomasz "Zyx" Jędrzejewski <http://www.invenzzia.org>
* License: New BSD License <http://www.invenzzia.org/license/new-bsd>
*/
class Opt_ExpressionInvalidContent_Exception extends Opt_Instruction_Exception
{
protected $_message = 'The opt:expression tag may contain only the text data.';
} // end Opt_ExpressionInvalidContent_Exception;
class Opt_Instruction_Expression extends Opt_Compiler_Processor
{
public function configure()
{
$this->_addInstructions(array('opt:expression'));
} // end configure();
public function processNode(Opt_Xml_Node $node)
{
if($node->countChildren() != 1 || ! $node->getLastChild() instanceof Opt_Xml_Text)
{
throw new Opt_ExpressionInvalidContent_Exception();
}
if($node->getLastChild()->countChildren() != 1 || ! $node->getLastChild()->getLastChild() instanceof Opt_Xml_Cdata)
{
throw new Opt_ExpressionInvalidContent_Exception();
}
// Extract the code
$lines = explode("\n", (string)$node->getLastChild()->getLastChild());
foreach($lines as $line)
{
$line = trim($line);
if($line != '')
{
$expression = $this->_compiler->compileExpression(trim($line), true, Opt_Compiler_Class::ESCAPE_OFF);
$node->addAfter(Opt_Xml_Buffer::TAG_SINGLE_AFTER, $expression[0].'; ');
}
}
$node->set('single', true);
$node->removeChildren();
} // end processNode();
} // end Opt_Instruction_Expression;
Installation instructions:
- Copy the file somewhere.
- Register the class in the autoloader:
Opl_Loader::mapAbsolute('Opt_Instruction_Expression', './some/path'); - Register the instruction in OPT:
$tpl->register(Opt_Class::OPT_INSTRUCTION, 'Expression');
Plugin installation:
- Save the code above as a file
instruction.Expression.phpin your OPT plugin directory. - Ensure that the plugin support is active in your script.
- Enjoy.
Usage
The opt:expression tag may contain only the text data with the expressions. No other XML nodes are allowed:
<opt:expression>
@hello is 'Hi universe'
$a is $foo + $bar
$text is spacify($text, '-')
</opt:expression>
<p>{$a}, {$text}</p>
Every expression must be written in the new line. Blank lines are omitted and ignored.

