Not a bug Conditional with empty PreEscaped always return true

truonglv

Well-known member
Affected version
Latest
PHP:
PreEscaped {#4729 
  +value: ""
  +escapeType: "html"
}

I don't know this is as designed or be an bug? But if this is as designed, very weird.
 
It’s an object so if the object exists then it will evaluate to true - just like it would in PHP.

There's a special templater function called empty that can be used to evaluate such things:
HTML:
<xf:set var="$preEscaped"></xf:set>
{{ dump($preEscaped) }}
<xf:if is="empty($preEscaped)">
   Empty
<xf:else />
   Not empty
</xf:if>
This will output "Empty".

HTML:
<xf:set var="$preEscaped">Test</xf:set>
{{ dump($preEscaped) }}
<xf:if is="empty($preEscaped)">
   Empty
<xf:else />
   Not empty
</xf:if>
This will output "Not empty".

It's worth noting that if you can "trust" or you handle the escaping of the original value yourself, then you can just do this:
HTML:
<xf:set var="$var" value="" />
{{ dump(var) }}
<xf:if is="!$var">
   Empty
<xf:else />
   Not empty
</xf:if>
 
It’s an object so if the object exists then it will evaluate to true - just like it would in PHP.

There's a special templater function called empty that can be used to evaluate such things:
HTML:
<xf:set var="$preEscaped"></xf:set>
{{ dump($preEscaped) }}
<xf:if is="empty($preEscaped)">
   Empty
<xf:else />
   Not empty
</xf:if>
This will output "Empty".

HTML:
<xf:set var="$preEscaped">Test</xf:set>
{{ dump($preEscaped) }}
<xf:if is="empty($preEscaped)">
   Empty
<xf:else />
   Not empty
</xf:if>
This will output "Not empty".

It's worth noting that if you can "trust" or you handle the escaping of the original value yourself, then you can just do this:
HTML:
<xf:set var="$var" value="" />
{{ dump(var) }}
<xf:if is="!$var">
   Empty
<xf:else />
   Not empty
</xf:if>
Thank you. It's still weird with previous XF version :D. Cause i have used callback for some places.
 
Hi @Chris D In the XF 2.0.5 I have this issue again. Same above object but i receive two results.
PHP:
{{ dump(empty($var)) }} // output ""
{{ dump(!empty($var)) }} // output true
{{ dump($var is not empty) }} // output false

It's strange.
 


Write your reply...
Back
Top Bottom