如何有条件地在 TAL (PHPTAL) 中添加 id 属性?

发布于 2024-09-19 15:39:38 字数 774 浏览 2 评论 0原文

我正在 PHPTAL 中创建一个表单元素模板文件。我希望能够选择性地传入字段的 id 属性...

到目前为止,代码如下所示:

<xml>
  <tal:block metal:define-macro="text">
    <label tal:condition="php: !isset(hideLabel) || isset(hideLabel) && !hideLabel">${field/label}</label>
    <input name="${name}" type="text" value="${field/value}" />
    <p tal:condition="exists:field/error">${field/error}</p>
  </tal:block>
</xml>

这正如广告中所宣传的那样。我想添加的是一些东西,比如

<input name="${name}" tal:attributes="id exists: id $id | $name" value="${field/value}" />

允许我选择从 METAL 调用中传递一个 id...

我应该采取不同的做法吗?我尝试过使用 PHP: isset(id) ? $id : NULL 及其变体,但最终在生成的 HTML 中以 id="0" 结尾。

有什么想法吗?

I'm creating a form elements template file in PHPTAL. I would like to be able to OPTIONALLY pass in an id attribute for a field...

So far the code looks like this:

<xml>
  <tal:block metal:define-macro="text">
    <label tal:condition="php: !isset(hideLabel) || isset(hideLabel) && !hideLabel">${field/label}</label>
    <input name="${name}" type="text" value="${field/value}" />
    <p tal:condition="exists:field/error">${field/error}</p>
  </tal:block>
</xml>

This works as advertised. What I'd like to add is something, like

<input name="${name}" tal:attributes="id exists: id $id | $name" value="${field/value}" />

to allow me to optionally pass in an id from the METAL call...

Should I be doing it differently? I've tried using PHP: isset(id) ? $id : NULL and variations thereof, but just end up with an id="0" in the resultant HTML.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

我是有多爱你 2024-09-26 15:40:00

在 PHP 中:

<div id="contentCenter" tal:attributes="id 

php:isset(variable)&&isset(variable.property)?'IDVALUE':NULL">

in PHP:

<div id="contentCenter" tal:attributes="id 

php:isset(variable)&&isset(variable.property)?'IDVALUE':NULL">
此生挚爱伱 2024-09-26 15:39:53

将 VAR 设置为包含即将使用的元素的 DIV:

div class="" tal:define="VAR context.property"
    div class="" tal:attributes="class python:'grid_8 omega' if VAR else 'grid_8 alpha'"

Set VAR at a DIV containing the soon to be used element:

div class="" tal:define="VAR context.property"
    div class="" tal:attributes="class python:'grid_8 omega' if VAR else 'grid_8 alpha'"
清晨说晚安 2024-09-26 15:39:50

如果其他人需要它,一个有效的答案是:

<xml>
  <tal:block metal:define-macro="text">
    <label tal:condition="not: exists:hideLabel">${field/label}</label>
    <input name="${name}" tal:attributes="id id | nothing" type="text" value="${field/value}" />
    <p tal:condition="exists:field/error">${field/error}</p>
  </tal:block>
</xml>

传入的变量是 id、name、一个名为 field 的数组和 hideLabel 。

请注意,我还设法将标签测试简化为我认为更惯用的 TAL 测试。

In case anyone else needs it, one working answer is:

<xml>
  <tal:block metal:define-macro="text">
    <label tal:condition="not: exists:hideLabel">${field/label}</label>
    <input name="${name}" tal:attributes="id id | nothing" type="text" value="${field/value}" />
    <p tal:condition="exists:field/error">${field/error}</p>
  </tal:block>
</xml>

Where passed in variables are id, name, an array named field, and hideLabel .

Note, that I've also managed to simplify the label test to something which I believe is more idiomatically TAL.

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