feulf / raintpl3

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

'Ternary' regex in parser.php wrong #119

Open lllopo opened 10 years ago

lllopo commented 10 years ago

Hi, the second ternary operator parser regex : {(.[^{?]?)\?(.?):(.*?)} is wrong (parser.php, line 59). The problem is, that is also matches PHP code inserted by plugins that parsed the template with beforeParse. Example :

{$data.User.something}<a href="<?php echo static::$conf['base_url']; ?>users/{$data.User.otherthing}

The code above gets matched in total although it is not a ternary operator, because it has a '{' for a start a '}' for end and it has a '?' and ':' in it.

I believe this is a fundamental problem that need fixing, since a simple string like this :

{$startmeup} How are you ? Please, choose : one or two. {$endmenow}

will also get matched.

lllopo commented 10 years ago

Maybe this : {(.[^{}?]?)\?(.[^}?]?):(.*?)} would work better ... not 100% sure though.

keskad commented 10 years ago

Mhm, you can try it yourself and make a pull request if it would work :smile:

lllopo commented 10 years ago

I already tried it and it looks like working. I'll be using it in that form at least ... but I'm not sure it covers all the cases properly. Don't know Rain.tpl in details, don't have the time to test deeply either.

keskad commented 10 years ago

Okey, so, you can also paste here some lines of changed code that we will be able to merge.

lllopo commented 10 years ago

Parser.php, line 59 should be :

'ternary' => array('({.[^{?]?\?.?:.?})', '/{(.[^{}?]?)\?(.[^}?]?):(.?)}/'),

I think.

keskad commented 10 years ago

Thank you.

kargnas commented 10 years ago

That regexp will not parse: {($var>1)?"{$var}s":""}

But, {$data.User.something} is not matched with {(.[^{?]*?)\?(.*?):(.*?)}. What is full source?

lllopo commented 10 years ago

@kargnas Updated the original issue post. Some markup was breaking it. Now full thing should be visible.

lllopo commented 10 years ago

Update : Parser.php, line 59 should probably be :

'ternary' => array('({.[^{}?]?\?.[^}?]?:.?})', '/{(.[^{}?]?)\?(.[^}?]?):(.?)}/'),

Forgot to fix the other regex.