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 Franois%>, but, when var = franois or Francois, it doesn't work. I tried also <%if var like Franois%>.

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 (Franois = francois)), but the operator "like" in the templates doesn't seem to work that way.

Thank you very much for your help.

Franois
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.

Franois
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

Franois
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

Franois

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 Sbastien, 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

Franois