• 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?

Despair

Active member
#1
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:
Code:
<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?

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

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

Also just found someone with the same question. :(
http://xenforo.com/community/threads/adding-my-own-custom-xen-tag.21299/
 

Fuhrmann

Well-known member
#6
Not that likely because it's very easy to cause a fatal error for users with this. What are you trying to do?
Something like this:

PHP:
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.
 
R

ragtek

Guest
#7
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****)
 

Fuhrmann

Well-known member
#8
I have tried created a new code event 'load_class_template'. Then use this:

PHP:
XenForo_Application::resolveDynamicClass('XenForo_Template_Compiler', 'template');
And in the listener, put this:

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

Mike

XenForo developer
Staff member
#9
Something like this:

PHP:
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.
That's how you're trying to do it, but what are functionality you actually trying to add functionality?
 

Fuhrmann

Well-known member
#10
That's how you're trying to do it, but what are functionality you actually trying to add functionality?
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...
 

Marcus

Well-known member
#11
That's how you're trying to do it, but what are functionality you actually trying to add functionality?
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.
 

Mike

XenForo developer
Staff member
#12
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.
That's unrelated to this issue. :)

But that is a pain unfortunately.
 

Despair

Active member
#13
You can't and if you could, it would fall over during the upgrade system.
Good thing I stopped trying. :) I had a couple more things to attempt, but it looks like I would have just wasted my time.

Not that likely because it's very easy to cause a fatal error for users with this. What are you trying to do?
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).
 

Uniphix

Active member
#15
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...