The rex template engine uses template compiler functions and integrated window (Route) methods to render primary PHP template files from the windows/Rex
default template directory. These functions and methods are discussed below:
compile() returns an object of the compiler class. The Compiler class was introduced in v2.6 of the framework which
controls how template files are rendered. It is the main compiler function used to render template files. An example of its usage is shown below:
<?php
use Routes;
class Home extends Routes {
function __construct() {
self::call($this, [
'home' => 'home'
]);
}
function home() {
echo compile('path.file', ['var'=>'value']);
}
}
Home route above, the home() method is called when the
relative url "home" is visited. The compile() function will however, render
the supplied file using the subpath of the window/Rex supplied as the first argument. The second argument will
however, parse arguments into the template file. The variables can then be accessed using the placeholder {{}} directive.
compile() function except that it returns the markup string of the template rendered. Unlike the
compile() function, it is mostly used to render multiple template files.
<?php
use Routes;
class Home extends Routes {
function __construct() {
self::call($this, [
'home' => 'home'
]);
}
function home() {
$view1 = view('path.file1');
$view2 = view('path.file2');
echo $view1.$view2;
}
}
self::load() method for final rendering. This will be discussed later.
compile() function.
However, the main difference is that unlike the compile() which returns object,
the rex() function returns a string.
<?php
use Routes;
class Home extends Routes {
function __construct() {
self::call($this, [
'home' => 'home'
]);
}
function home() {
echo rex('path.file', ['var' => 'value']);
}
}
rex() function will render the content and return a string of the rendered
component.
rexx is a helper function that is similar to the rex() function and also an
alias to the rexcompile() function. Unlike the rex() which returns a string, this A
function performs all the rendering and displaying of rendered components internally. An example of its usage is shown below:
<?php
use Routes;
class Home extends Routes {
function __construct() {
self::call($this, [
'home' => 'home' # {domain}/home
]);
}
function home() {
// render and display contents
rexx('path.file', ['var' => 'value']);
}
}
rex() function will render the content and return a string of the rendered
component.
file_get_contents() function except that the directory is
specific to the windows/Rex directory. This function may be useful in helping to
view the original state of the template markup before it is being rendered.
self::load() method is used within Routes
to load templates files similarly to the compile() function.
The sample of its usage is shown below
<?php
use Routes;
class Home extends Routes {
function __construct() {
self::call($this, [
'home' => 'home' # {domain}/home
]);
}
function home() {
// render and display contents
self::load('path.file',
fn() => compile(['var' => 'value'])
);
}
}
self::load() method will render the content and return a string of the rendered
component through the compile() function. As seen above, the compile()
function must be used to render the components before they can be properly loaded. We can also supply a normal text to this window method
for display. This is shown below.
<?php
use Routes;
class Home extends Routes {
function __construct() {
self::call($this, [
'home' => 'home' # {domain}/home
]);
}
function home() {
// render and display contents
self::load('path.file',
fn() => 'Hello world'
);
}
}
self::load() method will
display "Hello world".