[英]Vapor/Leaf Custom Tag not returning correct value
I have registered a custom tag to return true if the index is odd and false if even as follows: 我已经注册了一个自定义标记,如果索引是奇数则返回true,如果是,则返回false:
class OddEvenTag: BasicTag {
let name = "OddEven"
func run(arguments: ArgumentList) throws -> Node? {
guard
arguments.count == 1,
let index = arguments[0]?.int
else { return Node(nil) }
print(index, index & 1)
return Node((index & 1) == 1)
}
}
The print statement produces satisfyingly good output: print语句产生令人满意的良好输出:
0 0
1 1
2 0
3 1
...
However, when I use the custom tag inside a #loop in a leaf file, such as 但是,当我在叶子文件中的#loop中使用自定义标记时,例如
#OddEven(offset){hello}##else(){bye}
It always instantiates the hello. 它总是实例化你好。 I've tried hard-coding false into the return statement and it doesn't change the outcome. 我已经尝试在返回语句中硬编码false,但它不会改变结果。 I have used a (more complex) custom tag before, so I know they can work. 我之前使用过(更复杂的)自定义标签,所以我知道它们可以正常工作。
In case you're wondering, I really want to use the tag to alternate the row background colours of a grid! 如果您想知道,我真的想使用标签来交替网格的行背景颜色!
A BasicTag
can only be used like this: BasicTag
只能像这样使用:
#TagName(variable)
and returns a value. 并返回一个值。
If you want to conditionally render a following block like this: 如果要有条件地渲染以下块,如下所示:
#TagName(variable) { show if true }
then you need to extend Tag
, and put your show/hide code in the shouldRender(tagTemplate:arguments:value:)
function. 然后你需要扩展Tag
,并将你的show / hide代码放在shouldRender(tagTemplate:arguments:value:)
函数中。
As a starting point, look at the If
tag , but instead of testing for true values, test for even values. 作为起点,查看If
标记 ,但不是测试真值,而是测试偶数值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.