htmltemplateschameleontemplate-metal

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


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.


Solution

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