URL routing with OPT
From Invenzzia wiki
One of the most common tasks in the templates is the URL generation. When you are using a framework, you should get a utility class called router that generates an appropriate URL from the parameters, according to the routing rules. It would be nice to add it directly to OPT. It is a quite easy task. Let's take a look at Kohana Framework - the exact choice does not matter, because the general rules are the same over most of the popular frameworks. We have there some static methods in the URL helper:
-
url::base()- returns the base URL of a website. -
url::site()- returns the URL to the action specified in the argument as a string.
Let's simply register them as functions:
$tpl = new Opt_Class;
...
$tpl->register(Opt_Class::PHP_FUNCTION,
array('baseUrl' => 'url::base',
'url' => 'url::site'));
$tpl->setup();
Now we can generate the URL's directly in the templates:
<a parse:href="url('article/'~$article.id~'/edit')">Edit article</a>
For more sophisticated routers that allow to read the parameters from arrays, you can use the Opt_Instruction_Url instruction that can be downloaded from this wiki. All you have to do is to specify the router method name in the source code and start using it:
<form method="post">
<opt:url attribute="action" str:controller="article" str:action="edit" id="$article.id" />
...
</form>

