php - Adding conditional statements to engine -


i trying add ability use conditional statements basic engine trying develop , cannot figure out why not work. can help? not replacing text.

here template.php conditional statements

<?php  class template {     private $vars = array();      public function assign($key, $value) {         $this->vars[$key] = $value;     }      public function render($file_name) {         $path = $file_name . '.html';          if (file_exists($path)) {              $content = file_get_contents($path);              foreach ($this->vars $key => $value) {                 $content = preg_replace('/\{' . $key . '\}/', $value, $content);             }              $content = preg_replace('/\{if (.*)\}/', '<?php if ($1): ?>', $content);             $content = preg_replace('/\{elseif (.*)\}/', '<?php elseif ($1): ?>', $content);             $content = preg_replace('/\{else\}/', '<?php else: ?>', $content);             $content = preg_replace('/\{\/if\}/', '<?php endif; ?>', $content);              eval(' ?>' . $content . '<?php ');          } else {             exit('<h4>engine error...</h4>');         }     } }  ?> 

and here implementation in html

<div class="container">             <div id="content">                 <h3>{pagetitle}</h3>                 <hr />                 <span>my name {username} , {age} years old</span>                  {if ({age}==21)}                     21                 {elseif ({age}==22)}                     22                 {else}                     none                 {/if}             </div> </div> 

it literally prints out conditional block above see inline

i guess need make of regex patterns ungreedy.

doing this:

/\{if (.*)\}/ 

will replace between first instance of {if , last instance of } replacement. make match ungreedy using u flag in pattern this:

/\{if (.*)\}/u 

Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -