Gossamer Forum
Home : Products : Gossamer Forum : Discussion :

<%if var contains ... or <%if var like ...

Quote Reply
<%if var contains ... or <%if var like ...
Hi,

I try to use the tag <%if var contains François%>, but, when var = françois or Francois, it doesn't work. I tried also <%if var like François%>.

Is there a way to not consider the capital letters and the special charactors when we use the operators "contains" or "like"? The option "like" in users search engine works that way (it doesn't consider the capital letters and the special charactors (François = francois)), but the operator "like" in the templates doesn't seem to work that way.

Thank you very much for your help.

François
Quote Reply
Re: [Franco] <%if var contains ... or <%if var like ... In reply to
Hi,

No, I'm afraid not. You'd need to use a global for this.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] <%if var contains ... or <%if var like ... In reply to
Hi Alex,

Thank you for your answer.

I really need this simple feature. I'm ready to give 20$USD to the first person who will create me a good global to add this feature in my forum.

Thank you.

François
Quote Reply
Re: [Franco] <%if var contains ... or <%if var like ... In reply to
Ok, I found myself a solution:

global: "change"

Code:
sub {
my ($tag, $str) = @_;
$str =~ s|F|f|gi;
$str =~ s|ç|c|gi;
return { $tag => $str };
}


<%change(var => $var)%><%var%>

Thank you. Wink

François
Quote Reply
Re: [Franco] <%if var contains ... or <%if var like ... In reply to
You are changing a specific letter in one case, but it won't work in other ones.

A more general solution might be to create a global "contains"

<%contains (<%var%>, <%string%>)%>

This returns a tag CONTAINS which is True or False depending on the compare.
Code:
sub {
my ($var, $str) = @_;
m|$var|$str|gi && return ( {CONTAINS=>'True'} );
return ( {CONTAINS=>'False'} );

}


Not sure if this works, haven't tested it, but the idea is there.

Then in your templates, you'd use

<%if CONTAINS eq 'True'%> do something <%endif%>

The value of CONTAINS will always be the most recent test, so you can reuse this. You could also pass in the return variable, so you could use multiple tests, keeping the value of each for subsequent use.

There are still some bugs using the value of a function in a test, so you need to use this long form to get the value of the subroutine into a variable.




PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] <%if var contains ... or <%if var like ... In reply to
Thank you very much. The global I use is in fact this one:

Code:
sub {
my ($tag, $str) = @_;
$str =~ s|A|a|gi;
$str =~ s|À|a|gi;
$str =~ s|Â|a|gi;
$str =~ s|Ä|a|gi;
$str =~ s|à|a|gi;
$str =~ s|â|a|gi;
$str =~ s|ä|a|gi;
$str =~ s|B|b|gi;
$str =~ s|C|c|gi;
$str =~ s|Ç|c|gi;
$str =~ s|ç|c|gi;
$str =~ s|D|d|gi;
$str =~ s|E|e|gi;
$str =~ s|É|e|gi;
$str =~ s|Ê|e|gi;
$str =~ s|È|e|gi;
$str =~ s|Ë|e|gi;
$str =~ s|é|e|gi;
$str =~ s|ê|e|gi;
$str =~ s|è|e|gi;
$str =~ s|ë|e|gi;
$str =~ s|F|f|gi;
$str =~ s|G|g|gi;
$str =~ s|H|h|gi;
$str =~ s|I|i|gi;
$str =~ s|Ï|i|gi;
$str =~ s|Ì|i|gi;
$str =~ s|Î|i|gi;
$str =~ s|ï|i|gi;
$str =~ s|ì|i|gi;
$str =~ s|î|i|gi;
$str =~ s|J|j|gi;
$str =~ s|K|k|gi;
$str =~ s|L|l|gi;
$str =~ s|M|m|gi;
$str =~ s|N|n|gi;
$str =~ s|O|o|gi;
$str =~ s|Ö|o|gi;
$str =~ s|Ô|o|gi;
$str =~ s|Ò|o|gi;
$str =~ s|ö|o|gi;
$str =~ s|ô|o|gi;
$str =~ s|ò|o|gi;
$str =~ s|P|p|gi;
$str =~ s|Q|q|gi;
$str =~ s|R|r|gi;
$str =~ s|S|s|gi;
$str =~ s|T|t|gi;
$str =~ s|U|u|gi;
$str =~ s|Ü|u|gi;
$str =~ s|Û|u|gi;
$str =~ s|Ù|u|gi;
$str =~ s|ü|u|gi;
$str =~ s|û|u|gi;
$str =~ s|ù|u|gi;
$str =~ s|V|v|gi;
$str =~ s|W|w|gi;
$str =~ s|X|x|gi;
$str =~ s|Y|y|gi;
$str =~ s|Z|z|gi;

return { $tag => $str };
}


This global works perfectly for me. But maybe your solution use less ressources than mine in very long text ??

Thank you again! Smile

François

Last edited by:

Franco: Aug 21, 2003, 8:47 AM
Quote Reply
Re: [Franco] <%if var contains ... or <%if var like ... In reply to
In Reply To:
Thank you very much. The global I use is in fact this one:

...

This global works perfectly for me. But maybe your solution use less ressources than mine in very long text ??

This ought to be much less resource intensive on long or short strings, and accomplishes the same thing:

Code:
sub {
my ($tag, $str) = @_;
$str =~ tr|ÀÂÄàâäÇçÉÊÈËéêèëÏÌÎïìîÖÔÒöôòÜÛÙüûùA-Z|aaaaaacceeeeeeeeiiiiiioooooouuuuuua-z|;
return { $tag => $str };
}

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: [Jagerman] <%if var contains ... or <%if var like ... In reply to
For future reference, any product releases/updates after this post will contain support for "ieq", "ine", "ilike" etc. - these comparisons work exactly like the versions without "i", but perform a case-insensitive comparison. Unfortunately, this won't be able to take accented characters (e.g. Â, Ä, à, â, ä, Ç, ç, É, Ê, etc.) into account, but will work for non-accented A-Z case-insensitive comparisons.

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com
Quote Reply
Re: [Jagerman] <%if var contains ... or <%if var like ... In reply to
Thank you very much, Jason! Smile

It's strange that the ieq, ine, ilike functions will not support accented characters, since all the search functions (control panel, search post, users list) in GForum do not already make distinction between accented or not accented characters. For GForum, the username Sébastien, Sebastien or sebastien is the username of the same user. For GForum, the keywords "école" and "ecole" are the same keyword (in search post).

Why not simply integrate your global (in your previous post) in the ieq, ine and ilike functions? This way the comparaisons will always be done with non accented characters.

Thank you again for the global! Smile

François