3

使用 Codeigniter 1.7.2 和 Dwoo 实现我的 CMS 时遇到问题。我使用 Phil Sturgeon Dwoo 库。我的问题是我希望用户从管理面板创建模板,这意味着所有模板都将存储到数据库中,包括所有 Dwoo 变量和函数。我的问题:

  1. 是否可以从数据库加载 dwoo 模板?
  2. 如何从数据库中解析 dwoo 变量或函数?我尝试从数据库中加载内容,其中包含 dwoo var 和函数,并且我尝试使用 dwooeval()函数和 phil sturgeon进行评估,string_parse()但仍然没有运气。

例如:

我的控制器

$data['header'] = "<h1>{$header}</h1>"; --> this could be loaded from database

$this->parser->parse('header',$data);

我的观点

{$header}

这是错误消息:

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message:  Undefined index:  header_title</p>
<p>Filename: compiled/805659ab5e619e094cac7deb9c8cbfb5.d17.php</p>
<p>Line Number: 11</p>

header_title 是从数据库加载的 dwoo 变量。

谢谢,

4

1 回答 1

1

这样做绝对是可能的,但出于性能原因,将模板作为文件存储在文件系统上可能会更快,即使它们是由用户编辑的。如果您精通文件命名,则可以避免对数据库的大部分访问。

无论如何,如果你真的想用数据库来做,下面应该工作:

// rendering the header
$template = '<h1>{$header}</h1>'; // loaded from db, don't use double-quotes for examples or $header will be replaced by nothing
$data = array('header' => 'Hello'); // loaded from db as well I assume
$headerOutput = $this->parser->parse_string($template, $data, true); // true makes it return the output

// now rendering the full page (if needed?)
$data = array('header' => $headerOutput);
$this->parser->parse('header', $data); // no 'true', so goes straight to output

然后视图将包含{$header}标题模板的输出并将其传递给该变量。

现在我不确定 CI 是如何工作的,所以最好只输出第一个模板的结果并完全跳过第二个模板,但我会留给你。

于 2010-12-25T10:38:44.263 回答