JustSome person wrote:
thanks, but as you suspected...I don't have access to
the underlying code. Thats the problems.

this has to be done in template toolkit alone!

Ok, well, I thought I'd be clever and work up a hack for you and I found a TT 
bug which makes it ineffective. It appears the split vmethod does not work 
correctly with args. I'll fire off a new email to the mailing list to notify 
Andy of this.

Anyhow, you can technically do this search and replace by using .match and .split. 
(Probably not very efficient, but it gets the job done.) Here is the hack I wrote up. 
Note that I use the "split2" virtual method below which is the fixed version of 
the internal split vmethod. (Note, this can probably optimized a bit, but I don't have 
all day to work on this.) :)

-------------------------------------------------------------
[% t = 'catdog fooo catdog bar abcd asdfkj CatDog akdjd catDog abc CATDOG' %]

Start String is: [% t %]<br>

[% temp_string = t %]
[% all_matches = [] %]
[% string_parts = [] %]
[% WHILE temp_string %]
 [% matches = temp_string.match('(?i)(cat)(dog)') %]
 [% IF matches && matches.size() %]
   [% all_matches.push("${matches.0},${matches.1}") %]
   [% array = temp_string.split2(matches.join(''), 2) %]
   [% string_parts.push(array.0) %]
   [% temp_string = array.1 %]
 [% ELSE %]
   [% string_parts.push(temp_string) %]
   [% temp_string = '' %]
 [% END %]
[% END %]

[% new_string = '' %]
[% FOREACH string IN string_parts %]
 [% IF loop.index() > all_matches.size() - 1 %]
   [% new_string = new_string _ string %]
   [% LAST %]
 [% END %]
 [% index = loop.index() %]
 [% new_string = new_string _ string _ all_matches.$index %]
[% END %]

New String is: [% new_string %]<br>
-----------------------------------------------------------

Here's the split2 vmethod for those interested:
$Template::Stash::SCALAR_OPS->{ split2 } =
 sub {
   my ($str, $split, @args) = @_;
   $str = '' unless defined $str;
   return [ defined $split ? split($split, $str, $args[0])
            : split(' ', $str, $args[0]) ];
 };

_______________________________________________
templates mailing list
[email protected]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to