S
S
systemiv2013-02-03 04:46:15
PHP
systemiv, 2013-02-03 04:46:15

Template engine with functions

I am writing my own stencilizer, but I ran into the problem of calling functions in templates
. I will give an example:

...
'@\[\+rand\(([0-9]+)-([0-9]+)\)\+\]@si' => rand(intval("$1"), intval("$2")),
...

it is one of the array elements to replace.
Here, when you are in a template of a construction like: [+rand(4-10)+] a random number from 4 to 10 should be displayed.
I just can’t get it to work. When converting submasks to numbers, they become 0.
Thanks.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Pushkarev, 2013-02-03
@systemiv

Everything is working:

echo preg_replace_callback(
  '@\[\+rand\(([0-9]+)-([0-9]+)\)\+\]@si' ,
  function($m){return rand(intval($m[1]), intval($m[2]));},
  'test [+rand(4-10)+] test');

S
Sergey, 2013-02-03
Protko @Fesor

Why not use a lexer/parser? Compile the template into native code and execute it already.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question