Harald Joerg wrote:
> Emmanuel Quevillon asks:
> 
>> [...]
>> Harald Joerg wrote:
>>> Emmanuel Quevillon writes:
>>>
>>>> Hi,
>>>>
>>>> I am using TT to render DB output from search query box.
>>>> As I allow insensitive search, I'd like to bold the search
>>>> term in the output html page.
>>>> I can do it using replace vmethod but I did not find any
>>>> insensitive option to render it:
>>>>
>>>> [% entry.dbid.replace(query, "<b>$query</b>") %]
>>>> If the query is  "foo" and the db value is "Foo", the
>>>> replace doe snot work.
>>>> Does it exist a way to allow this functionality?
>>> Yep!
>>>
>>> The search string of the replace vmethod can contain all modifiers
>>> which are allowed in perl itself.  The only additional trick you need
>>> to use is to interpolate your variable into the regular expression:
>>>
>>> [% entry.dbid.replace("(?i:$query)", "<b>$query</b>") %]
>>>
>>> You did already take care for oddities like pattern metacharacters in
>>> your query variable, did you? ;-)
>> Hi Harald,
>>
>> Thanks for your help.
>> Also is there a possibility to $1 etc in replace method?
>> Something like:
>> [% entry.dbid.replace("(?i:$query)", "<b>$1</b>") %]
> 
> Yes, there is!
> 
> Two things to take into account:
> 
>  1) (?i:...) doesn't create a backreference for $1, so use an extra
>     pair of parens around your search string.
>  2) Use single quotes instead of double quotes in the replace string,
>     because $1 is not a TT variable.
> 
> [% entry.dbid.replace("((?i:$query))", '<b>$1</b>') %]
> 
> You might want to make sure that $query is a separate word, like this:
> 
> [% entry.dbid.replace("((?i:\\b$query\\b))", '<b>$1</b>') %]
> 
> Note the double backslashes: One of each pair gets "eaten" by the
> double quotes, the remaining makes Perl's \b escape for a word
> boundary.
Thanks a lot Harald, that did the work!
Cheers
Emmanuel

-- 
-------------------------
Emmanuel Quevillon
Biological Software and Databases Group
Institut Pasteur
+33 1 44 38 95 98
tuco at_ pasteur dot fr
-------------------------

_______________________________________________
templates mailing list
templates@template-toolkit.org
http://mail.template-toolkit.org/mailman/listinfo/templates

Reply via email to