php - if and foreach in a templating engine -


i'm working on templating engine (i don't want use existing one) , works fine. code have now:

class template {      private $vars = array();     public $page;      function __construct() {     }      public function render($render = false) {             $page = $this->page . '.php';             $content = file_get_contents($page);              foreach ($this->vars $key => $value) {                 $tag = '{' . $key . '}';                 $content = str_replace($tag, $value, $content);             }              if ($render)                 echo $content;             else                 return $content;     }      public function assign($key, $value, $overwrite = false) {         if ($overwrite) {             $this->vars[$key] = $value;         } else if (isset($this->vars[$key])) {             echo 'this var exists!';         } else {             $this->vars[$key] = $value;         }     }      public function append($key, $value) {         $this->vars[$key] .= $value;     }      public function setpage($page) {         $this->page = $page;     }  } 

but wondering how add if & elses , foreach loops engine. want in html:

<!doctype html> <html>     <head></head>             <body>         [@if {bool} == true][ <!-- different expressions {int} > 2 should work -->             <div>{text}</div>         ]         [@else][             <div> error </div>         ]          [@foreach {jslinks} {jslink}][             <script type="text/javascript" src="{jslink}"></script>         ]     </body> </html> 

the template files saved .php files, <?php if($bool == true){?> html <?php }?>, don't want use php code in templating files. can please me this?

edit

i added code:

$content = preg_replace('~\{loop:(\w+)\}~', '<?php foreach ($this->vars[\'$1\'] $element): $this->wrap($element); ?>', $content); $content = preg_replace('~\{endloop:(\w+)\}~', '<?php $this->unwrap(); endforeach; ?>', $content); 

and these 2 functions:

private function wrap($element){     $this->stack[] = $this->vars;     foreach ($element $k => $v) {         $this->vars[$k] = $v;     } }  private function unwrap() {     $this->vars = array_pop($this->stack); } 

but echoes <?php foreach ($this->vars[\'$1\'] $element): $this->wrap($element); ?> , <?php $this->unwrap(); endforeach; ?> literally in browser. way fix this?

edit: did it!

i can use foreach loops , ifs , elses in templates. template class:

<?php  class template {      private $vars = array();     public $page;     public $view;     public $bdisplay = true;      function __construct($view = false) {         $this->view = $view;     }      public function render($render = false) {         if ($this->bdisplay === true) {             $page = path_views . $this->page . '.php';             $page = (empty($this->page) ? ($this->view ? path_views . $this->view . ds . 'index.php' : path_views . 'index' . ds . 'index.php') : (file_exists($page) ? $page : path_views . 'index' . ds . 'index.php'));             $content = file_get_contents($page);              $content = $this->replace($content, $this->vars);              $content = preg_replace('~\{loop:(\w+)\}~', '<?php foreach ($this->vars[\'$1\'] $value){ echo  $this->replace(\'', $content);             $content = preg_replace('~\{endloop:(\w+)\}~', '\', $value);} ?>', $content);              $content = preg_replace('~\{if:([^\r\n}]+)\}~', '<?php if ($1): echo \'', $content);             $content = preg_replace('~\{else\}~', '\'; else: echo \'', $content);             $content = preg_replace('~\{endif\}~', '\'; endif; ?>', $content);               if ($render)                 eval('?>' . $content . '<?php');             else                 return $content;         }         else {             return false;         }     }      private function replace($content, $vars) {         foreach ($vars $key => $value) {             if (!is_array($vars[$key])) {                 $tag = '{' . $key . '}';                 $content = str_replace($tag, $value, $content);             }         }          return $content;     }      public function assign($key, $value, $overwrite = false) {         if ($overwrite) {             $this->vars[$key] = $value;         } else if (isset($this->vars[$key])) {             echo 'this var exists!';         } else {             $this->vars[$key] = $value;         }     }      public function append($key, $value) {         $this->vars[$key] .= $value;     }      public function addarray($array, $key, $value) {         $this->vars[$array][][$key] = $value;     }      public function setpage($page) {         $this->page = $page;     }  }  ?> 

with example template:

<!doctype html> <html>     <head></head>     <body>         <!-- foreach loops -->         {loop:js}         <script type="text/javascript" src="{link}"></script>         {endloop}          <!-- if statement -->                 {if: {bool} === true}         display text         {endif}          <!-- if-else statement -->                 {if: {number} > 2}         number bigger 2         {else}         number smaller 2         {endif}      </body> </html> 

the way write converter. example, parse template , convert [@if {bool} == true] <?php if ($bool == true): ?>.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -