Template engine creation / loops and statements

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

I have a question... I don't really understand how template engines parse if/elseif/else statements, all kinds of loops/functions... I don't understand the logic behind that.

I'm trying to learn... my current project is taking me to new heights in programming and I'm trying to reinforce my PHP knowledge (instead of free-riding off of other programmer's backs).

I've already advanced far in the past few days and taken up a project (self-inflicted project :lol: ) to learn HMVC (Hierarchical Model-View-Controller).

Just trying to do everything right the first time, so I don't have to recode the system.

Back to my question though... how do template engines (like twig for instance) are able to parse statements, loops and that kind of stuff?

Thanks in advance.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

Counter-question: how would you try and do it?
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

Good question. I remember tackling this way back then when I was creating the templating engine I posted in PHP tutorials and I've tried 'str_replace' from and then eval'd it... but that didn't work.

For instance if I have in my template file:
Code: [ Select ]
<if>$variable != true</if>
// My Code
<elseif>$variable == 'apples'</elseif>
// My Code
</endif>
  1. <if>$variable != true</if>
  2. // My Code
  3. <elseif>$variable == 'apples'</elseif>
  4. // My Code
  5. </endif>

And if I parse it with the following
PHP Code: [ Select ]
<?php
$contents = preg_replace("/\<if\>(.*)\<\/if\>/", "<?php if($1) {", $contents);
$contents = preg_replace("/\<elseif\>(.*)\<\/elseif\>/", "} elseif($1) {", $contents);
$contents = str_replace("</endif>", "} ?>", $contents);
 
$contents = eval($contents);
?>
  1. <?php
  2. $contents = preg_replace("/\<if\>(.*)\<\/if\>/", "<?php if($1) {", $contents);
  3. $contents = preg_replace("/\<elseif\>(.*)\<\/elseif\>/", "} elseif($1) {", $contents);
  4. $contents = str_replace("</endif>", "} ?>", $contents);
  5.  
  6. $contents = eval($contents);
  7. ?>

If I echo before I eval, I get...
Quote:
<?php if($variable != true) {
// My Code
} elseif($variable == 'apples') {
// My Code
} ?>

And that would be shown in the background. But if I eval it, I get the following error.
Quote:
Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\www\hmvc\includes\template_engine.php(84) : eval()'d code on line 1

Of course, the entire file that is being eval'd is:
Code: [ Select ]
<html>
  <head>
    <title>Test View Template Engine</title>
  </head>
  <body>
    <p>This is test, huh? My Title</p>
    <p>Testing the test</p>
    
    
      <?php if($variable != true) {
      // My Code
      } elseif($variable == 'apples') {
      // My Code
      } ?>

  </body>
</html>
  1. <html>
  2.   <head>
  3.     <title>Test View Template Engine</title>
  4.   </head>
  5.   <body>
  6.     <p>This is test, huh? My Title</p>
  7.     <p>Testing the test</p>
  8.     
  9.     
  10.       <?php if($variable != true) {
  11.       // My Code
  12.       } elseif($variable == 'apples') {
  13.       // My Code
  14.       } ?>
  15.   </body>
  16. </html>

Maybe the HTML part is weirding it out, but I don't know.

I'm also guessing that even if it did eval'd fine, that is still not the right way to do that.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

I just went off on a different route...
PHP Code: [ Select ]
$match = array();
preg_match_all("/\<if\>(.*)\<\/if\>/", $contents, $match[1]);
preg_match_all("/\<\/if\>(.*)\<elseif\>/", $contents, $match[2]);
preg_match_all("/\<elseif\>(.*)\<\/elseif\>/", $contents, $match[3]);
preg_match_all("/\<\/elseif\>(.*)\<\/endif\>/", $contents, $match[4]);
print_r($match);
  1. $match = array();
  2. preg_match_all("/\<if\>(.*)\<\/if\>/", $contents, $match[1]);
  3. preg_match_all("/\<\/if\>(.*)\<elseif\>/", $contents, $match[2]);
  4. preg_match_all("/\<elseif\>(.*)\<\/elseif\>/", $contents, $match[3]);
  5. preg_match_all("/\<\/elseif\>(.*)\<\/endif\>/", $contents, $match[4]);
  6. print_r($match);

And I'm getting the following array...
Code: [ Select ]
Array
(
  [1] => Array
    (
      [0] => Array
        (
          [0] => <if>$variable != true</if>
        )

      [1] => Array
        (
          [0] => $variable != true
        )

    )

  [2] => Array
    (
      [0] => Array
        (
        )

      [1] => Array
        (
        )

    )

  [3] => Array
    (
      [0] => Array
        (
          [0] => <elseif>$variable == 'apples'</elseif>
        )

      [1] => Array
        (
          [0] => $variable == 'apples'
        )

    )

  [4] => Array
    (
      [0] => Array
        (
        )

      [1] => Array
        (
        )

    )

)
  1. Array
  2. (
  3.   [1] => Array
  4.     (
  5.       [0] => Array
  6.         (
  7.           [0] => <if>$variable != true</if>
  8.         )
  9.       [1] => Array
  10.         (
  11.           [0] => $variable != true
  12.         )
  13.     )
  14.   [2] => Array
  15.     (
  16.       [0] => Array
  17.         (
  18.         )
  19.       [1] => Array
  20.         (
  21.         )
  22.     )
  23.   [3] => Array
  24.     (
  25.       [0] => Array
  26.         (
  27.           [0] => <elseif>$variable == 'apples'</elseif>
  28.         )
  29.       [1] => Array
  30.         (
  31.           [0] => $variable == 'apples'
  32.         )
  33.     )
  34.   [4] => Array
  35.     (
  36.       [0] => Array
  37.         (
  38.         )
  39.       [1] => Array
  40.         (
  41.         )
  42.     )
  43. )

For some reason that is not finding the stuff between </endif> and </elseif> and between <elseif> and </if>...

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 119 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.