feulf / raintpl3

The easiest Template Engine for PHP
https://feulf.github.io/raintpl
258 stars 57 forks source link

modifierReplace : whitespace param error #144

Closed BarryDam closed 9 years ago

BarryDam commented 10 years ago

I had an error when I did the following:

 $t = new Tpl;
 $t->assign('arraytest',array('this', 'is', 'a', 'test');
 $t->draw('test');

In the template I tried to implode the array with a whitespace:

{$arraytest|implode:" "}

But the result threw an error:

Parse error: syntax error, unexpected ';'.......

So I added and \s to the preg_match on line 610 of the Parser.php file.

 protected function modifierReplace($html) {

        $this->blackList($html);
        if (strpos($html,'|') !== false && substr($html,strpos($html,'|')+1,1) != "|") {
            preg_match('/([\$a-z_A-Z0-9\(\),\[\]"->]+)\|([\$a-z_A-Z0-9\(\):,\[\]"->\s]+)/i', $html,$result);

            $function_params = $result[1];
            $explode = explode(":",$result[2]);
            $function = $explode[0];
            $params = isset($explode[1]) ? "," . $explode[1] : null;

            $html = str_replace($result[0],$function . "(" . $function_params . "$params)",$html);

            if (strpos($html,'|') !== false && substr($html,strpos($html,'|')+1,1) != "|") {
                $html = $this->modifierReplace($html);
            }
        }

        return $html;
    }

Result:

this is a test

Barry

leonardfischer commented 10 years ago

This works fine for me :+1:

I had this issue:

{$number|number_format:2,'.',' '}

Resulting in a syntax error

<?php echo htmlspecialchars( number_format($total,2,'.',') ', ENT_COMPAT, 'UTF-8', FALSE ); ?>
feulf commented 9 years ago

Awesome! Thanks.