是否可以在 tal:define 中添加 if 条件?

发布于 2025-01-13 05:22:32 字数 1047 浏览 7 评论 0原文

我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白况 2025-01-20 05:22:32

您可以使用“and”,其中左侧是条件,右侧是条件为 True 时的值。
像这样的事情:

<div tal:define="myvarible python: CONDITION and 'value when condition is True' or 'value when condition is False'">

</div>

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:

<div tal:define="myvarible python: CONDITION and 'value when condition is True' or 'value when condition is False'">

</div>

我是男神闪亮亮 2025-01-20 05:22:32

我猜你想要的是:

tal:define="certification result/h_or_e | string:blank"

如果路径表达式无效(这就像 try/ except ), il 将返回字符串“blank”。

但请注意,如果这是 h_or_e 的返回值,它将返回 False 或 None。

您还可以单独检查路径是否存在,如下所示:

tal:define="path_exists exists:nocall:result/h_or_e"

nocall 会阻止路径结果在存在时执行。

I guess what you want is:

tal:define="certification result/h_or_e | string:blank"

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:

tal:define="path_exists exists:nocall:result/h_or_e"

The nocall prevents the path result to be executed in case it exists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文