Templates through Hooks
#1
Hi again, I feel like I've read so much about how templates work in MyBB and yet I am unable to wrap my head around it for some reason.

I have had success with working with templates in custom pages I have coded. But as for working with templates through hooks I must say I feel pretty beat. After trying many possible options I feel more and more retarded, and can't say that this is good for my well-being either. Over the course of a week I have tried to study how scopes are affected by hooks, and there seems to be some delicate issues and my head is unable to wrap around it.

Here comes a case where I'm beat, I'm hooking a function into the hook 'postbit'. And in the postbit template I have added 
{$atv_votebuttons}

And in the function hooked to postbit I have something like this

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

Which is getting it from another template.
Tbh this is one of many many attempts at doing this, and I think I've gotten so deep into it that all the tries I've made has been faulty in logic so many times I kind of have given up. Spent a week looking at this, and templates have been ups and downs for me.

I appreciate all and any help, I have also tried other hooks but to no avail.

Feelings: Huge Sigh
Reply
#2
The postbit hook is a little strange in how it expects things to be done. You're setting the variable correctly, but it's not within the correct scope, so MyBB isn't seeing it when it evaluates the postbit template as a whole.

In general, you can use the $post['atv_votebuttons'] variable instead. I have a good example of this in a tiny multiple usergroup images plugin that I did. As you can see, the variables are refactored a bit to put them within $post['someVariable'] instead, but it works as intended.

https://github.com/Darth-Apple/APGI/blob...s/apgi.php

function apgi (&$post) {
	global $mybb, $templates, $cache, $apgi, $templates;
	$u_groups = explode(",", $post['additionalgroups']);
	$usergroups_cache = $cache->read("usergroups");
			
	foreach ($u_groups as $groupID) {
		$usergroup = $usergroups_cache[$groupID];

		if (!empty($usergroup['image'])) {
			$post['additional_images'] .= "<div style='margin-top: 3px; padding: 0px;'></div>"; // Adding a <br /> tag between group images results in alignment issues on chrome. This seems to work better.  
			eval("\$post['additional_images'] .= \"".$templates->get("postbit_groupimage")."\";");
		}
	}
	
	$post['groupimage'] = str_replace('<br />', '', $post['groupimage']); // chrome fix
	$post['additional_images'] .= "<div style='margin-top: 3px; padding: 0px;'></div>"; // padding fix
	return $post;
}

Notice that the function header has (&$post) within the definition. I'm also adding "return $post" to the bottom. Now, the $post['groupimage'] variable can be used within the postbit template and is evaluated as intended.

This can't be done on most hooks, but it is specifically supported on this hook (and a few others). May make things a little easier. Smile
Reply
#3
(2020-05-04, 02:12 AM)Darth Apple Wrote: The postbit hook is a little strange in how it expects things to be done. You're setting the variable correctly, but it's not within the correct scope, so MyBB isn't seeing it when it evaluates the postbit template as a whole.

In general, you can use the $post['atv_votebuttons'] variable instead. I have a good example of this in a tiny multiple usergroup images plugin that I did. As you can see, the variables are refactored a bit to put them within $post['someVariable'] instead, but it works as intended.

https://github.com/Darth-Apple/APGI/blob...s/apgi.php

function apgi (&$post) {
	global $mybb, $templates, $cache, $apgi, $templates;
	$u_groups = explode(",", $post['additionalgroups']);
	$usergroups_cache = $cache->read("usergroups");
			
	foreach ($u_groups as $groupID) {
		$usergroup = $usergroups_cache[$groupID];

		if (!empty($usergroup['image'])) {
			$post['additional_images'] .= "<div style='margin-top: 3px; padding: 0px;'></div>"; // Adding a <br /> tag between group images results in alignment issues on chrome. This seems to work better.  
			eval("\$post['additional_images'] .= \"".$templates->get("postbit_groupimage")."\";");
		}
	}
	
	$post['groupimage'] = str_replace('<br />', '', $post['groupimage']); // chrome fix
	$post['additional_images'] .= "<div style='margin-top: 3px; padding: 0px;'></div>"; // padding fix
	return $post;
}

Notice that the function header has (&$post) within the definition. I'm also adding "return $post" to the bottom. Now, the $post['groupimage'] variable can be used within the postbit template and is evaluated as intended.

This can't be done on most hooks, but it is specifically supported on this hook (and a few others). May make things a little easier. Smile

Thanks Darth, I'll try this but even just looking at your code I can see this working, I actually did employ something like this before in an earlier plugin I really am facepalming right now. Thanks a lot, I'll try again. 

Really thanks!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)