Search code examples
phptemplatestemplate-engineplatessavant3

Changing the template engine of a project from savant2 to plates


I am having a problem converting an old project that uses savant2 template engine to plates template engine, I have gone through the platesphp documentation, and it is still confusing. The project in savant2 template is structured this way(example), first file

$savant = new Savant2();
$savant->addPath('template', [LINK TO TEMPLATE OR THEME]);

Then in other file, where the needed variables are declared

global $savant;
$my_name = "Victor";
$savant->assign('name', $my_name);
$savant->display('include/header.tmpl.php');

Then, in the header.tmpl.php file

<?php echo $this->name; ?>

Now, I want to use plates template engine instead to replace savant2, this is how my code is now structure, first file

$plates  = new League\Plates\Engine();
$plates->addFolder('template', [LINK TO TEMPLATE OR THEME]);

In the other file,

global $plates;
$my_name = "Victor";
$plates->addData('name', $my_name);
$plates->render('include/header.tmpl.php');

Then, In the header.tmpl.php file

<?=$this->e($name)?>

Though it is not working as expected, my confusion lies in the use of render,addData, and addFolder to produce the same results as savant2


Solution

  • I finally solved it

        //Location of savant2 library
          require('/Savant2/Savant2.php');
        // set default template paths: 
          $savant = new Savant2();
          $savant->addPath('template', '/themes/');
    

    Replace the above, and add this instead

        // Enable the composer autoload file (Depending on how your system is set up)
         require_once '/vendor/autoload.php';
         $plates = League\Plates\Engine::create('/themes/', 'tmpl.php');
    

    Then for the below savant2 implementation

        require 'config.php';
    
        $name = 'Victor Alagwu';
        $school = 'University of Nigeria, Nsukka';
        $course = 'Computer Science';
        $savant->assign('author', $name);
        $savant->assign('school', $school);
        $savant->assign('course' $course);
        $savant->display(home.tmpl.php);
    

    Replace it with this plates implementation

        require 'config.php';
        $name = 'Victor Alagwu';
        $school = 'University of Nigeria, Nsukka';
        $course = 'Computer Science';
    
        plate['name'] = $name;
        plate['school'] = $school;
        plate['course'] = $course;
    
        echo $plates->render('home.tmpl.php', $plate);
    

    Then for the template files (Savant2)

           Name:
           <?php echo $this->name; ?>
           Course:
           <?php echo $this->course; ?>
           School: 
           <?php echo $this->school; ?>
    

    Replace with the below (For Plates)

        Name:
         <?php echo $name; ?>
        Course:
        <?php echo $course; ?>
        School: 
        <?php echo $school; ?>
    

    And there you have your previously savant2 app, now running on plates template engine