I using TAL (template attribute language) with my Pyramid Chameleon project. I want to use a loop using TAL to define attributes in my template.

<tal tal:repeat="reward_program reward_programs">
    <button tal:attributes="data-target repeat.reward_program.index">Button</button>
</tal>

This gets me close. But only yield me 'data-target' with a number in it (which increases) like so:

<button data-target="0">Button</button> 

I would like to have a fixed AND an option to have a variable string prefix that number in the data-target attribute like this:

<button data-target="#program0">Button</button>

How would you go about added a prefix to the TAL index variable? I would like to know how to make the prefix a fix string and having it defined in a variable.

有帮助吗?

解决方案

This answer works:

<tal tal:repeat="reward_program reward_programs">
    <button tal:attributes="data-target python:'#program-{}'.format(repeat.reward_program.index)>Button</button>
</tal>

其他提示

You could interpolate the repeat index with a string expression:

<tal tal:repeat="prog reward_programs">
    <button 
        tal:attributes="data-target string:#program-${repeat.prog.index}"
        >Button</button>
</tal>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top