gpt4 book ai didi

php - 使用自定义模板引擎如何用 php 替换内容

转载 作者:行者123 更新时间:2023-11-28 00:13:35 29 4
gpt4 key购买 nike

所以,我有一个我制作的模板引擎,我想用 PHP 文件中的内容替换 {pageBody},当我使用模板引擎执行它时,它不会执行 PHP,而是在查看源选项中显示它.

模板.PHP

<?php
class TemplateLibrary {
public $output;
public $file;
public $values = array();

public function __construct($file) {
$this->file = $file;
$this->file .= '.tpl';
$this->output = file_get_contents('templates/'.$this->file);
}

public function replace($key, $value)
{
$this->values[$key] = $value;
}

public function output() {
foreach($this->values as $key => $value)
{
$ttr = "{$key}";
$this->output = str_replace($ttr, $value, $this->output);
}
return $this->output;
}
}

index.tpl
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>{page_title}</title>
{page_style}
</head>

<body>
{page_header}
{page_body}
{page_footer}
</body>
</html>

HTML 替换工作正常,但 PHP 不工作。有什么想法吗?

最佳答案

您的文件相当分散,列表不完整。我已在下方列出了您目前提供的所有文件。

@Jonathon 上面是正确的,您将需要使用输出缓冲来捕获 PHP 文件的输出和 include() 文件(以便执行)而不是使用 file_get_contents()(不执行文件)。

[edit] 我在本地环境中重新创建了所有这些文件,并确认@Jonathon 的建议完美无缺。我更新了 dev/replacements.php 以包含建议的代码。

此外,我向您的 TemplateLibrary 类添加了另外两个函数:replaceFile($key, $filename) 执行 file_get_contents($filename) 这样您就不必经常重复它,并且 replacePhp($key, $filename) 在捕获输出时执行 include() ,所以您可以封装包含 PHP 文件的复杂性。

祝你好运!

ma​​in.php

<?php
require_once 'dev/dev.class.php';
require_once 'dev/templatelibrary.php';
$dev = new dev('netnoobz-billing');
$dev->loadLib('JS', 'js', 'jquery');

// template library and required files
$template = new TemplateLibrary('index');
require_once 'dev/replacements.php';


echo $template->output();

dev/templatelibrary.php

<?php
class TemplateLibrary {
public $output;
public $file;
public $values = array();

public function __construct($file) {
$this->file = $file;
$this->file .= '.tpl';
$this->output = file_get_contents('templates/'.$this->file);
}

public function replace($key, $value)
{
$this->values[$key] = $value;
}

public function replaceFile($key, $filename)
{
$this->values[$key] = file_get_contents($filename);
}

public function replacePhp($key, $filename)
{
ob_start();
include($filename);
$data = ob_get_clean();
$this->values[$key] = $data;
}

public function output() {
foreach($this->values as $key => $value)
{
$ttr = "{$key}";
$this->output = str_replace($ttr, $value, $this->output);
}
return $this->output;
}
}

index.tpl

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>{page_title}</title>
{page_style}
</head>

<body>
{page_header}
{page_body}
{page_footer}
</body>
</html>

replacements.php

<?php
$configStyleSheet = '<style type="text/css">'
. file_get_contents('styles/default/main.css')
. '</style>';
$pageHeader = file_get_contents('templates/header.tpl');
$pageFooter = file_get_contents('templates/footer.tpl');

#$pageBody = file_get_contents('loaders/pageBody.php');
ob_start();
include('loaders/pageBody.php');
$pageBody = ob_get_clean();

$template->replace('{page_style}' , $configStyleSheet);
$template->replace('{page_title}' , 'NetBilling');
$template->replace('{page_header}', $pageHeader);
$template->replace('{page_footer}', $pageFooter);
$template->replace('{page_body}' , $pageBody);

loaders/pageBody.php

<?php echo 'test'; ?>

[edit] 从 OP 的评论中添加了 loaders/pageBody.php

[编辑] 更新了 dev/replacements.php 以捕获输出缓冲区并在 .php 上使用 include

关于php - 使用自定义模板引擎如何用 php 替换内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13593714/

29 4 0
文章推荐: javascript - 制作
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com