MyBB Community Forums

Full Version: Add Global Templates And Use Them In Other Templates
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I made a thread previously discussing something like this but was a bit confused on how it actually worked, however my previous thread only included a new template that was in the Index Templates of a specific template group.

My goal this time is to add global templates that I can just call in specific template groups to be used.


So for example I have made the template site_news in Global Templates, inside I have placed this information:
<b>01/25/2017</b> &mdash; The site is just now under development. Please check back at a later date for more information.

I want to pull this template and put it in Default Templates --> Header Templates --> Header (but I also want to be able to stick this new template any where other than header templates if I ever need to). I tried adding it in the header like this but it does not show so once again I am stumped:
{$site_news}


I would REALLY love for someone to EXPLAIN to me what needs to be done (not just give me a code and say "hey insert this") to make this template global, as well as to make it where it can literally be used ANYWHERE I wish to put it.
Or, you could use the plugin I made here: https://community.mybb.com/thread-206980...pid1257887

Just need to change the plugin file name and function names, and the actual template it renders.
@.m.
That is an interesting way to go about it and considering I actually have that plugin downloaded I will give it a shot.

@Matt
I do appreciate the help from you, I feel like you're one of the main ones that always comments on my questions so thank you!
Unfortunately I don't quite understand how to make it global instead of just on the index, I've been looking over MyBB's hello plugin to try to grasp some of the information needed to make a plugin and what does what but I am still stumped highly.

So far this is what I have, it's not finished but personally I don't think it will work at all:
<?php
// No accessing this file directly from the browser.
if(!defined('IN_MYBB'))
{
	die('This file cannot be accessed directly.');
}

$plugins->add_hook('global_start', 'site_news');

$function site_news_info()
{
	global $lang;
	$lang->load('site_news');
	return array(
		'name' => 'Site News!',
		'description' => 'Load the site_news template',
		'website' => 'Not Applicable',
		'author' => 'Not Applicable',
		'authorsite' => 'Not Applicable',
		'version' => '1.0',
		'compatibility' => '18*',
		'codename' => 'site_news'
	);
}

function site_news_activate()
{
	
}
function site_news_deactivate()
{
	
}
I'm afraid I messed up when changing $plugins->add_hook('global_start', 'site_news');
I added 'global_start' where you had originally 'index_start', I honestly don't know the difference but thought if maybe I used global_start it would be global and able to be used on any templates, not just templates on the index page.
Quote:I do appreciate the help from you, I feel like you're one of the main ones that always comments on my questions so thank you!

No worries! Smile

global_start should work, the issue seems to be you removed the function that actually loads the template Toungue So, at the bottom add:

function site_news()
{
	global $templates, $site_news;

	eval("\$site_news = \"".$templates->get('site_news')."\";");
}
Thinking about it, I'm going to make a plugin to do this without needing a custom plugin every time as it's not ideal, essentially replicating what xthreads does but without needing the full plugin. Use this for now but I'll have a proper plugin for you soon.
Awesome thank you!! Smile

Just a few more questions and I will try to be out of here before overthinking things and trying to take too many steps lol.

In your plugin file you gave me your last bit was:
function index_affiliates_load()
{
	global $templates, $index_affiliates;

	eval("\$index_affiliates = \"".$templates->get('index_affiliates')."\";");
}

Right here though you just have
function site_news()

Is there a difference between template_name() and template_name_load()?

------

Is this necessary if its not being used/is empty? What exactly does it do?
function site_news_activate()
{
	
}
function site_news_deactivate()
{
	
}
The actual name of the function is largely irrelevant, it just needs to be unique, and match what you're putting in the hook as the function to call. I just called it that here as that's what you already had in the hook line.

$plugins->add_hook('hook_name', 'function_name');

As for the activate/deactivate, you can probably remove them, I think the ACP checks if they exist before trying to call them; usually they'd add settings/templates/make template changes, in this case it's more of an OCD thing to always have at minimum the info/activate/deactivate functions.
So hook name would be the place where it is? (Index, header, etc.)
And function name is the name of what we're trying to accomplish? Adding in the template set.

Makes a lot more sense now, thank you VERY much! Someone needs to make you some cake and homemade ice cream. Smile

Okay this is my bad, marked as solved too soon. When trying to upload the plugin to the inc-->plugins folder like all other plugins something goes wrong.

Here is the finished code:
<?php
// No accessing this file directly from the browser.
if(!defined('IN_MYBB'))
{
    die('This file cannot be accessed directly.');
}

$plugins->add_hook('global_start', 'site_news');

$function site_news_info()
{
    global $lang;
    $lang->load('site_news');
    return array(
        'name' => 'Site News!',
        'description' => 'Load the site_news template',
        'website' => 'Not Applicable',
        'author' => 'Not Applicable',
        'authorsite' => 'Not Applicable',
        'version' => '1.0',
        'compatibility' => '18*',
        'codename' => 'site_news'
    );
}

function site_news_activate()
{
    
}
function site_news_deactivate()
{
    
}
function site_news()
{
    global $templates, $site_news;

    eval("\$site_news = \"".$templates->get('site_news')."\";");
}

?>

My plugins page disappears and then this doesn't even work...

Before file upload: https://s30.postimg.org/cuzpscp7l/Capture.png
After file upload: https://s29.postimg.org/6cuzohzo7/Capture2.png
Yeah, the hooks are defined all throughout the core MyBB code, then the function is just whatever you want to run at the point the hook runs.

Under the hook, you've got:

$function

Remove the $

Also, you'll need to remove:

$lang->load('site_news');

As that file doesn't exist and you're not using any languages anyway. Also need to make sure it's called site_news.php
Pages: 1 2