Conditional Statements for XenForo 2

Conditional Statements for XenForo 2

smozgur

Active member
I'm trying to add conditional statement in the author_info of post_article_macro but nothing is working.

Code:
<xf:if is="in_array({$forum.node_id}, [11,12])">
</xf:if>

Code:
<xf:if is="$xf.visitor.canStartConversationWith($post.User)">
</xf:if>

Both of these don't work. Please help me.
$thread object and no curly braces in the in_array function.
HTML:
<xf:if is="in_array($thread.node_id, [11,12])">
 

JoyFreak

Well-known member
$thread object and no curly braces in the in_array function.
HTML:
<xf:if is="in_array($thread.node_id, [11,12])">
That's great thank you!
What about this?
Code:
<xf:if is="$xf.visitor.canStartConversationWith($post.User)">
<xf:button href="{{ link('conversations/add', null, {'to': $post.username}) }}" class="button--link">
</xf:button>
</xf:if>
 

smozgur

Active member
What about this?
Code:
<xf:if is="$xf.visitor.canStartConversationWith($post.User)">
<xf:button href="{{ link('conversations/add', null, {'to': $post.username}) }}" class="button--link">
</xf:button>
</xf:if>
This one looks ok to me.

As a general recipe: whenever you need to see which objects you can use in a template, just use {{dump(vars())}} in that template to see the available objects and their relations.
 

rdn

Well-known member
This code for After first post location:
$post.position % $xf.options.messagesPerPage == 0

Doesn't work on article page/1st page.
But works fine on question forums.

Please advice.
 

JoyFreak

Well-known member
This one looks ok to me.

As a general recipe: whenever you need to see which objects you can use in a template, just use {{dump(vars())}} in that template to see the available objects and their relations.
It didn't work for me and I couldn't quite understand the dump method, even after reading the guide Brogan posted above.
I did work out a different way of doing it though, outside of the macros.
 

smozgur

Active member
It didn't work for me
Perhaps you didn't set a button text, and can't see the button?

This works in post_macros template (I only added a button label as a difference):
HTML:
<xf:if is="$xf.visitor.canStartConversationWith($post.User)">
<xf:button href="{{ link('conversations/add', null, {'to': $post.username}) }}" class="button--link">
Start Conversation</xf:button>
</xf:if>

dump() is a function that you can print the object/variable content in entity form. vars() is the function to return all the variables in the current template context. So, {{dump(vars())}} prints all variables in the current context beautifully. It is also very well explained in the resource Brogan posted.
 

JoyFreak

Well-known member
Perhaps you didn't set a button text, and can't see the button?

This works in post_macros template (I only added a button label as a difference):
HTML:
<xf:if is="$xf.visitor.canStartConversationWith($post.User)">
<xf:button href="{{ link('conversations/add', null, {'to': $post.username}) }}" class="button--link">
Start Conversation</xf:button>
</xf:if>

dump() is a function that you can print the object/variable content in entity form. vars() is the function to return all the variables in the current template context. So, {{dump(vars())}} prints all variables in the current context beautifully. It is also very well explained in the resource Brogan posted.
Yeah there was a button label when I tried to add it but not when I posted it here. So no idea why it isn’t working. I was trying to add it in the author_info of the post_article_macros
 

smozgur

Active member
Yeah there was a button label when I tried to add it but not when I posted it here. So no idea why it isn’t working. I was trying to add it in the author_info of the post_article_macros
Oh, I see. You were trying to use author_info macro, I missed that part.

That's because there is no $post variable in that macro context. However, there is a $user variable which is direct access to the user entity. So, you just needed to use it $user instead of $post.

HTML:
<xf:if is="$xf.visitor.canStartConversationWith($user)">
<xf:button href="{{ link('conversations/add', null, {'to': $user.username}) }}" class="button--link">
Start Conversation</xf:button>
</xf:if>

Note: To see how dump() works, go to the same macro, and use it right after the macro definition line. So it will look like the following:

Rich (BB code):
<xf:macro name="author_info" arg-user="!" arg-fallbackName="">
    {{dump(vars())}}
    <div class="contentRow">

Then you should see how it works.
 

JoyFreak

Well-known member
Oh, I see. You were trying to use author_info macro, I missed that part.

That's because there is no $post variable in that macro context. However, there is a $user variable which is direct access to the user entity. So, you just needed to use it $user instead of $post.

HTML:
<xf:if is="$xf.visitor.canStartConversationWith($user)">
<xf:button href="{{ link('conversations/add', null, {'to': $user.username}) }}" class="button--link">
Start Conversation</xf:button>
</xf:if>

Note: To see how dump() works, go to the same macro, and use it right after the macro definition line. So it will look like the following:

Rich (BB code):
<xf:macro name="author_info" arg-user="!" arg-fallbackName="">
    {{dump(vars())}}
    <div class="contentRow">

Then you should see how it works.
Thank you, so that makes a little bit of sense.

I have this from an add-on:
<xf:if is="$feedback.canSubmitFeedback">

And trying to also add/move it to that area, but adding user. in the beginning doesn't work. Not sure if it's the same principle for this kind of thing?
 

smozgur

Active member
Thank you, so that makes a little bit of sense.
Glad to hear it helps a bit.

I have this from an add-on:
<xf:if is="$feedback.canSubmitFeedback">
I can't say anything certain about third-party add-ons.
However, the method was mentioned many times already: just dump the variables, and use the ones you need. In this case, there must be an entity variable called $feedback that third-party add-on exposes. If that entity has a method called canSubmitFeedback, then you would likely need to pass the $user entity with that method.
 

JoyFreak

Well-known member
Glad to hear it helps a bit.


I can't say anything certain about third-party add-ons.
However, the method was mentioned many times already: just dump the variables, and use the ones you need. In this case, there must be an entity variable called $feedback that third-party add-on exposes. If that entity has a method called canSubmitFeedback, then you would likely need to pass the $user entity with that method.
Yeah, that's why I was confused because that's what I did and it's not working. I've contacted the add-on author to see what they say.
 

JoyFreak

Well-known member
I've dumped the variables and I get null:
"feedback" => null
"user" => null

Does this mean it doesn't have any or have I done it wrong?
 

smozgur

Active member
I've dumped the variables and I get null:
"feedback" => null
"user" => null

Does this mean it doesn't have any or have I done it wrong?
null means it is not defined in the context as explained in the resource. It means it has nothing to do with that template, has not been passed to that template, not related to that template.

You should better wait to hear from the developer to find out the correct implementation.
 

JoyFreak

Well-known member
null means it is not defined in the context as explained in the resource. It means it has nothing to do with that template, has not been passed to that template, not related to that template.

You should better wait to hear from the developer to find out the correct implementation.
Thank you so much for your time, you've been really helpful. I appreciate that!
 

JoyFreak

Well-known member
Their support is currently having difficulties so been trying to work this one out myself. I have gotten far but stuck on this last bit that I've been trying to figure out the past couple of hours. I have the following in the post_article_macros:
Code:
<a href="{{ link ( 'feedback/add-feedback', $post.User, {'noRedirect':1, 'threadId':$feedback.threadId}) }}" class="userTitle" data-xf-click="overlay">

I've been working out the correct way to use the threadId and feedback.threadId but it seems like it's not correct as it's not showing the thread_id when I dump the variables. Any idea what would be the correct way? I've tried all sorts.
 

smozgur

Active member
Their support is currently having difficulties so been trying to work this one out myself. I have gotten far but stuck on this last bit that I've been trying to figure out the past couple of hours. I have the following in the post_article_macros:
Code:
<a href="{{ link ( 'feedback/add-feedback', $post.User, {'noRedirect':1, 'threadId':$feedback.threadId}) }}" class="userTitle" data-xf-click="overlay">

I've been working out the correct way to use the threadId and feedback.threadId but it seems like it's not correct as it's not showing the thread_id when I dump the variables. Any idea what would be the correct way? I've tried all sorts.
Try using $post.thread_id instead of $feedback.threadId. It should work then.
 

smozgur

Active member
It also depends on the Feedback controller action as I don't know its structure, but following should work.

HTML:
<a href="{{ link ( 'feedback/add-feedback', $post.User, {'noRedirect':1, 'threadId':$post.thread_id}) }}" class="userTitle" data-xf-click="overlay">

Can you paste a working link from another template? The link for feedback/add-feedback?
 
Top