MyBB Community Forums

Full Version: Templating System
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hey guys

I have read a lot of threads on this forum about how myBB parses it's templates, using the eval() function. Anyway, none of this threads give a clear answer to how it actually passes the vars from the global scope into the eval function scope.

Could you PLEASE explain me how it's done (with code if possible).

For my own scripts I've managed to do it this way (minified code):

$some_var = 'simon';
$other_var = 2;
parse();

function parse(){
	ob_start();
	include('template.html');
	$output = ob_get_contents();
	ob_end_clean();
	$output = preg_replace('#{\$(.*?)}#sie', 'globalize(\'$1\')', $output );
	print($output);
}

function globalize($var){
	global $$var;
	return $$var;
}

And the template.html file could look like this for example:

Hi {$some_var}, you have {$other_var} unread private messages!

This code has one problem: it can't evaluate class objects or array keys... meaning: {$class->object} or {$array['key']} will lead to errors if written in the template.
This is from one of my plugins:

eval("\$headerwarnlevel = \"".$templates->get('headerwarnlevel')."\";");

I then put {$headerwarnlevel} in the templates and it loads the headerwarnlevel template, which is this:

 &mdash; {$lang->postbit_warning_level} <a href="./usercp.php"><strong>{$warninglevel}</strong></a>

$lang->postbit_warning_level is already defined by loading the language file and then $warninglevel is defined in the plugin. So $headerwarnlevel in the templates loads the headerwarnlevel template which contains two variables which have been defined in the code.

Does that help in any way??