是否可以在 tal:define 中添加 if 条件?
我将 python 与 Zope 一起使用,并尝试创建一个带有 if 条件的变量(如果可能)。我很难在网上找到示例和资源,所以我不知道这是否可能,但我确实知道你可以使用变量做这样的事情
tal:define="
var1 python:(container.securityUtil.sanitize(path('result/h_or_e | nothing') or ''));
var2 python:container.util.get_strdate(renewDate, -adj3);
true_or_false python:var1 or var2;
"
所以 true_or_false 将是 var1 或 var2。所以现在我的代码有一个变量名称initialized_by,它检查容器以查看路径是否存在,如果不存在则等于空“”。但我希望它包含“空白”一词,而不是空字符串。这是我的代码。
tal:omit-tag=""
tal:repeat="result results">
tal:define="
certification python:container.securityUtil.sanitize((path('result/cert | nothing') or ''));
initiated_by python:(container.securityUtil.sanitize(path('result/h_or_e | nothing') or ''))
"
<button type="button" class="blend"
tal:content="python:initiated_by" tal:attributes="aria-label python:'Initiated by, %s, %s' % (certification, initiated_by)">
Initiated<br/>By
</button>
我看不到“无”的容器路径意味着什么,但是当我使用 devtools 时,我可以看到该字段为空。它不是来自空的“”,因为我尝试在引用中放入空白,但什么也没发生。 “无”似乎是我无权访问的变量。我可以采取什么方法来解决这个问题?
I'm using python with Zope and I'm trying to create a variable with a if condition (if possible). I'm having a difficult time finding examples and resources online so I don't know if that's possible, but I do know that you can do something like this with variables
tal:define="
var1 python:(container.securityUtil.sanitize(path('result/h_or_e | nothing') or ''));
var2 python:container.util.get_strdate(renewDate, -adj3);
true_or_false python:var1 or var2;
"
So true_or_false will either be var1 or var2. So right now my code has a variable name initiated_by which checks a container to see if the path exists if it doesn't it equals an empty ' '. But instead of the empty string I want it to contain the word "blank". Here is my code.
tal:omit-tag=""
tal:repeat="result results">
tal:define="
certification python:container.securityUtil.sanitize((path('result/cert | nothing') or ''));
initiated_by python:(container.securityUtil.sanitize(path('result/h_or_e | nothing') or ''))
"
<button type="button" class="blend"
tal:content="python:initiated_by" tal:attributes="aria-label python:'Initiated by, %s, %s' % (certification, initiated_by)">
Initiated<br/>By
</button>
I can't see what the container path from "nothing" means or is but when I use devtools I can see that the field is empty. It's not from the empty ' ' because I tried putting blank inside the qoutes and nothing happened. The "nothing" seems to be a variable I don't have access to. What approach can I take to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用“and”,其中左侧是条件,右侧是条件为 True 时的值。
像这样的事情:
You can use an 'and' where the left side is the condition and the right side is the value when the condition is True.
Something like this:
我猜你想要的是:
如果路径表达式无效(这就像 try/ except ), il 将返回字符串“blank”。
但请注意,如果这是 h_or_e 的返回值,它将返回 False 或 None。
您还可以单独检查路径是否存在,如下所示:
nocall 会阻止路径结果在存在时执行。
I guess what you want is:
If the path expression is not valid (this is like try/except), il will return the string "blank".
Note however that it will return False or None if that's the return value of h_or_e.
You can also check separately if a path exists, like this:
The nocall prevents the path result to be executed in case it exists.