【问题标题】:Codeigniter + DwooCodeigniter + Dwoo
【发布时间】:2011-05-30 08:16:17
【问题描述】:

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

  1. 是否可以从数据库加载 dwoo 模板?
  2. 如何从数据库中解析 dwoo 变量或函数?我尝试从包含 dwoo var 和函数的数据库中加载内容,我尝试使用 dwoo eval() 函数和 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 是从 DB 加载的 dwoo 变量。

谢谢,

【问题讨论】:

    标签: templates codeigniter dwoo


    【解决方案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 是如何工作的,所以最好只输出第一个模板的结果并完全跳过第二个模板,但我会留给你。

    【讨论】:

    • 谢谢seldaek,我现在就试试,让你知道结果。
    • 嗨 Seldaek,它不起作用。我不知道问题可能来自 phil 的图书馆,我会先尝试检查图书馆。
    • 知道了!!!我忘记了我的 dwoo 变量是 $header_title 而不是 $header。我的错,谢谢 seldaek
    • 感谢 Seldaek 为我报道,Google 快讯只给我发了一封关于这个的电子邮件。 >.
    • @RedTruck:很高兴你让它工作。如果您有时间接受我的回答作为您问题的解决方案,那就太好了:)
    猜你喜欢
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    • 2014-02-16
    • 2011-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多