简体   繁体   English

如何在TYPO3 Fluid中显示整数0但不为空或NULL

[英]How to show integer 0 but not empty or NULL in TYPO3 Fluid

In my TYPO3 Fluid template I have a value where I would like to check if its not empty before showing it.在我的 TYPO3 Fluid 模板中,我有一个值,我想在显示它之前检查它是否不为空。 My way now:我现在的方法:

<f:if condition="{myvalue}">
<div class="myclass">{myvalue}</div>
</f:if>

This works when I type in the backend a value like "test" or "2", and if I dont type anything it won't show the div tag.当我在后端输入一个像“test”或“2”这样的值时,这会起作用,如果我不输入任何东西,它就不会显示 div 标签。 But when I type in the backend "0", the condition is also not true.但是当我在后端输入“0”时,条件也不成立。 How can I fix that the integer 0 will be showed, and if its empty (in database NULL) not be showed?如何修复将显示整数 0,并且如果不显示其为空(在数据库中为 NULL)的问题? (its very common that the value will be 0) (该值为 0 是很常见的)

Btw i tried things like:顺便说一句,我试过这样的事情:

<f:if condition="{myvalue} !=NULL">
<f:if condition="{myvalue} >= 0">

But then also the empty value's wil be show.但随后也会显示空值。 If I do如果我做

<f:debug>{myvalue}</f:debug>

I get things like:我得到类似的东西:

myvalue = NULL 
myvalue = 0 
myvalue = "test"

So only the first one must not been shown.所以只有第一个不能显示。

I hope someone can help me, thank you.我希望有人可以帮助我,谢谢。

There are two solutions first is transient field in your model of bool type which getter just checks if value is not null, but additionally returns true if value is 0 (actually in most languages 0 IS a value)有两种解决方案,首先是bool类型模型中的transient字段,getter 仅检查值是否为空,但如果值为0 ,则另外返回true (实际上在大多数语言中0是一个值)

Second solution is even more universal, it's just writing custom ViewHelper, which will allow uou to check if value is 0 or has value:第二种解决方案更加通用,它只是编写自定义 ViewHelper,这将允许您检查 value 是否为 0 或具有 value:

<?php
namespace VENDOR\YourExt\ViewHelpers;

class notEmptyOrIsZeroViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper {

    /**
     * @param mixed $value Value to check
     *
     * @return bool the rendered string
     */
    public function render($value) {
        return ($value === 0 || $value === '0' || $value) ? true : false;
    }
}

So you can later use this as a condition for common <f:if > condition like:因此,您可以稍后将其用作常见<f:if >条件的条件,例如:

<f:if condition="{yourNameSpace:notEmptyOrIsZero(value: myValue)}">
    <f:then>Now it recognizes 0 as a value</f:then>
    <f:else>It still DOESN'T recognize 0 as a value</f:else>
</f:if>

I had a similar case, where I wanted to check a fluid variable for 0 or positive integers.我有一个类似的案例,我想检查流体变量是否为 0 或正整数。 A simple >= 0 comparison wouldn't work.简单的>= 0比较不起作用。 In TYPO3 10 LTS I could resolve this problem by doing this:在 TYPO3 10 LTS 中,我可以通过执行以下操作来解决此问题:

<f:if condition="{value} === 0 || {value * 1} > 0">
    value is zero or positive integer
</f:if>

(Note: this will also allow integer strings, such as "123" or "1st", but not "val3" - basically as you would expect when casting a string as integer in PHP.) (注意:这也将允许整数字符串,例如“123”或“1st”,但不允许“val3” - 基本上就像在 PHP 中将字符串转换为整数时所期望的那样。)

If you just want to check that {value} is not null or empty (but allow zero as a valid value), you can simplify the condition to:如果您只想检查{value}不为 null 或为空(但允许零作为有效值),您可以将条件简化为:

<f:if condition="{value} === 0 || {value}">
    value is set and not empty
</f:if>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM