• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Possible template_create bug with PHP 5.4 ?

MOZ

Well-known member
#1
One of my addons utilises the template_create even to replace a template.

I have used the following code, however the template is not replaced. The method is definitely called as I tested manually .

PHP:
<?php
 
class SortMem_Listener_MemListSort
{
    public static function templateCreate($templateName, array &$params, XenForo_Template_Abstract $template)
    {
        if ($templateName == 'member_list')
        {
            $templateName = 'member_list_item';
        }
    }
}
 

xfrocks

Well-known member
#3
I'm not at my dev machine but the signature should be

Code:
&$templateName
if you want to modify the template name... I don't recommend doing that btw, you will break other add-ons.
 

MOZ

Well-known member
#4
Thanks, that worked, however, now the question arises, why did the earlier work with PHP 5.3?

In XF Code event listener editor the given signature is as below:
$templateName, array &$params, XenForo_Template_Abstract $template
So, could the devs please correct this?

Also, xfrocks, what would you suggest I do to edit the template if hooks are not present, TMS? That seems like the only solution.
 

Chris D

XenForo developer
Staff member
#6
Also, xfrocks, what would you suggest I do to edit the template if hooks are not present, TMS? That seems like the only solution.
Where possible use template post render. It gives you the entire template to work with as long as it is a sort of container template (i.e. not one included by xen:template include).

If that isn't feasible then, yeah, TMS is a much better solution.

Replacing an entire template is just not good. If I've created an add-on that modifies the same template then that will break my add-on.
 

MOZ

Well-known member
#8
Where possible use template post render. It gives you the entire template to work with as long as it is a sort of container template (i.e. not one included by xen:template include).
I've been unable to find a good example that utilises the template_post_render listener to add code somewhere in the middle of the template. If you know one, could you please link me to it.

Could Kier or Mike please correct the signature in the next XF update
 

Chris D

XenForo developer
Staff member
#9
PHP:
	public static function templatePostRender($templateName, &$content, array &$containerData, XenForo_Template_Abstract $template)
	{
		if ($templateName == 'option_list')
		{
			$content = str_replace('find', 'replace', $content);
		}
	}
That's a basic example.

$content is the HTML content of your template after it has been rendered.

str_replace does a simple find and replace.
 

Chris D

XenForo developer
Staff member
#11
If it's a problem just replace the template. But ultimately it will be you with the headache when people complain that other add-ons aren't working ;)