Creating a grouped output using Template toolkit -
i have datafile
city : name london : john london : george paris : jean
and output
london john george paris jean
i feel want like
[% use namelist = datafile( 'namelist.txt' ) %] [% foreach city = unique namelist.city ??? %] [% city %] [% foreach name =???? %] [% name %] [%end %] [%end %]
it best kind of data munging in controller. template toolkit's job lay things out , make them pretty, not "calculations".
what want more like:
[% set list = formatter.group_them('namelist.txt') %] [% foreach item in list %] [% item.key %] [% foreach name in item.value %] [% name %] [% end %] [% end %]
it possible want in variety of ways. can use vmethods http://template-toolkit.org/docs/manual/vmethods.html split, create array, split again, build hash:
[% data = insert 'namelist.txt' %] [% lines = data.split("\n") %]\ [% list = {} %] [% foreach line in lines %] [% pair = line.split(': ') %] [% city = pair.0; name = pair.1; list.city.push(name) %] [% end %]
ok, have admit, mortified see in template inherited. things mortify others reason... @ time... :-)
a better approach insert
[% rawperl %]
block. @ least then, admitting, have code within template , doing operations in natural , efficient way.
Comments
Post a Comment