XF 2.1 Callback - how to return true or false in template?

Improvs

Member
I'm using callback inside xenforo template. I need to pass there some params: true or false.

That is what I got bellow:

Code:
PreEscaped {#341 ▼
  +value: """
    \n
    1\n
    """
  +escapeType: "html"
}

Now i have true data inside value (1) or false ('').
But i can't use xenforo conditional statements:

Code:
<xf:if is="{$isAnonymous} != '1'">
    test
</xf:if>

I always got TRUE result. The reason is PreEscaped type, how can i return only string or number or true,false from callback back to the template?
Please, any help.
 

Chris D

XenForo developer
Staff member
The root cause of this is that you are setting the value of the callback to a variable using <xf:set> and the contents of which, by default, will always be escaped and wrapped into a PreEscaped object.

There isn't really an ideal way around this. It isn't really what template callbacks were designed for.

Template callbacks are designed to return content, usually expected to be HTML, rather than the return value being used in template conditions.

I'd strongly advise you to find another way. You may want to consider extending the controller to add the isAnonymous variable and using Template modifications to make use of that variable as needed.

There is one workaround but it's clunky and hacky at best:

Code:
<xf:if is="$isAnonymous|htmlspecialchars != '1'">
    test
</xf:if>

This really only works because the htmlspecialchars filter turns any incoming value to a string first which causes the PreEscaped object to return its string value instead of the object. Also htmlspecialchars itself wouldn't change anything here because the value will only be '1' or '0' so it kind of works, but it's really not great code, unfortunately.

If you want to explain in more detail what you want to do then me or someone else might point you more specifically in the right direction.
 

Improvs

Member
If you want to explain in more detail what you want to do then me or someone else might point you more specifically in the right direction.

Thank you very much, I did not expect more. Since you asked.

I have a template in private extension Joint purchases 'alert_user_kst_participant_delete'.
This template is used to send alert to a user subscribed to a specific topic (event: when TC remove user from purchases participants).

This template alert_user_kst_participant_delete recieves XF:UserAlert params (User, Receiver, Addon and some extra data).
I need to pass in this template also one custom param from the Thread based, it can be true or false.

Usually when I need to pass extra params in the template, I'm going to make extenstion and extend default action functionality.
But I didn’t deal with alerts earlier. I tried to figure it out, but it seemed complicated for me now.

So, I chose a simpler and at the same time 'not great code' to achieve the necessary result.

If you could advice me, how to make it better and pass extra param into this template, I would really appreciate it.
 
Top