Automatic Template Generation
Example: PHP Templates
The easiest way to generate a template file in route is to use the
addRex() method before calling the
load() method. An example is shown below:
<?php
namespace spoova\mi\windows\Routes;
use Routes;
class Home extends Routes {
function __construct() {
self::addRex(); //generate template file if it does not exist.
self::call($this, [ lastUrl() => 'home'] ); //call home() method in url: home.
}
function home() {
self::load('some-template'); //auto-generate unknown template
}
}
From the sample above, the directive
addRex() supplied will force the
self::load() to generate the template file path if it does not already exist
in the windows/Rex template directory. The template pattern generated will resemble
an html file pattern as shown below:
<!DOCTYPE html>
<html lang="en">
<head>
@load('headers')
@live
</head>
<body>
</body>
</html>
Template files generated in the format above usually comes with an
@live
directive as shown above, meaning that the file is generated on a live state. In certain situations, we may
need to generate a template file through inheritance from a parent template. This situation requires specifying the parent template path within the
addRex() method as shown below:
<?php
namespace spoova\mi\windows\Routes;
use Routes;
class Home extends Routes {
function __construct() {
self::addRex('build.main.file'); # windows/Rex/build/main/file.rex.php
self::call($this, [ lastUrl() => 'home'] ); //call home() method in url: home.
}
function home() {
self::load('some-template'); //auto-generate unknown template
}
}
As in the case above, the template will be automatically generated in the specified template path using the format below:
@template('build.main.file')
@template;
In the sample above, notice that the
live() directive is not added. This is because the
file is inheriting from a parent template which may already have a live server script. If the parent template already has the
live server script, we can easily turn it off manually in the generated template using the :off
directive.