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

XF 2.0 PHP callback inside if

#1
Can I make a php callback for a xf:if?

Using that callback:
Code:
<xf:callback class="To\\The\\Class" method="isAmp" params="[]">
</xf:callback>
Inside an if:
Code:
<xf:if "here">
 
#3
Wrap it in a <xf:if hascontent="true">?
Can you explain better what is that hascontent?

I tried like this:
Code:
<xf:set var="$test">
        <xf:callback class="to\\the\\class" method="isAmp" params="[]"></xf:callback>
    </xf:set>
<xf:if is="$test">
        XXXX
    <xf:else />
        YYYY
    </xf:if>
isAmp returns false.

The code above doesn't work properly, in fact it will print XXXX every time.
 

Snog

Well-known member
#4
So far as I know, a callback used in a template has to return what you want to display (a string, a rendered template or HTML created in PHP) or NULL. And you can't use it to set a variable value in a template.

Your PHP would look something like this...
Code:
public static function getIsAmp($contents, $params)
{
    .... do whatever ...

    if($result_from_whatever)
    {
        $value = '<span class="aclass">' . $result_from_whatever . '</span>';
    }
    else
    }
        $value = null;
    }

    return $value;
}
So, your template would look something like this...
Code:
<xf:if hascontent="true">
        <xf:callback class="to\\the\\class" method="getIsAmp" params="[]"></xf:callback>
<xf:else />
        YYYY
</xf:if>
AND your method name MUST start with 'get'.
 
Last edited:
#5
I solved by making the HTML with the PHP callback, just another question:
You said that I cannot use xf:set variables got by a php callback, but if I just use them with {$var} I can see them in the HTML.

Also, in your case you are creating the "XXXX" inside the PHP, and the YYYY from the template, right?
 

Snog

Well-known member
#7
After a quick test, it also appears that "isXXX" or "getXXX" are valid "read only" function names.
 
#8
After a quick test, it also appears that "isXXX" or "getXXX" are valid "read only" function names.
Yes but it's strange that appears only XXXX if I make something like this:
Code:
<xf:if is="$test">XXXX<xf:else>YYYY</xf:if>
$test is false.
 

Snog

Well-known member
#9
That is why you can't assign a variable value from a callback. If it returns something other than what is to be displayed, you get this as the value...
Code:
PreEscaped {#360 ▼
  +value: ""
  +escapeType: "html"
}
Which equates to "true" in the if is= test.

Dump your "false" variable in the template to see what I mean {{dump ($var)}}