1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. This forum has been archived. New threads and replies may not be made. All add-ons/resources that are active should be migrated to the Resource Manager. See this thread for more information.

To developers: please do not change $templateName on template_create

Discussion in 'Development Tutorials [Archive]' started by xfrocks, Apr 10, 2011.

  1. xfrocks

    xfrocks Well-Known Member

    Hi all,

    Today I ran into a weird problem when one of my listener worked incorrectly. After quite a lot of time, I have found the problem: another addon changed the $templateName in its listener for `template_create`. I'm not sure why XenForo passes the $templateName as reference but I'm sure that: YOU SHOULD NOT CHANGE IT IN YOUR LISTENERS.

    Let me explain why
    1. You will break other `template_create` listeners. The listener for `template_create` relies on $templateName to do its job (mostly to preload templates, at least for me). And boom, you change it to something else, no preload can be done anymore... That's a waste of resources. Okie, you can change your priority to minimize the problem but... read on
    2. You will break YOUR other `template_create` listeners. Imagine 2 of your addons want to change the $templateName. It's obvious that only 1 of them will succeed. Well, congratulation, 50% of your addons doesn't work.
    3. You will break all `template_post_render` listener. I'm not sure how everybody does it but on my listeners for `template_post_render`, I always rely on $templateName to decide what to do. Well, if it was changed, nothing will be done. Thank you very much for that :)
    That's it. My rant stops here. Please don't do that. If you need to inject something to output, please find a hook or please do some hacky search and replace with `template_post_render`.

    Thank you for reading. Gotta find a workaround for my problem now :(
  2. compwhizii

    compwhizii Active Member

    Unless you're changing the template intentionally due to some condition.
  3. xfrocks

    xfrocks Well-Known Member

    Of course people do that intentionally. What I was trying to say is: even if you need to change it, you shouldn't. There should be other ways to achieve what you want.
  4. Jaxel

    Jaxel Well-Known Member

    I actually think this is a good feature... I don't use it, but I can imagine the reasons for it. Sometimes you want to load up a different template than the one you're supposed to get, if certain conditions are met. I can imagine this being very useful if someone wanted to create an articles forum mod... hell, even making a website like vb.org.
    Darkimmortal likes this.

Share This Page