MyBB Community Forums

Full Version: Using variable created in plugin with template[Not Resolved!!!!]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4
Hey Guys,

In the plugin I am creating, I have the following structure:

  // Usual plugin stuff

  function pluginname_activate() {
  // Some stuff out here
  // Creating template

// deactivate function

   function pluginname_run() {
   // some stuff

   // creating $var here

What I want to do is, use the value of $var in the template, and because the value changes according to input, I am using a variable.

EDIT: The template is for a custom page.
Everything is running fine, I just need to use a $var in the template

Please help me out!
You need to eval your template.
function pluginname_run()
	global $templates;

	$var = 'Hellow world!';

	eval('$foo = "'.$templates->get('random').'";');

	echo $foo;
	// write {$foo} in your "random" template
Does it work like:
//pluginname_activate function
 $template = '//blah blah

// activate function end

// other stuff here

// pluginname_run function
 $blahblah = 'blah';
eval('$blahblah = "'.$templates->get('random').'";');
echo $blahblah;

If yes, then it is not working...
Please someone help me out, this is the last part of my plugin, I have done everything else
'random' is a template, you create and paste the code in there.
//blah blah  {$blahblah}
That's what I am doing, I am creating a template in the variable $template and then adding {$blahblah} to it.
btw, my template is in global templates.
Any way it affecting the functioning?
Please do paste your full code here if possible.
Sure, it has two files:
first file, tagcloud.php in MyBB root:
	define('IN_MYBB', 1); require "./global.php";
	add_breadcrumb("Tag Cloud", "tagcloud.php"); 
	eval("\$html = \"".$templates->get("tagcloud_cloud")."\";"); 

Second file, the plugin file:

		Tag Cloud v1.0
		Plugin made by Aniruddh Agarwal
	if(!defined("IN_MYBB")) {
		die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
	function tagcloud_info(){
		return array (		
			"name"			=> "Tag Cloud",		
			"description"	=> "A plugin which displays threads in a list according to the criteria selected by the Admin",		
			"website"		=> "",		
			"author"		=> "Aniruddh Agarwal",				
			"version"		=> "v1.0",		
			"compatibility" => "16*", 
			"guid" => ""	
	include 'tagcloud/tagcloud_template.php';
	function tagcloud_activate() {
		global $db, $mybb, $message;
		$tagcloud_cloud = 
			array (
				"tid"            => NULL,
				"title"            => 'tagcloud_cloud',
				"template"        => $db->escape_string(
			<title>Tag Cloud</title>
			<table border="0" cellspacing="1" cellpadding="4" class="tborder">
					<td class="thead"><span class="smalltext"><strong>Tag Cloud</strong></span></td>
					<td class="trow1">
				"sid"            => "-1",
				"dateline"        => time(),
		$db->insert_query("templates", $tagcloud_cloud);
		$tagcloud_group = 
			array (		
				"gid"			=> "NULL",		
				"name"			=> "tagcloud",		
				"title" 		=> "Tag Cloud",		
				"description"	=> "Settings for the plugin.",		
				"disporder"		=> "1",		
				"isdefault"		=> "no",
		$db->insert_query("settinggroups", $tagcloud_group);	$gid = $db->insert_id();
		$tagcloud_setting_1 = 
			array (		
				"sid"			=> "NULL",		
				"name"			=> "tagcloud_enable",		
				"title"			=> "Enable Tag Cloud",		
				"description"	=> "Select yes if you want to enable Tag Cloud.",		
				"optionscode"	=> "yesno",		
				"value"			=> "0",		
				"disporder"		=> "1",		
				"gid"			=> intval($gid),
		$db->insert_query("settings", $tagcloud_setting_1);

		$tagcloud_setting_2 = 
			array (		
				"sid"			=> "NULL",		
				"name"			=> "tagcloud_latestthreads",		
				"title"			=> "Show Latest Threads",		
				"description"	=> "Select yes if you want to show the latest threads on the Tag Cloud.",		
				"optionscode"	=> "yesno",		
				"value"			=> "1",		
				"disporder"		=> "2",		
				"gid"			=> intval($gid),
		$db->insert_query("settings", $tagcloud_setting_2);

	function tagcloud_deactivate() {
		global $db, $mybb;
		$db->delete_query("templates", "title LIKE 'tagcloud_%' AND sid='-2'");
		$db->query("DELETE FROM ".TABLE_PREFIX."settinggroups WHERE name='tagcloud'");
		$db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name='tagcloud_enable'");
		$db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name='tagcloud_latestthreads'");
		$db->query("DELETE FROM ".TABLE_PREFIX."templates WHERE title='tagcloud_cloud'");

	function tagcloud_run() {
		global $mybb, $db;
		$latestthreads = $db->query("SELECT tid,subject FROM `".TABLE_PREFIX."threads` WHERE `replies`>1 ORDER BY `lastpost` DESC LIMIT 40");
		while($thread = $db->fetch_array($latestthreads)){
			$sizes = array("11","14","18","22","28");
			$fontsize = rand('0', '4');
			$message = "<a href=\"".$mybb->settings['bburl']."/showthread.php?tid=".$thread['tid']."\"><span style=\"font-size:".$sizes[$fontsize]."px\">".$thread['subject']."</span></a> |\r\n";
		eval('$message = "'.$templates->get('tagcloud_cloud').'";');
		echo $message;	

I can't really see what you are trying to do. May you explain it with details? It does seems like there is a missing file too (unless you forgotten to paste all the files here).

Are you trying to write a plugin or modify an existing one?
I am writing a plugin, which creates a template, which is used by a separate individual page at the MyBB root.
For the template,
The plugin fetches the latest threads from the db, and then stores them in the var $message
I want to use the variable in the template.
Wait, so you want to use the variable in the custom page? If so, you need to either add a hook to the page or just define the variable there...
Pages: 1 2 3 4