2012-01-14, 03:34 AM
This tutorial will focus on how to use templates in your plugins in a better way than what is currently "standard". Many plugin authors currently implement their plugins with templates added to the global templates (sid = -1) and just remove them by name. This can make updating a plugin a bear when it removes your template - with all your existing modifications to it - during the upgrade process. This method eliminates that, and in addition inserts the template into each theme instead of into the global templates. It also allows you to use a templategroup to group the templates for your plugin. How nice, eh?
What this means for the user's of your plugin:
Users of my new User Agents plugin are finding this a very nice feature. At least those that are beta testing for me!
Questions/Comments/Suggestions welcome. I fully admit I don't always think of everything. But if you make a comment about something that I've already covered and you didn't bother to read the whole tutorial... I will beat you with a clue-by-four. So don't do it. I'm sick of wasted time answering the same thing over and over like happened in the 1.6.5 Plugin Changes thread. Basically this is no different than RTFM, so RTFP & RTFT (Read the _F_ Post & Read the _F_ Thread!). Please.
Now... on to it!
Installation:
Nothing to do with templates goes here. (See caveat in the activation section)
Is Activated
Nothing to do with templates goes here.
Uninstall
Template Group removal goes here.
Example:
Activate
Template group creation, if it doesn't exist! We don't do this in the install function because that would break the ability to update from a previous version of the plugin that doesn't include a templategroup, but does include (the proper way!) all settings added & removed via install/uninstall as they would lose all their settings. If this is a brand new plugin, I suggest placing this in the install without the if wrapper.
Example:
Template creation. This is a pretty standard thing in plugins, and with this method you need to have these in the activate function, not install, so that they can just deactivate then activate to update the plugin! Note the version numbers here! I'm starting at 1600 (for MyBB 1.6 series), and any time you modify the template you increase the version by 1. Otherwise... don't mess with it! Also note that I'm using -2 for my sid, this is important for this method.
Example:
Deactivate
Here we simply remove the templates we added in the activate function. Very very simple.
However it is important to do it the right way. Note the use of IN() for the list of template names, but then after it we also have AND sid='-2', you cannot forget that part, or you will remove the user modified versions of the template as well!
Example
Notes:
Things I did not yet address with this include:
What this means for the user's of your plugin:
- They can have the templates setup different for different themes instead of being locked into the same template for every theme.
- If they modify the template and its changed in a new release of the plugin, then they can use the find updated templates feature just like with MyBB's official templates!
- In the end the users have more control of how their plugin appears to the members of their sites.
Users of my new User Agents plugin are finding this a very nice feature. At least those that are beta testing for me!
Questions/Comments/Suggestions welcome. I fully admit I don't always think of everything. But if you make a comment about something that I've already covered and you didn't bother to read the whole tutorial... I will beat you with a clue-by-four. So don't do it. I'm sick of wasted time answering the same thing over and over like happened in the 1.6.5 Plugin Changes thread. Basically this is no different than RTFM, so RTFP & RTFT (Read the _F_ Post & Read the _F_ Thread!). Please.
Now... on to it!
Installation:
Nothing to do with templates goes here. (See caveat in the activation section)
Is Activated
Nothing to do with templates goes here.
Uninstall
Template Group removal goes here.
Example:
function pluginname_uninstall()
{
global $db;
$db->delete_query("templategroups", "title = 'My Template Group Name'");
}
Activate
Template group creation, if it doesn't exist! We don't do this in the install function because that would break the ability to update from a previous version of the plugin that doesn't include a templategroup, but does include (the proper way!) all settings added & removed via install/uninstall as they would lose all their settings. If this is a brand new plugin, I suggest placing this in the install without the if wrapper.
Example:
function pluginname_activate()
{
global $db;
$q = $db->simple_select("templategroups", "COUNT(*) as count", "title = 'My Template Group Name'");
$c = $db->fetch_field($q, "count");
$db->free_result($q);
if($c < 1)
{
$ins = array(
"prefix" => "mytemplateprefix",
"title" => "My Template Group Name",
);
$db->insert_query("templategroups", $ins);
}
}
Template creation. This is a pretty standard thing in plugins, and with this method you need to have these in the activate function, not install, so that they can just deactivate then activate to update the plugin! Note the version numbers here! I'm starting at 1600 (for MyBB 1.6 series), and any time you modify the template you increase the version by 1. Otherwise... don't mess with it! Also note that I'm using -2 for my sid, this is important for this method.
Example:
function pluginname_activate()
{
global $mybb;
$ins = array(
"tid" => NULL,
"title" => 'mytemplateprefix_Template 1',
"template" => $db->escape_string('some template html stuff here'),
"sid" => "-2",
"version" => $mybb->version + 1
"dateline" => time(),
);
$db->insert_query("templates", $ins);
$ins = array(
"tid" => NULL,
"title" => 'mytemplateprefix_Template 2',
"template" => $db->escape_string('some other html here'),
"sid" => "-2",
"version" => $mybb->version + 1,
"dateline" => time(),
);
$db->insert_query("templates", $ins);
}
Deactivate
Here we simply remove the templates we added in the activate function. Very very simple.
However it is important to do it the right way. Note the use of IN() for the list of template names, but then after it we also have AND sid='-2', you cannot forget that part, or you will remove the user modified versions of the template as well!
Example
function pluginname_deactivate()
{
global $db;
$db->delete_query("templates", "title LIKE 'mytemplateprefix_%' AND sid='-2'");
}
Notes:
Things I did not yet address with this include:
- User modified templates are never removed. I figure end users can do this manually. This is for those rare occasions where you have to reinstall the plugin to update because it has new settings added.
- The addition of a new function for plugins called "puginname_update()" since MyBB's core doesn't currently support it. I think that when I have free time I'll write in support for this in MyBB's plugin class. We need it.