Opt Instruction Php
From Invenzzia wiki
| Opt_Instruction_Php | |
| 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:php tag that allows you to embed the PHP code directly in the OPT templates. The PHP code must be enclosed within a CDATA section.
This instruction is potentially dangerous. You use it for your own risk. Furthermore, if your template is going to use some internal stuff from the compiled code, I cannot guarantee it won't be changed between the releases.
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:php" tag to OPT.
*
* Author: Tomasz "Zyx" Jędrzejewski <http://www.invenzzia.org>
* License: New BSD License <http://www.invenzzia.org/license/new-bsd>
*
* !!!!!!!!!!! WARNING !!!!!!!!!!!
* This instruction is potentially dangerous. You use it for your own risk.
* Furthermore, if your template is going to use some internal stuff from the
* compiled code, I cannot guarantee it won't be changed between the releases.
*/
class Opt_PhpInvalidContent_Exception extends Opt_Instruction_Exception
{
protected $_message = 'The opt:php tag may contain only the text data.';
} // end Opt_InstructionTooManyItems_Exception;
class Opt_Instruction_Php extends Opt_Compiler_Processor
{
public function configure()
{
$this->_addInstructions(array('opt:php'));
} // end configure();
public function processNode(Opt_Xml_Node $node)
{
if($node->countChildren() != 1 || ! $node->getLastChild() instanceof Opt_Xml_Text)
{
throw new Opt_PhpInvalidContent_Exception();
}
if($node->getLastChild()->countChildren() != 1 || ! $node->getLastChild()->getLastChild() instanceof Opt_Xml_Cdata)
{
throw new Opt_PhpInvalidContent_Exception();
}
$node->set('single', true);
$node->addAfter(Opt_Xml_Buffer::TAG_SINGLE_AFTER, (string)$node->getLastChild()->getLastChild());
$node->removeChildren();
} // end processNode();
} // end Opt_Instruction_Php;
Installation instructions:
- Copy the file somewhere.
- Register the class in the autoloader:
Opl_Loader::mapLocal('Opt_Instruction_Php', './some/path'); - Register the instruction in OPT:
$tpl->register(Opt_Class::OPT_INSTRUCTION, 'Php');
Usage
The opt:php tag may contain only the CDATA section with the PHP code. No other XML nodes are allowed:
<p>
<opt:php><![CDATA[
echo 'HI UNIVERSE';
]]></opt:php>
</p>

