2.0+
Compiler Class
The spoova's compiler class is introduced in version 2.0 in order to improve rex template engine by providing an easier and much flexible way of rendering template files. The major effect of this is to be able to add an extended control for template files. The following below are the useful subjects that be discussed separately in order to have a full understnding of what the Compiler object does.

  • Compiler instantiation
  • Compiler for string
  • Fetching rendered content
  • Viewing rendered content
  • Compiler improvements
  • Compiler helper functions

Compiler Instantiation
The compiler object initialization syntax is as shown below
  use spoova/mi/core/classes/Compiler;

  $Compiler = new Compiler($filepath, $vargs);


  where:
  
    $filepath : optional rex file path 
    $vargs    : optional variable arguments  

    
Example 1
  use spoova/mi/core/classes/Compiler;

  $Compiler = new Compiler('user.home', ['name' => foo]);

                                
In the example above, the compiler was initialized with a rex file path "user.home" and variable arguments were supplied using array as the second parameter which can the be accessed using the placeholder template directive (i.e {{ $name }} ) . Although, arguments are supplied above, we don't necessarily need to instantiate the Compiler class with objects. The setFile() method can be used to set or update the file name while the setArgs() can be used to set or update arguments. This is shown below:
Example 2
  use spoova/mi/core/classes/Compiler;

  $Compiler = new Compiler;

  $Compiler->setFile('user.home');
  $Compiler->setArgs(['name' => foo]);
                                
In the example 2 above, the setFile() and setArgs() are both used to set or update the file path and file arguments respectively. Both of these can also be defined using the compile() method instead. This is shown below:

Example 3
  use spoova/mi/core/classes/Compiler;

  $Compiler = new Compiler;

  $Compiler->compile('user.home', ['name' => foo]);
                                
In example 3 above, the compiler method does not necessarily need to follow the predefined order. The arguments may be switched interchangeably such that arguments come before the file name, however, both arguments cannot be of the same data type. One must be a string which defines the file path while the others is an array which defines the variable arguments.


Compiler For Strings
One of the major feature of the compiler class is the ability to compile strings only into file path. This means that rather than a real template file, instead, a string content is supplied for rendering. The string will be compiled and added into a defined template file for viewing. This can be achieved through the sample procedure below
  use spoova/mi/core/classes/Compiler;

  $Compiler = new Compiler;

  $Compiler->body($body);

  $Compiler->setBase($storage_path); //set a storage path for rendered string

 where :

  $body         : a string content to be rendered. 
  $storage_path : a unique subdirectory in "core/storage" directory. 

    
Example 5
  use spoova/mi/core/classes/Compiler;

  $Compiler = new Compiler();

  $Compiler->setBase('folder_name');

  $Compiler->body('This is a body content');
                                
In the example above, the compiler will assume to render the string as a raw content into the specfied folder name. It is important to note that setBase() method is used by compiler to specify folder name where rendered contents are expected to be stored. This is usually a subdirectory of the "core/storage" directory. It is important to keep rendered strings path as a separate path from other basic files to avoid conflicting files.


Fetching Rendered Content
When a file or string is rendered, the contents can be obtained using the rex() method which returns a rendered string content. This is shown below
  <?php

  namespace spoova/mi/windows/Routes;

  use spoova/mi/core/classes/Compiler;

  class Home { 

      function __construct() {

        $Compiler = new Compiler();

        $Compiler->compile('file');

        $rex = $Compiler->rex();

        vdump($rex); //variable dump content

      }

  }
    

Viewing Rendered Content
There are couple of ways to print the rendered components. However, the displaying of rendered components is best understood by printing the object of the class itself. This means that any of the methods of the Compiler class that returns the Compiler object instance will be able to display the rendered contents as long as all essential parameters have be properly defined. An example below is a short way of displaying a rendered file.
  <?php
  
  namespace spoova/mi/windows/Routes;

  use Window;
  use spoova/mi/core/classes/Compiler;

  class Home extends Window { 

      function __construct() {

        echo new Compiler('file', ['name'=> 'foo']);

      }

  }
    
In the example above, the compiler object will be able to display the rendered component on the webpage when the relative page is visited. We can also make use of the compiler helper function "compile()" rather than the class itself


Compile Improvements
One of the major improvements to the compiler function is that previously it does not exist as a stand-alone function which means that it needs to be used within the Res::load() or a window's "self::load()" method. The new updates now ensures that the compile() function can exist as a stand alone function.
  <?php
  
  namespace spoova/mi/windows/Routes;

  use Window;

  class Home extends Window { 

      function __construct() {

        echo compile('sample', ['name'=> 'foo']);

      }

  }
    
In the example above, the compile function will render the sampe file and the content will be printed to the page just like the Compiler class does. This behaviour is because the compile function now returns a compiler object.


Compiler Helper Functions
Aside from the "compile()" function, new functions have been introduced to help render files in a specifically designed way. These functions include raw() and rex() functions.

Function: raw()
This function is used to obtain the direct raw content of a rex file before it is rendrered. The example of its usage is displayed below
  <?php
  
  namespace spoova/mi/windows/Routes;
  
  use Window;
  
  class Home extends Window { 
  
      function __construct() {
  
         $raw = raw('sample'); //source code
         vdump($raw);
  
      }
  
  }
      
In the example above, function will return a raw string content of the rex sample file expected to be within the "window/Rex" directory.
Function: rex()
This function is used to obtain the string content of a rendered rex file. The example of its usage is displayed below
  <?php
        
  namespace spoova/mi/windows/Routes;

  use Window;
  
  class Home extends Window { 
  
      function __construct() {
  
         $rex = rex('sample'); //return rendered content
         vdump($rex);
  
      }
  
  }
      
In the example above, function will return a string content of the rex file expected to be within the "window/Rex" directory. We can also return a string content into the rex file through the second argument as shown below:
  <?php
  
  namespace spoova/mi/windows/Routes;
  
  use Window;

  class Home extends Window { 
  
      function __construct() {
  
         $rex = rex('sample', fn() => 'My url is domurl()'); //return rendered content of the string    
         vdump($rex);
  
      }
  
  }
      
Just like the window::load() method, we can also use the "compile()" function as a directive to compile the sample file and pass arguments to the file instead of using the "sample" file as an anchor for a string content like the previous example.
  <?php

  namespace spoova/mi/windows/Routes;

  use Window;
  
  class Home extends Window { 
  
      function __construct() {
  
         $rex = rex('sample', fn() =>compile(['name'=>'foo'])); //return rendered content of the string    
         vdump($rex);
  
      }
  
  }