Template On The Go

In spoova, the terminal makes it easier to generate template formats for required files. However, when it comes to auto-generation of primary template files, spoova has an integrated feature that makes it much easier to generate template files which is known as Template-On-The-Go. This feature is an automatic template generation algorithm that is powered by the combined effect of Window (i.e Routes) and live server feature. The window simply directs the template engine to automatically generate a primary template file if a template path was accessed but it does not already exist within the project application. The PHP template file can be generated in different formats depending on the type of route instruction fowarded to the template engine. The examples below reveals the samples of how template files can be generated.
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.

Benefits of TOTG
Example: TOTG Benefits
The benefits of TOTG can be seen when working with sub routes of specific routes like the sample 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, 
            [ 
                'home' => 'home', //call home() method.  
                'home/user' => 'user', //call user() method. 
            ] 
        ); 

    }

    function home() {

        self::load('home.home'); //auto-generate home template

    }

    function user() {

        self::load('home.user'); //auto-generate user template

    }

  }
                
From the sample above, by declaring the addRex() at the top level of the route file, each method will easily generate its own template file if the file does not already exist within the specified template path. This makes it easier for developers to generate template files rather than using the terminal every time a template is expected to be generated. This reduces development time considerably and provides a more comfortable environment of development.