MyBB Community Forums

Full Version: How to call a template ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
How to call a template? e.g. a template is in test_activate(), how do we call it in test() function?

I m trying to adding a template with a plugin, and settings of the plugin is changeable, or I can directly use if,else in template?
e.g
test_activate (){
if (mybb->settings['fi']==1)
{
fi template data
}

if (mybb->settings['fi']==2)
{
fi template data 2
}
}

OR I have to over write the template every time the settings changes
test(){
if (mybb->settings['fi']==1)
{
fi template data
}

if (mybb->settings['fi']==2)
{
fi template data 2
}
}
(2011-09-25, 11:53 AM)crazy4cs Wrote: [ -> ]http://wiki.mybb.com/index.php/MyBB_Plugin_Hooks

I don't want to add hooks, I've added them. My problem is how to call that template in your plugin?


admin_style_templates
 100     admin_style_templates_add_set
 161     admin_style_templates_add_template
 196     admin_style_templates_add_template_commit
 297     admin_style_templates_edit_set
 374     admin_style_templates_edit_template
 429     admin_style_templates_edit_template_commit
 579     admin_style_templates_search_replace
 974     admin_style_templates_find_updated
 1076    admin_style_templates_delete_set
 1114    admin_style_templates_delete_set_commit
 1131    admin_style_templates_delete_template
 1159    admin_style_templates_delete_template_commit
 1195    admin_style_templates_diff_report
 1220    admin_style_templates_diff_report_run
 1257    admin_style_templates_revert
 1285    admin_style_templates_revert_commit
 1309    admin_style_templates_set
 1540    admin_style_templates_start

Particularly this, not sure, maybe:

admin_style_templates_start

?
I think its not
You're using incorrect way to "Use" a function. You're using;
test()
{
// blah blah blah
}
However correct syntax should be like this;
function test()
{
// blah blah blah
}

next you've to think where you want to use that particular template. Use the hook corresponding to it and run the function, e.g. if you want to use it on all pages, use global_start hook and use the function. BUT make sure the name of the function SHOULD be exactly the same as that of the name of the hook command.
Does he mean:

$templates->get('temp_name');

^This?
Okay here is the code, Tell me how to make a template in it and call it so that when test_op changes variable $test also changes to different value?

<?php


if(!defined("IN_MYBB"))
{
die ("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}

$plugins->add_hook("index_start","test");

function test_info()
		{
return array(
		
		"name"=> "Just a test",
		"description"=> "This plugin is a test",
		"website"=> "http://www.vubscs.tk",
		"author"=> "sunjava1",
		"authorsite"=> "http://www.facebook.com/sunjava1",
		"version"=> "1.0",
			);
		}
		
function test_activate(){
global $db, $mybb;
	
	$test_group = array(
		"name" => "test",
		"title" => "Test settings",
		"description" => "Test and test .",
		"disporder" => "501",
		"isdefault" => "no",
		);
	$group['gid'] = $db->insert_query("settinggroups", $test_group);
	$gid = $db->insert_id();
	
	
	$test_set = array(
		"name" => "test_op",
		"title" => "Number of test",
		"description" => "one more test dude",
		"optionscode" => "select \n30=1 test\n20=2 test",
		"value" => "30",
		"disporder" => "1",
		"gid" => intval($gid),
		);
	$db->insert_query("settings", $test_set);
	require MYBB_ROOT."/inc/adminfunctions_templates.php";
find_replace_templatesets("index", "#".preg_quote('{$forums}')."#i", '{$forums}<br>{$test}'); 
	 	
	rebuild_settings();
}		

function test_deactivate(){
global $db;
	
	$db->query("DELETE FROM ".TABLE_PREFIX."settinggroups WHERE name='test'");
    $db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name='test_op'");
	
	
  require MYBB_ROOT."/inc/adminfunctions_templates.php";
    find_replace_templatesets("index", "#".preg_quote('{$forums}<br>{$test}')."#i", '{$forums}'); 

	// Rebuilding settings
    
	rebuild_settings();
	}

function test(){
global $test, $mybb;

if ($mybb->settings['test_op']==20)
{

$test=' You have selected  '. $mybb->settings['test_op'] .'  no of test times';	


}

else 
{

	$test='You have not selected anything, default value for test is  '. $mybb->settings['test_op'] .'  Cheers';	

}

}

?>


You need to insert the template (my_template) into the database in _activate() and delete it in _deactivate(). Then you need to use find_replace_templatesets() to insert a template variable {$my_template} into another template (parent template) where you want it to appear.

Then you need to hook into where the parent template is called and call your template like such:

eval("\$my_template = \"".$templates->get("my_template")."\";");
(2011-09-26, 08:32 AM)Nitrus Wrote: [ -> ]You need to insert the template (my_template) into the database in _activate() and delete it in _deactivate().

Plugin Development 101.

_activate and _deactivate should run tasks as if the user is switching the plugin on or off, e.g. modifying existing templates.

If you install anything into the database e.g. templates, settings etc., you should always use _install and _uninstall and have an _is_installed function to verify it exists.

This way, the user doesn't lose anything as they believe they are just deactivating it - not uninstalling the entire plugin.
Pages: 1 2