Invenzzia »

Opt Instruction Expression

From Invenzzia wiki

Jump to: navigation, search
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:

  1. Copy the file somewhere.
  2. Register the class in the autoloader: Opl_Loader::mapAbsolute('Opt_Instruction_Expression', './some/path');
  3. Register the instruction in OPT: $tpl->register(Opt_Class::OPT_INSTRUCTION, 'Expression');

Plugin installation:

  1. Save the code above as a file instruction.Expression.php in your OPT plugin directory.
  2. Ensure that the plugin support is active in your script.
  3. 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.

Personal tools