Function : Compile

Template compilers are of four types which are the compile(), view(), rex() and raw() functions. These functions are used to render rex template files through the compiler class.

Compile

The compile() function is the root function for rendering template files. Prior to version 2.0, it was not a stand alone function but after recent improvements, it is now capable of rendering template files alone using the Compiler engine. There are three ways by which this function can be used in window files to render template files. The sample of the usuage are shown below

Assuming we have a template file within the template directory "windows/Rex" named "home.rex.php" as below:
  The boy is {{ $age }} years today
                  

We can compile this home template file through the compiler function as shown below:
  <?php 

  namespace spoova/mi/windows/Routes/Home;

  use window;

  class Home extends Window { 


    function __construct() {

      // The boy is 15 years today
      self::load('home', fn() => compile(['age' => '15']));

    }


  }
                  

Automatically, the self::load() which is a window method tells the compiler to render the template file immediately as a page without using any print or echo word before it. In previous versions, this is the known way to render template files. While this approach will also work in recent versions, starting from version 2.0, we can now use the compiler as a stand alone function without depending on the the self::load() method as below:

version 2.0+
  <?php 

  namespace spoova/mi/windows/Routes/Home;

  use window;

  class Home extends Window { 


    function __construct() {

      // The boy is 15 years today
      echo compile('home', ['age' => '15']);

    }


  }
                  

In the above, the compiler returns a compiler object, when we print the compiler object, the template file becomes automatically rendered. We can also take advantage of this new feature to obtain the rendered component easily as shown below.

  <?php 

  namespace spoova/mi/windows/Routes/Home;

  use window;

  class Home extends Window { 


    function __construct() {

      $markup = compile('home', ['age' => '15'])->rex();

      vdump($markup); // view rendered markup

    }


  }
                  

Since the compile() function returns a Compiler object, there are couple of extended functions we can perform on the compile() function. Visit version 2.0 compiler class release notes to learn more about compiler object.