1. 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 ?

Discussion in 'XenForo Development Discussions' started by MOZ, Jan 31, 2013.

  1. MOZ

    MOZ Well-Known Member

    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 &$paramsXenForo_Template_Abstract $template)
        {
            if (
    $templateName == 'member_list')
            {
                
    $templateName 'member_list_item';
            }
        }
    }
     
  2. MOZ

    MOZ Well-Known Member

    Can anyone confirm this please.
     
  3. xfrocks

    xfrocks Well-Known Member

    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.
     
    cclaerhout likes this.
  4. MOZ

    MOZ Well-Known Member

    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:
    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.
     
  5. havanaclub

    havanaclub Member

    or event template_post_render
     
  6. Chris D

    Chris D XenForo Developer Staff Member

    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 likes this.
  7. Luke F

    Luke F Well-Known Member

    They're always changing stuff regarding pass by reference vs value
     
    MOZ likes this.
  8. MOZ

    MOZ Well-Known Member

    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
     
  9. Chris D

    Chris D XenForo Developer Staff Member

    PHP:
        public static function templatePostRender($templateName, &$content, array &$containerDataXenForo_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.
     
  10. MOZ

    MOZ Well-Known Member

    Classic hack and slash at HTML using str_replace and preg_replace. :cautious:
     
  11. Chris D

    Chris D XenForo Developer Staff Member

    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 ;)
     
  12. MOZ

    MOZ Well-Known Member

    I know, I know. Back to the desk then. :)
     

Share This Page