0

I'm creating a system where users can create their own themes, styles, and templates. I'm storing their page templates in a MySQL database and currently I've written a small replace script that uses tags so the user can define where content should go in their template elements. The script replaces their tags with their content during when the page is rendered.

What I want to do now is add the ability for them to define a section conditionally - so for example they might have an element in their template that looks like this:

 <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>

Replacing the titlebar heading and subheading with the required values is no problem, but I can't figure out how to do the following and replace the conditionals with PHP if statements and have it process.

 <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>

Basically I would want to replace:

 @titlebar?subheading 
 @end?

with:

if($titlebar->subheading){ }

and process through PHP appropriately. Any suggestions would be appreciated.

halfer
  • 19,824
  • 17
  • 99
  • 186
Joshua
  • 791
  • 9
  • 24
  • something wrong with all the existing temple engines? –  Sep 19 '13 at 21:41
  • Not to mention php itself can be a templating engine, no sense to re-invent the wheel: http://stackoverflow.com/a/4979198/398519 – Jim Sep 19 '13 at 21:45
  • I'm trying to figure out how to let a user store conditions within their own templates which are stored in mysql. It would be easy if I were having them define templates as files, but I'm allowing them to make a page more modular so to speak. – Joshua Sep 19 '13 at 21:58
  • I'm sure with the necessary view logic, Smarty or Twig could be set up to do what you want. You'd then have a robust and well-tested templating system, with less work to do. – halfer Sep 19 '13 at 22:11
  • if you want to use `if () {}`, then you have to start parsing your template, not just replacing the values, and believe me, it's not gonna be easy ... – Mahdi Sep 20 '13 at 14:05

2 Answers2

0

Look for the question mark, then the NEXT @end?, then remove the text between the two if it doesn't exist.

Something along the lines of:

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];
    }
}

Obviously this is extremely naive, doesn't handle recursion, could probably be simplified with regular expressions, etc.

However, this is the basic concept.

rockerest
  • 10,412
  • 3
  • 37
  • 67
0

I appreciate all the input. I decided the best way to integrate this was to use Mustache. I'm still using my custom tags and sending a tag array to Musatche to process. Here's how I use it if anyone is interested.

If this template is stored in my MySql db.

 <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>

I simply retrieve it and store it in an $content variable and process it like this.

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

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

I then simply send $rendered to my view and it works like a charm. I decided to use Mustache as it can be used with Javascript as well so it will have future use in my application design beyond just user based php templating.

Joshua
  • 791
  • 9
  • 24