RE: [Templates] Can't comparing strings (strings are treated as numbers)

[prev] [thread] [next] [Date index for 2004/12/23]

From: Jason Gottshall
Subject: RE: [Templates] Can't comparing strings (strings are treated as numbers)
Date: 21:06 on 23 Dec 2004
Oleg wrote:
> What I want to know is: How to compare two strings in TT ?

Since the string comparison operators aren't implemented int TT, try
using the list sort vmethod, like this:

	[% MACRO lt(str1, str2) BLOCK;
		templist =3D [ str1, str2 ];
		(str1 =3D=3D str2 or templist.sort.first =3D=3D str2) ? 0 : 1;
	END %]

	[% IF lt('2004-11-23', '2004-11-26') %]
	Good
	[% END %]

If you need this kind of thing often enough, you could define lt (and
gt, for that matter) as function refs in your perl script and pass them
in as variables:

	#!/usr/bin/perl
	use Template;

	my $func =3D {
		'lt' =3D> sub { $_[0] lt %_[1] },
		'gt' =3D> sub { $_[0] gt %_[1] },
	};

	my $tt =3D Template->new();
	local $/ =3D undef;
	$tt->process(<DATA>, $func);

	__DATA__
	[% IF func.lt('2004-11-23', '2004-11-26') %]
	Good
	[% END %]

_______________________________________________
templates mailing list
templates@xxxxxxxxxxxxxxxx.xxx
http://lists.template-toolkit.org/mailman/listinfo/templates

RE: [Templates] Can't comparing strings (strings are treated as numbers)
Jason Gottshall 21:06 on 23 Dec 2004

Generated at 08:56 on 15 Mar 2005 by mariachi v0.52