MyBB Community Forums

Full Version: Adding PHP Vars In Mybb Templates
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Hey,

I am adding updates my plugin now I have one question which is confusing me.

In order to make the plugin better user experience wide I am trying to add PHP Variables in mybb templates for example I see it in the header templates in other announcement mods {show_messages}

How is it possible to add PHP Variables in the Mybb templates I will need to add them in multiple templates so that I can add new functionality and features to the plugin.

I have seen it done in other mods.

Thanks,

Spudster
Defining variables to be used in templates from a logistics standpoint usually works like this:

  1. In _activate, alter the templates to include your variable (header for example) and ofc in _deactivate restore the templates
  2. Then you'll need to hook in properly before the template is eval()'d and globalize the variable and set its value.

For example, to hook into index_start (having already added {$this_var} to the index template):

$plugins->add_hook('index_start', 'my_plugin_function');

function my_plugin_function()
{
	global $this_var;

	$this_var = '<a href="link">Link or whatever</a>';
}
I have added the things you have said above, How would I be able to evaluate it.

Thanks for the help so far ;.)

Alright I did it ;.)

Where would I be able to find A list of hooks.

I need to gain access header global, index_start and bottom index and bottom global templates.
There is documentation available (just search this forum for links). It isn't complete by any means, but let me tell you the way I always find the appropriate hook (or find if one exists) without consulting any documentation.

Using the URL of the page you want to alter (showthread.php) and then browsing through the templates to find a specific one (showthread_quickreply) is the first step.

The next step is to open the file that the URL of the page points to (showthread.php) and using any text editor just search for 'hook'. Pay attention to where in the code the hook is placed so that you are before any eval()s that would affect what you are doing.

Then refer to the code above, if you are having issues with using templates with eval() then is is as simple as ensuring that the variables used in the template have been set and then eval()ing the variable you have placed in the template ($this_var) with the contents of the template.

// globalize any variables necessary,
// if you use $mybb in the template it
// must be available to your function
global $templates, $this_var;

// set variables used in the template code here:
$another_var = 'value';

eval("\$this_var = \"" . $templates->get("template_name") . "\";");
What is eval used for? I didn't used eval only the index_start hook and the global function to make $this_var global.
I was only showing that if you used a template.
Ok Thanks for the help ;.)

Think I get the bases of it I can now start adding updates to the plugin.

I have another issue which I am currently stumped on.

I am adding data to a certain by using this


if($global_switch_row==1) {
							find_replace_templatesets("header",'#'.preg_quote('{$unreadreports}').'#','{$unreadreports}{$randomvar}');	

}


Only issue is every time the user goes the index it will re-execute the function, How can I make it so if it has already been added to the template then don't re-execute it.

Thanks

hoping to get an answer soon that way I can resume on with the plugin.
That's what I meant by editing the templates in _activate.

in your_plugin_activate -- edit the template as you've done above.
in your_plugin_deactivate -- restore the templates:

find_replace_templatesets("header",'#'.preg_quote('{$randomvar}').'#','');

Now rather than the condition above ($global_switch_row) affecting whether the templates are edited, use it to determine whether to set $randmvar or not:

if ($global_switch_row == 1) {
	$randomvar = 'show something';
} else {
	//don't :p
}

I don't explain things superbly all the time so sorry if I am not making sense.
Thanks that method worked. Should of thought of that sooner.
(2013-07-01, 03:36 AM)Wildcard Wrote: [ -> ]There is documentation available (just search this forum for links). It isn't complete by any means, but let me tell you the way I always find the appropriate hook (or find if one exists) without consulting any documentation.

Using the URL of the page you want to alter (showthread.php) and then browsing through the templates to find a specific one (showthread_quickreply) is the first step.

The next step is to open the file that the URL of the page points to (showthread.php) and using any text editor just search for 'hook'. Pay attention to where in the code the hook is placed so that you are before any eval()s that would affect what you are doing.

Then refer to the code above, if you are having issues with using templates with eval() then is is as simple as ensuring that the variables used in the template have been set and then eval()ing the variable you have placed in the template ($this_var) with the contents of the template.

// globalize any variables necessary,
// if you use $mybb in the template it
// must be available to your function
global $templates, $this_var;

// set variables used in the template code here:
$another_var = 'value';

eval("\$this_var = \"" . $templates->get("template_name") . "\";");


I did by this way, but there is 2 problems!

1. When I change the template, the output don't changes! I think the template includes from another theme!

2. The internal language variables not working. I mean this variables:
"Current time: {1} Welcome back, Admin."
why "{1}" not working?

How i can fix this problems?
Pages: 1 2 3