I am using Template Toolkit and the replace functionality isn't working. Here's my code...
开发者_开发百科 [% FOREACH item = file_conversions %]
[% item.name | replace('a','z') %]
<option value="[% item.name %]">[% item.name %]</option>
[% END %]
Replace in the above case doesn't do a thing on item.name. Just for kicks, I switched it to the following...
[% FOREACH item = file_conversions %]
[% item.name="Janie" | replace('a','z') %]
<option value="[% item.name %]">[% item.name %]</option>
[% END %]
And it works perfectly.
Does anyone have an idea why I can't do a simple replace on a variable in TT?
Replace appears in two contexts in TT: as a filter, and as a virtual method for a scalar.
Virtual method modifies the scalar:
[% item.name.replace('a', 'z') #item.name has changed %]
Filter modifies the output:
[% item.name |replace('a', 'z') #item.name has not changed %]
In your original problem case, you are using a filter, therefore the value of item.name remains the same. In the "working" example, you are filtering the value "Janie" and then assigning the result to item.name:
[% item.name =
'Janie' | replace('a', 'o') %]
This is an old post and you have moved on, but perhaps someone will find this helpful! (Actually, I did when I had to look a few things up to answer it!)
Hey guys! I figured it out! This works great!
[% FOREACH item = file_conversions %]
[% item.name = item.name.replace('a','z') %]
<option value="[% item.name %]">[% item.name %]</option>
[% END %]
精彩评论