MyBB Community Forums

Full Version: [PHP] Passing variables in myBB style
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is more a question for the myBB developers and maybe a little off topic because it's not related directly to the forum system rather more to PHP.

Let's say a have a simple php index file that includes a html template.

Now, I want to define in php...

$var = 'Hello World';

and then in the html template write..

{$var}

And then display 'Hello World!' instead of that {$var}...
Like myBB does with templates.

How can I pass vars like this?

I had an idea wich didn't work, and it was parsing '{' and '}' to '<?php echo ' and ' ?>' but I see that eval command in the forum's php files... could you please explain how you do that?

Regards
MyBB uses eval() to parse the template variables.
You're probably better off using just PHP files, unless you're trying to make a distributed system.
And how exactly is that?
(2009-01-03, 08:23 AM)Shadow Player Wrote: [ -> ]And how exactly is that?
Oops, sorry, looks like I read your question too fast... :/

If you look around MyBB's code, you'll see stuff like:
eval("\$somevar = \"".$templates->get("ttt")."\";");
That's how the variables get replaced (it gets evaluated as a PHP string).

Let's say the template ttt contains the following:
Template {$variable}
The $templates->get() method returns the template (with some things escaped). If you break it down, you might get something like this:
eval("\$somevar = \"Template {\$variable}\";");
Thus, the PHP code to be evaluated, in the eval() statement becomes:
$somevar = "Template {$variable}";
so you end up getting the $variable passed into the string.

Not sure if I can make it any clearer - you really have to understand and figure out the code yourself.

Hope that helped.
(2009-01-04, 08:44 AM)Yumi Wrote: [ -> ]...Not sure if I can make it any clearer - you really have to understand and figure out the code yourself.

Hope that helped.

Bookmarked with the tag "advanced."
Things I don't understand can be fun.
In ~six months it will make much more sense.
It's definitely pretty neat how it's done, and yet advanced at the same time - but well worth itSmile Good luck getting the hang of it(I don't mean it will be hard, just wishing ya luck)
Is there any preg_replace involved in this?... I can only imagine this working if I parsed:

{$variable}

to...

<?php echo $variable ?>

Considering that it's html code.

Edit:

Got it now, thx
I used a php template mod, it works pretty good.
found the plugin, was written by ZiNgA BuRgA, called PHP and Template Conditionals (1.3)