JustSome person wrote:
Hey,
How would I do this in template toolkit. I am using a
Macro because I need to run this in many places...

basically I want to find a word within a string and
then add a apostrophe on it. But the word sometimes is
capitalized, sometimes not etc...this is what I have,
but I don't know what to do once I find a
match...ala..

MACRO changword(t)
t.replace('(?ig)catdog','');

I want to end up with ..
cat,dog

But sometimes it could be Cat,Dog

JustSome Person, ;)

Well you could use multiple replaces, but I'd suggest just creating your own 
virtual method. Like so:
$Template::Stash::SCALAR_OPS->{ changeword } =
 sub {
   # Make sure not to put any parameters in the
   # second portion of the replace. (Since they could be tainted
   # and do something bad, I think.)
   my ($scalar, $text1, $text2) = @_;
   $scalar =~ s/($text1)($text2)/$1,$2/ig;
return };

Then call it via: t.changeword('cat', 'dog');

or no params like so:
$Template::Stash::SCALAR_OPS->{ changeword } =
 sub {
   # Make sure not to put any parameters in the
   # second portion of the replace. (Since they could be tainted
   # and do something bad, I think.)
   my ($scalar) = @_;
   $scalar =~ s/(cat)(dog)/$1,$2/ig;
return };

Then call it via: t.changeword();

Disclaimer: I didn't check the syntax here.

If you don't have access to the underlying code, then that's a different matter 
all together. :)

-- Josh

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

Reply via email to