我在将使用 savant2 模板引擎的旧项目转换为 plate 模板引擎时遇到问题,我已经浏览了platephp文档,但仍然令人困惑。 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; ?>
现在,我想使用plate模板引擎代替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相同的结果
答案 0 :(得分: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应用程序,现在可以在印版模板引擎上运行