Q: if and foreach in a templating engine I'm working on a templating engine (I don't want to use an already existing one) and it works fine. This is the code I 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 as $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 already exists!';
        } else {
            $this->vars[$key] = $value;
        }
    }

    public function append($key, $value) {
        $this->vars[$key] .= $value;
    }

    public function setPage($page) {
        $this->page = $page;
    }

}

but I was wondering how I could add if & elses and foreach loops to the engine. I want it to look something like this in the html:
<!DOCTYPE html>
<html>
    <head></head>        
    <body>
        [@if {bool} == true][ <!-- different expressions like {int} > 2 should work too -->
            <div>{text}</div>
        ]
        [@else][
            <div> error </div>
        ]

        [@foreach {jsLinks} as {jsLink}][
            <script type="text/javascript" src="{jsLink}"></script>
        ]
    </body>
</html>

the template files are saved as .php files, so I could do <?php if($bool == true){?> some html <?php  }?>,
but I don't want to use php code in the templating files.
Can anyone please help me with this?
EDIT
I added this code:
$content = preg_replace('~\{LOOP:(\w+)\}~', '<?php foreach ($this->vars[\'$1\'] as $ELEMENT): $this->wrap($ELEMENT); ?>', $content);
$content = preg_replace('~\{ENDLOOP:(\w+)\}~', '<?php $this->unwrap(); endforeach; ?>', $content);

and these two functions:
private function wrap($element){
    $this->stack[] = $this->vars;
    foreach ($element as $k => $v) {
        $this->vars[$k] = $v;
    }
}

private function unwrap() {
    $this->vars = array_pop($this->stack);
}

but this just echoes <?php foreach ($this->vars[\'$1\'] as $ELEMENT): $this->wrap($ELEMENT); ?> and <?php $this->unwrap(); endforeach; ?> literally in the browser. Any way to fix this?
EDIT: I did it!
I can now use foreach loops and ifs and elses in my templates. My 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\'] as $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 as $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 already 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 as 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 some text
        {ENDIF}

        <!-- if-else statement -->        
        {IF: {number} > 2}
        number is bigger than 2
        {ELSE}
        number is smaller than 2
        {ENDIF}

    </body>
</html>


A: The only way is to write converter. For example, parse template and convert [@if {bool} == true] to <?php if ($bool == true): ?>. 
