MyBB Community Forums

Full Version: Using Template in PlugIn
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm going to migrate a PHP application earlier written by me to a MyBB plugin. It also happens to be my first MyBB plugin. I came across the wiki pages, but as I can see that those do not detail upon authoring templates. Googling further, I came across a plugin, which makes use of the following template
<html>
<head>
<title>{\$mybb->settings[\'bbname\']} - {\$title}</title>
<script type=\"text/javascript\">
function validate_form(){
var title=document.forms[\"snippet\"][\"txtTitle\"].value;
if (title==null || title==\"\"){
alert(\"Title must be filled out\");
return false;
}
if (title.length > 50){
alert(\"Title must not be longer than 50 characters\");
return false;
}
var category=document.forms[\"snippet\"][\"ddLang\"].selectedIndex;
if (category==0){
alert(\"You must select a category\");
return false;
}
var snip=document.forms[\"snippet\"][\"txtSnippet\"].value;
if (snip==null || snip==\"\")
 {
 alert(\"You must enter a snippet\");
 return false;
 }
return true;
}
</script>
{\$headerinclude}
</head>
<body>
{\$header}
<table border=\"0\" cellspacing=\"{\$theme[\'borderwidth\']}\" cellpadding=\"{\$theme[\'tablespace\']}\" class=\"tborder\">
<tr>
<td class=\"thead\"><strong>{\$title}</strong></td>
</tr>
{\$content}
</table>
{\$footer}
</body>
</html>

The plugin consists of few more pages which are written in PHP. The approach of the author is to keep on appending the dynamically generated HTML to a $content PHP variable & then executing the following code
$template=$templates->get("my_plugin_teamplate");
eval("\$page=\"".$template."\";");
output_page($page);

Since little amount of HTML is generated by the PHP, that approach worked well in that plugin. But, my application is larger & therefore generates bulk amount of HTML throughout the PHP pages. Adapting the same approach will turn my code messy. What I want to know is whether it is the ONLY approach to deal with templates or is there any better way to do that?
Typically, you have several templates. For example, let's say you want to display a list of people and their ages. You'd have a page frame template (note that all of these are very simplified):
<html>
<head>
<!-- Header stuff -->
</head>
<body>
<!-- Default body stuff -->
{$content}
<!-- More default body stuff-->
</body>
</html>

I personally would put the list container in it's own template:
<table class="tborder">
<tr>
<td class="thead">
<!-- Table header stuff -->
</td>
</tr>
<!-- Optional column header stuff in class="tcat" -->
{$list}
</table>

Then you'd have the list bit:
<tr>
<td>
{$username}
</td>
<td>
{$userage}
</td>
</tr>

Now the plugin code to display it:
// Basic setup stuff

// Note, I have little idea what MyBB's user structure is without looking it up
// so I have no idea if these are right.
$query = $db->select("username, birthday", "users");

// Now, render the list rows
$list = "";
while($user = $db->fetch_array($query))
{
    $username = htmlspecialchars_uni($user['username']);
    // Made up function:
    $userage = figure_age($user['birthday']);

    // I might have gotten this wrong
    eval('\$list .= "' . $templates->get("userage_listbit") . '";');
}

// Now render the list table:
eval('\$content = "' . $templates->get("userage_list") . '";');

// Now render the page:
eval('\$output .= "' . $templates->get("userage") . '";');
output_page($output);

The reason it's done this way is because output_page does a bunch of processing on the output, including sending headers and compressing if the option is set. IIRC it also replaces some template tags like <navigation> and <debug>, though it might be a different file that does those.