Conditional Statements for XenForo 2

Conditional Statements for XenForo 2

I would like to change something in conversation page.

message_macros

Code:
<xf:if is="$template == 'conversation_view'">
     A
<xf:else />
     B
</xf:if>

However, I can only see B even in conversation page which shows data-template="conversation_view" in the header.
 
If I wanted to write a conditional for replacing a user's username with a custom field input within a specific node, how would one do that?

I have this, but I know it's wrong and the brain's gone foggy:

Code:
<xf:if is="{{$forum.node_id == 53 && $user.Profile.custom_fields.DDCharName}}"><xf:username user="$user.Profile.custom_fields.DDCharName" rich="true" defaultname="{$fallbackName}" itemprop="name" /><xf:else /><xf:username user="$user" rich="true" defaultname="{$fallbackName}" itemprop="name" /></xf:if>
 
I don't think this will work with <xf:username .. />. This template function takes a User object ($user in your code) and this has the user name included.

So, if you just want to display a user name as plain text, you could do something like:

HTML:
<xf:if is="$forum.node_id == 53 && $user.Profile.custom_fields.DDCharName">
[COLOR=rgb(224, 224, 224)]    $user.Profile.custom_fields.DDCharName
<xf:else />
   <!-- // your default code -->
</xf:if>

If you want more fancy stuff (like displaying an alternative user overlay), I think you will need to code an add-on.[/COLOR]
 
I don't think this will work with <xf:username .. />. This template function takes a User object ($user in your code) and this has the user name included.

So, if you just want to display a user name as plain text, you could do something like:

HTML:
<xf:if is="$forum.node_id == 53 && $user.Profile.custom_fields.DDCharName">
[COLOR=rgb(224, 224, 224)]    $user.Profile.custom_fields.DDCharName
<xf:else />
   <!-- // your default code -->
</xf:if>

If you want more fancy stuff (like displaying an alternative user overlay), I think you will need to code an add-on.[/COLOR]
Yeah, I assumed it would be something to do with that. I tried just the custom field, that doesn't work, either.
 
Yeah, I assumed it would be something to do with that. I tried just the custom field, that doesn't work, either.

sorry, my code was messed up.

Try this one:

HTML:
<xf:if is="$forum.node_id == 53 && $user.Profile.custom_fields.DDCharName">
    {{$user.Profile.custom_fields.DDCharName}}
<xf:else />
   <!-- // your default code -->
</xf:if>
 
message_macro
o.k. so it's message_macros, so this part should work:

{{$user.Profile.custom_fields.DDCharName}}

but you won't have access to $forum. Try replacing that part with $xf.reply.contentKey == 'node-53'.

So, try this (untested):
HTML:
<xf:if is="$xf.reply.contentKey == 'node-53' && $user.Profile.custom_fields.DDCharName">
    {{$user.Profile.custom_fields.DDCharName}}
<xf:else />
   <!-- // your default code -->
</xf:if>
 
o.k. so it's message_macros, so this part should work:

{{$user.Profile.custom_fields.DDCharName}}

but you won't have access to $forum. Try replacing that part with $xf.reply.contentKey == 'node-53'.

So, try this (untested):
HTML:
<xf:if is="$xf.reply.contentKey == 'node-53' && $user.Profile.custom_fields.DDCharName">
    {{$user.Profile.custom_fields.DDCharName}}
<xf:else />
   <!-- // your default code -->
</xf:if>
That's still not doing it, sadly.
 
Tried this approach, as well (we added sub-forums, lol):

HTML:
<xf:if is="in_array({$thread.forum_id}, [53, 55, 56, 57]) && !empty($user.Profile.custom_fields.DDCharName)">
    {{$user.Profile.custom_fields.DDCharName}}
<xf:else />
    <xf:username user="$user" rich="true" defaultname="{$fallbackName}" itemprop="name" />
</xf:if>
 
Last edited:
HTML:
<xf:if is="$xf.reply.containerKey == 'node-53' && $user.Profile.custom_fields.DDCharName">
    {{$user.Profile.custom_fields.DDCharName}}
<xf:else />
            <h4 class="message-name"><xf:username user="$user" rich="true" defaultname="{$fallbackName}" itemprop="{{ $includeMicrodata ? 'name' : '' }}" /></h4>
</xf:if>

There will be no link to the user profile though.
 
Thank you, @Lee - was just testing myself and found the issue. It must be containerKey and not contentKey for nodes.. :-D
 
  • Like
Reactions: Lee
Fur future reference, dump the vars in the template you are working with and it will immediately show what is available.

And/or use the browser inspector.
 
Top Bottom