【问题标题】:Changing the template engine of a project from savant2 to plates将项目的模板引擎从 savant2 更改为 Plates
【发布时间】:2018-05-25 09:16:58
【问题描述】:

我在将使用 savant2 模板引擎的旧项目转换为 plates 模板引擎时遇到问题,我已经浏览了platesphp 文档,但仍然令人困惑。 savant2 模板中的项目是这样组织的(示例),第一个文件

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

然后在其他文件中,声明需要的变量

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

然后,在 header.tmpl.php 文件中

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

现在,我想用plates模板引擎代替savant2,这就是我的代码现在的结构,第一个文件

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

在另一个文件中,

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

然后,在 header.tmpl.php 文件中

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

虽然它没有按预期工作,但我的困惑在于使用 render、addData 和 addFolder 来产生与 savant2 相同的结果

【问题讨论】:

    标签: php templates template-engine plates savant3


    【解决方案1】:

    我终于解决了

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

    替换上面的,然后添加这个

        // 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');
    

    那么对于下面的 savant2 实现

        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);
    

    用这个板块实现替换它

        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);
    

    然后是模板文件(Savant2)

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

    替换为以下(用于板)

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

    你以前的 savant2 应用程序现在在板块模板引擎上运行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-19
      • 2012-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      相关资源
      最近更新 更多