MyBB Community Forums

Full Version: {These things}
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Within MyBB
Wheel-World Wrote:How does mybb do it?
MyBB's template system uses the simple PHP concept that variables within double-quoted strings will be replaced when executed. There is nothing complicated about it, and if you think it is complicated, you are overthinking it.

Broken down to bare bones, this is how it works:
$name = 'Dennis'; // This represents your variable.

$output = "Hello there, {$name}"; // This represents your template

echo $output; // This represents the output_page() function in MyBB
The above code would output:
Quote:Hello there, Dennis

That is fundamentally how MyBB templates work, and for the same reason that is why you cannot put PHP code within templates and expect it to work.

$name = 'Dennis';
$output = "Hello there, <?php strtolower($name); ?>";
echo $output;
Will not work as expected.

Outside MyBB
Wheel-World Wrote:I am not talking about within mybb, i am saying how do you do it for other projects.
I recommend Smarty.
DennisTT Wrote:Within MyBB
Wheel-World Wrote:How does mybb do it?
MyBB's template system uses the simple PHP concept that variables within double-quoted strings will be replaced when executed. There is nothing complicated about it, and if you think it is complicated, you are overthinking it.

Broken down to bare bones, this is how it works:
$name = 'Dennis'; // This represents your variable.

$output = "Hello there, {$name}"; // This represents your template

echo $output; // This represents the output_page() function in MyBB
The above code would output:
Quote:Hello there, Dennis

That is fundamentally how MyBB templates work, and for the same reason that is why you cannot put PHP code within templates and expect it to work.

$name = 'Dennis';
$output = "Hello there, <?php strtolower($name); ?>";
echo $output;
Will not work as expected.

Outside MyBB
Wheel-World Wrote:I am not talking about within mybb, i am saying how do you do it for other projects.
I recommend Smarty.

I'm more then sure you use eval(); I think... I didn't say the way it parses the variables is complex, I just said the overall template system is quite complex.
rcpalace Wrote:I'm more then sure you use eval(); I think... I didn't say the way it parses the variables is complex, I just said the overall template system is quite complex.
It's the same thing. It's just that you can't expand the string within double quotes if you're retrieving it directly from the DB - which is why you need a secondary PHP parser.

Continuing from DennisTT's example:

$template = 'Hello there, {$name}'; //variable not expanded
$name = 'Dennis';
// we have to somehow expand the $template variable, but how?
eval("$output = \"$template\";");
echo $output;
Alwell, its all right. Right now i am just getting them to include
<?php
include('display.php')
?>
into their html template, i was just trying to shorten that.
Yeah, actually every template would like a PHP file, the only difference is that is stored in the database instead of files.

If you will be using files instead of the DB, then you can just include the PHP file (or you can use the .htaccess trick mentioned before by rcpalace) and everything with {$blah} will be replaced with your variable.
rcpalace Wrote:I'm more then sure you use eval(); I think... I didn't say the way it parses the variables is complex, I just said the overall template system is quite complex.


I think what you mean by "complex" is the way that MyBB stores the templates in the database, and grabs it. Of course we have to sanitize the template (escape the double-quotes used in the template, etc), but this is just the way we implement the templates.

As I said earlier, the fundamental concept of the MyBB template parser is the PHP double quotes. There isn't anything more or less than these things which make the templates work their magic --> " and "
DennisTT Wrote:
rcpalace Wrote:I'm more then sure you use eval(); I think... I didn't say the way it parses the variables is complex, I just said the overall template system is quite complex.


I think what you mean by "complex" is the way that MyBB stores the templates in the database, and grabs it. Of course we have to sanitize the template (escape the double-quotes used in the template, etc), but this is just the way we implement the templates.

Storing it in a file is easy, but the thing I've never played around with was grabbing the template from the database and converting the {$blah}. The new template system in my software that I'm developing stores the templates in the database, but grabs them in a different way then what you guys do. Theirs always a zillion ways to do things. Toungue
The storage of the databases has nothing to do with the way they are parsed, but hey if you're happy, I'm happy.
Pages: 1 2