Template Compilers

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:

Function: compile()
The 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']);

    }

  }
                    
From the sample 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.

Function: view()
This function is similar to the 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;

    }

  }
                    
From the sample both the viewed template files will be rendered and displayed. In most cases it is preferable to use this function within the window's self::load() method for final rendering. This will be discussed later.

Function: rex()
This function is a helper function that is similar to the 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']);

    }

  }
                    
From the sample the rex() function will render the content and return a string of the rendered component.

Function: rexx()
This 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']); 

    }

  }
                    
From the sample the rex() function will render the content and return a string of the rendered component.

Function: raw()
This function returns the raw contents of a file within the template directory. It is similar to the PHP's internal 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.

Method: self::load()
This Windows's 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']) 
      ); 

    }

  }
                    
From the sample the 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'
      ); 

    }

  }
                    
From the sample above the self::load() method will display "Hello world".