How to add a prefix string to a TAL:attribute repeat index command?

349 views Asked by At

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.

2

There are 2 answers

0
fat fantasma On BEST ANSWER

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>
0
Martijn Pieters On

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>