0

我正在创建一个系统,用户可以在其中创建自己的主题、样式和模板。我将他们的页面模板存储在 MySQL 数据库中,目前我已经编写了一个使用标签的小型替换脚本,以便用户可以定义内容应该在他们的模板元素中的位置。该脚本在页面呈现期间将其标签替换为其内容。

我现在要做的是为他们添加有条件地定义部分的能力 - 例如,他们的模板中可能有一个如下所示的元素:

 <h1 class="entry-title">
      <span class="top-left-ribbon"></span>
      @titlebar>heading 
      <span class="sub-heading">@titlebar>subheading</span>
      <span class="right-ribbon"></span>
 </h1>

用所需的值替换标题栏标题和副标题没有问题,但我不知道如何执行以下操作并用 PHP if 语句替换条件并让它处理。

 <h1 class="entry-title">
      <span class="top-left-ribbon"></span>
      @titlebar>heading 
 @titlebar?subheading
      <span class="sub-heading" >@titlebar>subheading</span>
      <span class="right-ribbon"></span>
 @end?
 </h1>

基本上我想替换:

 @titlebar?subheading 
 @end?

和:

if($titlebar->subheading){ }

并适当地通过PHP进行处理。任何建议,将不胜感激。

4

2 回答 2

0

我感谢所有的意见。我决定整合它的最佳方法是使用 Mustache。我仍在使用我的自定义标签并将标签数组发送给 Musatche 进行处理。如果有人感兴趣,这就是我如何使用它。

如果此模板存储在我的 MySql 数据库中。

 <h1 class="entry-title">
      <span class="top-left-ribbon"></span>
      {{ @titlebar>heading }}
 {{# @titlebar>subheading }}
      <span class="sub-heading">{{ @titlebar>subheading }}</span>
      <span class="right-ribbon"></span>
 {{/ @titlebar>subheading }}
 </h1>

我只需检索它并将其存储在 $content 变量中并像这样处理它。

 $tags = [
          "@titlebar>heading"=>$titlebar->heading,
          "@titlebar>subheading"=>$titlebar->subheading
         ];

 $m = new Mustache_Engine;
 $rendered = $m->render($content,$tags);

然后我只需将 $rendered 发送到我的视图,它就像一个魅力。我决定使用 Mustache,因为它也可以与 Javascript 一起使用,因此它将在我的应用程序设计中使用,而不仅仅是基于用户的 php 模板。

于 2013-09-20T14:30:20.550 回答
0

查找问号,然后查找 NEXT @end?,然后删除两者之间的文本(如果不存在)。

类似于以下内容:

if( strpos($theUsersStringThatStartsWithAtSymbol, "?") > 2 ){
    // parse the string for the pieces, assuming you already have something for this
    $parts = explode( "@end?", $theRestOfTheContentAfterTheBeginningConditional );

    // check the DB for the pieces they want
    if( $piecesExist ){
        echo $parts[0];
    }
}

显然这是非常幼稚的,不处理递归,可以用正则表达式等来简化。

但是,这是基本概念。

于 2013-09-19T21:50:08.000 回答