Opt Instruction Url
From Invenzzia wiki
| Opt_Instruction_Url | |
| Project | Open Power Template 2 |
| Type | Instruction |
| Author | Tomasz Jędrzejewski |
| Website | http://www.invenzzia.org/ |
| License | New BSD License |
| Status | No information |
This instruction is an URL routing helper for use with various frameworks. It provides an alternative way to create dynamic URL-s with the framework router.
The source code
The source code for the instruction can be found below. You can either register it manually or as an OPT plugin.
<?php
class Opt_Instruction_Url extends Opt_Compiler_Processor
{
const ROUTING_FUNCTION = 'myRoutingFunction';
static private $_cnt = 0;
public function configure()
{
$this->_addInstructions(array('opt:url'));
} // end configure();
public function processNode(Opt_Xml_Node $node)
{
// Prevent from adding an attribute to OPT tags
if(!$node->getParent() instanceof Opt_Xml_Element)
{
throw new Opt_InstructionInvalidParent_Exception($node->getXmlName(), 'any non-OPT tag');
}
if($this->_compiler->isNamespace($node->getParent()->getNamespace()))
{
throw new Opt_InstructionInvalidParent_Exception($node->getXmlName(), 'any non-OPT tag');
}
// Parse the instruction attributes
$params = array(
'attribute' => array(0 => self::OPTIONAL, self::HARD_STRING, 'href'),
'__UNKNOWN__' => array(0 => self::OPTIONAL, self::EXPRESSION)
);
$vars = $this->_extractAttributes($node, $params);
if($node->hasChildren())
{
$attributes = $node->getElementsByTagNameNS('opt', 'attribute', false);
if(sizeof($attributes) == 0)
{
$this->_buildCode($node, $vars, $params['attribute']);
return;
}
foreach($attributes as $attr)
{
$attr->set('attributeValueStyle', Opt_Instruction_Attribute::ATTR_RAW);
}
$node->set('priv:url', $vars);
$node->set('priv:attribute', $params['attribute']);
$node->set('postprocess', true);
$this->_process($node);
}
else
{
$this->_buildCode($node, $vars, $params['attribute']);
}
} // end processNode();
public function postprocessNode(Opt_Xml_Node $node)
{
$attribute = $node->get('call:attribute');
$vars = $node->get('priv:url');
if(is_null($attribute))
{
$this->_buildCode($node, $vars, $node->get('priv:attribute'));
return;
}
// Ok, we have the opt:attribute instructions right there
$code = ' $_routing_'.(self::$_cnt).' = array(';
foreach($vars as $name => $value)
{
$code .= $name.' => '.$value.',';
}
$code .= '); ';
foreach($attribute as $attr)
{
$attr->set('nophp', true);
$code .= $attr->buildCode(Opt_Xml_Buffer::ATTRIBUTE_BEGIN);
$code .= ' $_routing_'.(self::$_cnt).'['.$attr->buildCode(Opt_Xml_Buffer::ATTRIBUTE_NAME).'] = '.$attr->buildCode(Opt_Xml_Buffer::ATTRIBUTE_VALUE).'; '.$attr->buildCode(Opt_Xml_Buffer::ATTRIBUTE_END);
}
$node->getParent()->addAfter(Opt_Xml_Buffer::TAG_BEFORE, $code);
// Create an attribute for the parent.
$attribute = new Opt_Xml_Attribute($node->get('priv:attribute'), null);
$attribute->addAfter(Opt_Xml_Buffer::ATTRIBUTE_VALUE, ' echo '.self::ROUTING_FUNCTION.'($_routing_'.(self::$_cnt).'); unset($_routing_'.(self::$_cnt).');');
$node->getParent()->addAttribute($attribute);
self::$_cnt++;
// Clean
$node->set('call:attribute', null);
$node->set('priv:url', null);
$node->set('priv:attribute', null);
} // end postprocessNode();
private function _buildCode(Opt_Xml_Node $node, Array $vars, $attributeName)
{
// Build the code
$code = 'echo '.self::ROUTING_FUNCTION.'( array (';
foreach($vars as $name => $value)
{
$code .= $name.' => '.$value.',';
}
$code .= '))';
// Create an attribute for the parent.
$attribute = new Opt_Xml_Attribute($attributeName, null);
$attribute->addAfter(Opt_Xml_Buffer::ATTRIBUTE_VALUE, $code);
$node->getParent()->addAttribute($attribute);
} // end _buildCode();
} // end Opt_Instruction_Url;
Manual installation instructions:
- Copy the file somewhere.
- Register the class in the autoloader:
Opl_Loader::mapAbsolute('Opt_Instruction_Url', './some/path'); - Register the instruction in OPT:
$tpl->register(Opt_Class::OPT_INSTRUCTION, 'Url'); - Be sure to modify the
ROUTING_FUNCTIONconstant so that it points to the router.
Plugin installation:
- Save the code above as
instruction.Url.phpin your OPT plugin directory. - Ensure that your script is configured to load plug-ins.
- Be sure to modify the
ROUTING_FUNCTIONconstant so that it points to the router. - Enjoy.
The usage
The instruction is really simple in use. Below you can find a template:
<a>
<opt:url foo="$foo" bar="$bar" />
Foo
</a>
This will give you (the exact address depends on your router):
<a href="/foo/value/bar/value/">
Foo
</a>
By default, the instruction creates href attribute, but you can freely use it for other purposes:
<form method="post">
<opt:url foo="$foo" bar="$bar" attribute="action" />
Foo
</form>
The instruction can also load the attributes from a section:
<a>
<opt:url str:foo="foo">
<opt:attribute name="$extra.name" value="$extra.value" opt:section="extra" />
</opt:url>
Foo
</a>

