代码之家  ›  专栏  ›  技术社区  ›  Dycey

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

  •  8
  • Dycey  · 技术社区  · 14 年前

    我正在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}" />
    

    允许我有选择地传入金属呼叫的id。。。

    PHP: isset(id) ? $id : NULL 和它的变体,但最终只是一个 id="0" 在生成的HTML中。

    有什么想法吗?

    3 回复  |  直到 11 年前
        1
  •  4
  •   Dycey    14 年前

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

    <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。

        2
  •  3
  •   nerdfiles    13 年前

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

    div class="" tal:define="VAR context.property"
        div class="" tal:attributes="class python:'grid_8 omega' if VAR else 'grid_8 alpha'"
    
        3
  •  0
  •   Michal - wereda-net Stinky Tofu    11 年前

    在PHP中:

    <div id="contentCenter" tal:attributes="id 
    
    php:isset(variable)&&isset(variable.property)?'IDVALUE':NULL">
    
    推荐文章