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

Lack of interest Templates: check if property/class exists

Arty

Well-known member
#1
This is a double request for 2 similar features for template system:
  • Ability to check if style property exists
  • Ability to check if class exists
Both abilities would allow style authors to add add-on specific code to styles by checking ether style property or class name.

Several suggestions for possible syntax:

1. For style properties only:
Code:
<xen:if is="@propertyName">
Currently this will trigger error because property does not exist

2. For anything:
Code:
<xen:if is="exists(property @propertyName)"> - for style properties
<xen:if is="exists(@propertyName)"> - also for style properties. @ before name gives away that it is style property
<xen:if is="exists(class XenForo_Application)"> - for class
or
Code:
<xen:if is="exists property @propertyName"> - for style properties
<xen:if is="exists @propertyName"> - also for style properties. @ before name gives away that it is style property
<xen:if is="exists class XenForo_Application"> - for class
This syntax could be extended further with ability to add more different checks based on keyword before item name.
 

cclaerhout

Well-known member
#3
Use the xen tag property:

HTML:
<xen:if is="{xen:property propertyName}">
    Exists
<xen:else />
    Doesn't not exist
</xen:if>
=>Doesn't not exist

HTML:
<xen:if is="{xen:property mutedTextColor}">
    Exist
<xen:else />
    Doesn't not exist
</xen:if>
=>Exists

You will have probably to use this syntax in the official TMS when doing some search & replace

Edit: only in callbacks (see here)
 

cclaerhout

Well-known member
#5
Code:
<xen:if is="exists @propertyName"> - also for style properties. @ before name gives away that it is style property
<xen:if is="exists class XenForo_Application"> - for class
I'm curious, why do you want to check if a class exists? Is it to check if an addon is installed? (speaking of this it would be nice to have a xen tag to check if an addon is active or not)
If you really need a class or method check, you can create your own helper (requires between 5-15 minutes). Read this great tutorial of Fuhrmann and use this as helper function:

PHP:
    public static function checkClassOrMethod($class, $method == null)
    {
        if($method)
        {
            return method_exists($class, $method);
        }

        return class_exists($class);
    }
 

Chris D

XenForo developer
Staff member
#6
What's this syntax?
Curly syntax

{xen:if 'condition', 'if true', 'if false'}

You see this a lot where you want to use conditionals "inline"... e.g.

Code:
class="{xen:if {$message.isIgnored}, 'ignored', ''}"
Or of course:

Code:
class="{xen:if {$message.isIgnored}, 'ignored'}"
 

cclaerhout

Well-known member
#7
Curly syntax

{xen:if 'condition', 'if true', 'if false'}

You see this a lot where you want to use conditionals "inline"... e.g.

Code:
class="{xen:if {$message.isIgnored}, 'ignored', ''}"
Or of course:

Code:
class="{xen:if {$message.isIgnored}, 'ignored'}"
I meant the syntax with '== true', '==false'. The operator is inside the return value...