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

Creating a new or custom template tag?

Discussion in 'XenForo Development Discussions' started by Despair, Nov 9, 2011.

  1. Despair

    Despair Active Member

    It seems relatively easy to do within the main XenForo library, but how can I add a template tag with an addon? For example the usage in a template would be similar to other tags:
    <xen:customtag attr="$var" />
    I didn't have much luck with extending the template compiler (my only guess), and not sure how else to approach this, or would it not be possible currently?

    Okay this works: http://xenforo.com/community/threads/extending-the-core-library.14297/#post-189255

    Although this would require usage like:
    {xen:helper myhelper, $var, true}
    But I would prefer to have it as:
    <xen:mytag something="$var" />
    Is creating a custom tag that way possible?

    Also just found someone with the same question. :(
  2. Fuhrmann

    Fuhrmann Well-Known Member

    Would be good to know this.
  3. Mike

    Mike XenForo Developer Staff Member

    You can't and if you could, it would fall over during the upgrade system.
  4. Fuhrmann

    Fuhrmann Well-Known Member

    Any changes to do this in a future version?
  5. Mike

    Mike XenForo Developer Staff Member

    Not that likely because it's very easy to cause a fatal error for users with this. What are you trying to do?
    Fuhrmann likes this.
  6. Fuhrmann

    Fuhrmann Well-Known Member

    Something like this:

    class MyXenTag_Extend_Compiler extends XenForo_Template_Compiler
    Then, trying to override the function _setupDefaults() in this class, so I can use the function addTagHandlers to add my own tags.
  7. ragtek

    ragtek Guest

    isn't there the way to use a own template compiler and inject an own manipulated autoloader (which loads the own compiler if xenforo_template_compiler is called?:D

    (that's just a brain****)
  8. Fuhrmann

    Fuhrmann Well-Known Member

    I have tried created a new code event 'load_class_template'. Then use this:

    And in the listener, put this:

    if ($class == 'XenForo_Template_Compiler')
    $extend[] = 'MyXenTag_Extend_Compiler';
    Seems that I am not doing it the right way.
  9. Mike

    Mike XenForo Developer Staff Member

    That's how you're trying to do it, but what are functionality you actually trying to add functionality?
  10. Fuhrmann

    Fuhrmann Well-Known Member

    I have not created a new tag or function to do something yet. I just have tried to override this method, to add my own tag and see if this is possible. I know I can use helpers, but I want to know if it is possible to create new tags (and many other users).

    My tag will not be compiled if it is not in the $_tagHandlers variable...
  11. Marcus

    Marcus Well-Known Member

    Basically I want to display default avatars depending on custom user field. If a user enters a gender, there is a special gender avatar as current. If a user enters a special userfield_a, he gets a userfield_a avatar. Simple as that. This is very easy to do with a file edit in XenForo/Template/Helper/Core.php in protected static function _getDefaultAvatarUrl(array $user, $size), but how could you do this without a file edit.
  12. Mike

    Mike XenForo Developer Staff Member

    That's unrelated to this issue. :)

    But that is a pain unfortunately.
  13. Despair

    Despair Active Member

    Good thing I stopped trying. :) I had a couple more things to attempt, but it looks like I would have just wasted my time.

    In my case I was trying to create a tag very similar to <xen:avatar />. I can replicate the functionality with a helper function, but it's just much easier to input the attributes when using a tag vs. a function. I also prefer to use the tag in this case for a bit more consistency.

    BTW, I also sent you a PM, not sure if you had a chance to take a look at it yet (not related to this issue).
  14. infis

    infis Well-Known Member

    2developers: Please add load_class_template_... event.
    Hugilore111, Fuhrmann and Uniphix like this.
  15. Uniphix

    Uniphix Active Member

    I think extending the template tags is not that difficult, I've done it except the fact that I have to modify the XenForo Core everytime to re-add my tags in. And I second infis's response, adding a load_class_template, as well will be beneficial...

Share This Page