MyBB Community Forums

Full Version: PHP Variables in templates (non-MyBB project)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm attempting a project that makes use of templates similarly to how MyBB does, but I'm royally stuck.

What I have in the template is this:
<form action="{url}/member.php?do=login">
          <strong>Username</strong>
            <input type="text" /><br />

         
         <strong>Password</strong>
            <input type="password" /><br />

          <input name="login" type="submit" value="Login" />
        </form>
        <a href="{url}/member.php?do=lostpw">Forgot Password</a>

What I want my function to do is replace the {url} with the value of $settings->load("url"), but I want it to be relatively dynamic so that {SETTING} can be replaced with the value of it's respective setting.

I don't want to have it do a str_replace for each and every setting as that doesn't allow for people to include custom settings in the templates without a manual file edit and if there's a bugfix in the templates class, either the fix has to be applied manually or they have to replace their str_replace commands for every setting.

Here's the $templates->load code:
	function load($template){
		global $settings;
		
		$query = "SELECT `template` FROM `{$this->tbl}` WHERE `name` LIKE '{$template}' LIMIT 0,1";
		$sql = mysql_query($query) or die ("Query failed: ".mysql_error());
		$result = mysql_fetch_array($sql) or die ("Query failed: ".mysql_error());
		
		 print $result['template'];
	}
MyBB kind of cheats and just uses PHP variables, so when you see {$lang->something} in the template, you're directly reading the $lang->something PHP variable. MyBB does it something like this:

$template = "<tag title=\"{\$lang->something\">";
eval("\$output = \"" . $template . "\";");
echo $output;

Note the big-mess-o-important-quote-escapes, they're important to how the thing works.
I personally would wrap the eval() contents in a function (and do so in a few plugins) to reduce chances of errors:
function template($varName, $temp, $concat = false)
{
	return "\${$varName} " . ($concat? "." : "") . "= \"" . $temp . "\";";
}

// Usage: eval(template("variable", "template", $tackOntoEnd));

I'm sure there's a place where MyBB does some escaping magic to the templates, but I can't find it at the moment.
I tried the eval, but it required me to have this in my template in place of {url}:

<?php print $settings->load("url"); ?>

I might consider copying it to a pload that prints it instead of just returning the value....

And MyBB's way of storing the eval'd template into another variable isn't how I'm doing the templates.

EDIT: I have a solution, not the most elegant, but it'll work!
function load($template){
		global $settings;
		
		$query = "SELECT `template` FROM `{$this->tbl}` WHERE `name` LIKE '{$template}' LIMIT 0,1";
		$sql = mysql_query($query) or die ("Query failed: ".mysql_error());
		$result = mysql_fetch_array($sql) or die ("Query failed: ".mysql_error());
		$return = $result['template'];
		$return = str_ireplace('{',"<?php ",$return);
		$return = str_ireplace('}',"; ?>",$return);
		$return = "?>".$return;
		$return = eval($return);
		print $return;

	}
I've changed the $settings->load to $settings->pload in the template (pload prints instead of returns the value).

Hope this will help people if they have a similar problem!