Discussion:
[SMARTY] fuzzy comparisons
Travis Low
2003-01-15 15:02:53 UTC
Permalink
I'd like to compare two strings in the template, and take one action if they
are the same, and another action if they are not.

One string comes from an XML file. The other string comes from a CSV file. I
cannot (easily) manipulate the strings from within PHP, because I'd have to do
all of them, or none of them.

It turns out that sometimes one or the other string has leading or trailing
spaces. This makes the comparison evaluate to false, which is a problem.

I tried doing this:

{if $string1|replace:" ":"" == $string2|replace:" ":""}

but that caused a template error. I also tried this:

{assign var=s1 value=$string1|replace:" ":""}
{assign var=s2 value=$string2|replace:" ":""}
{if $s1 == $s2}
...
{/if}

but I got an error that way too. Any suggestions?

Thanks in advance.

cheers,

Travis
--
Travis Low
<mailto:***@dawnstar.org>
<http://dawnstar.org/travis>
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jeff Dickens
2003-01-15 15:33:09 UTC
Permalink
try this:

{assign var=var1 value="stuff"}
{assign var=var2 value="stuff "}
{assign var=var2a value=$var2|trim}
{if $var1 = $var2a}
They match.
{else}
They don't.
{/if}

I still haven't figured out where it says that all php functions are
transparently imported as smarty modifiers, but a apparently they are.
(i.e. trim isn't in the smarty docs)
-----Original Message-----
Sent: Wednesday, January 15, 2003 10:03 AM
Subject: [SMARTY] fuzzy comparisons
I'd like to compare two strings in the template, and take one
action if they
are the same, and another action if they are not.
One string comes from an XML file. The other string comes from a CSV file.
I
cannot (easily) manipulate the strings from within PHP, because I'd have to do
all of them, or none of them.
It turns out that sometimes one or the other string has leading or trailing
spaces. This makes the comparison evaluate to false, which is a problem.
{if $string1|replace:" ":"" == $string2|replace:" ":""}
{assign var=s1 value=$string1|replace:" ":""}
{assign var=s2 value=$string2|replace:" ":""}
{if $s1 == $s2}
...
{/if}
but I got an error that way too. Any suggestions?
Thanks in advance.
cheers,
Travis
--
Travis Low
<http://dawnstar.org/travis>
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Travis Low
2003-01-15 15:38:33 UTC
Permalink
Thanks!!! I looked EVERYWHERE for a "trim" modifier, so that is very useful
information. (That PHP string functions become smarty modifiers, I mean.)

And to think I was about to resign myself to writing a plugin. :-)

cheers,

Travis
Post by Jeff Dickens
{assign var=var1 value="stuff"}
{assign var=var2 value="stuff "}
{assign var=var2a value=$var2|trim}
{if $var1 = $var2a}
They match.
{else}
They don't.
{/if}
I still haven't figured out where it says that all php functions are
transparently imported as smarty modifiers, but a apparently they are.
(i.e. trim isn't in the smarty docs)
-----Original Message-----
Sent: Wednesday, January 15, 2003 10:03 AM
Subject: [SMARTY] fuzzy comparisons
I'd like to compare two strings in the template, and take one
action if they
are the same, and another action if they are not.
One string comes from an XML file. The other string comes from a CSV file.
I
cannot (easily) manipulate the strings from within PHP, because I'd have to do
all of them, or none of them.
It turns out that sometimes one or the other string has leading or trailing
spaces. This makes the comparison evaluate to false, which is a problem.
{if $string1|replace:" ":"" == $string2|replace:" ":""}
{assign var=s1 value=$string1|replace:" ":""}
{assign var=s2 value=$string2|replace:" ":""}
{if $s1 == $s2}
...
{/if}
but I got an error that way too. Any suggestions?
Thanks in advance.
cheers,
Travis
--
Travis Low
<http://dawnstar.org/travis>
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
Travis Low
<mailto:***@dawnstar.org>
<http://dawnstar.org/travis>
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Monte Ohrt
2003-01-15 15:29:00 UTC
Permalink
what error did you get? the syntax looks ok.

this will work better anyways:

{if $string1|trim eq $string2|trim}
...
{/if}
Post by Travis Low
I'd like to compare two strings in the template, and take one action if they
are the same, and another action if they are not.
One string comes from an XML file. The other string comes from a CSV file. I
cannot (easily) manipulate the strings from within PHP, because I'd have to do
all of them, or none of them.
It turns out that sometimes one or the other string has leading or trailing
spaces. This makes the comparison evaluate to false, which is a problem.
{if $string1|replace:" ":"" == $string2|replace:" ":""}
{assign var=s1 value=$string1|replace:" ":""}
{assign var=s2 value=$string2|replace:" ":""}
{if $s1 == $s2}
...
{/if}
but I got an error that way too. Any suggestions?
Thanks in advance.
cheers,
Travis
--
Travis Low
<http://dawnstar.org/travis>
--
Monte Ohrt <***@ispi.net>
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Travis Low
2003-01-15 15:43:40 UTC
Permalink
Damn, I can't reproduce the error. It might have been a typo. It was
something about missing parenthesis. I assumed it was bad syntax because I
wasn't using any parenthesis.

As for your if statement, the variables I'm dealing with look like this:

$x._NODE_.table80[foo80]._NODE_.EmployeeLastNameCont[0]._NODE_

so assigning them first makes the code a lot easier to read. As always,
thanks for your help.

cheers,

Travis
Post by Monte Ohrt
what error did you get? the syntax looks ok.
{if $string1|trim eq $string2|trim}
...
{/if}
Post by Travis Low
I'd like to compare two strings in the template, and take one action if they
are the same, and another action if they are not.
One string comes from an XML file. The other string comes from a CSV file. I
cannot (easily) manipulate the strings from within PHP, because I'd have to do
all of them, or none of them.
It turns out that sometimes one or the other string has leading or trailing
spaces. This makes the comparison evaluate to false, which is a problem.
{if $string1|replace:" ":"" == $string2|replace:" ":""}
{assign var=s1 value=$string1|replace:" ":""}
{assign var=s2 value=$string2|replace:" ":""}
{if $s1 == $s2}
...
{/if}
but I got an error that way too. Any suggestions?
Thanks in advance.
cheers,
Travis
--
Travis Low
<http://dawnstar.org/travis>
--
Travis Low
<mailto:***@dawnstar.org>
<http://dawnstar.org/travis>
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
M Mohr
2003-01-15 16:21:39 UTC
Permalink
Hello,
Post by Travis Low
Damn, I can't reproduce the error. It might have been a typo. It was
something about missing parenthesis. I assumed it was bad syntax because I
wasn't using any parenthesis.
$x._NODE_.table80[foo80]._NODE_.EmployeeLastNameCont[0]._NODE_
so assigning them first makes the code a lot easier to read. As always,
thanks for your help.
cheers,
Travis
Post by Monte Ohrt
what error did you get? the syntax looks ok.
hmm, i get an error on
{if $string1|replace:" ":"" == $string2|replace:" ":""}
with smarty-2.3.1

the generated code is:
<?php if ($this->_run_mod_handler('replace', true, $this->_tpl_vars['string1'], ") ":" " == $string2|replace:" ":" "): ?>

using the same template with the latest cvs checkout,
everything is fine though and the code is:

<?php if ($this->_run_mod_handler('replace', true, $this->_tpl_vars['string1'], " ", "") == $this->_run_mod_handler('replace', true, $this->_tpl_vars['string2'], " ", "")): ?>

HTH
messju
Post by Travis Low
Post by Monte Ohrt
{if $string1|trim eq $string2|trim}
...
{/if}
Post by Travis Low
I'd like to compare two strings in the template, and take one action if
they are the same, and another action if they are not.
One string comes from an XML file. The other string comes from a CSV
file. I cannot (easily) manipulate the strings from within PHP, because
I'd have to do all of them, or none of them.
It turns out that sometimes one or the other string has leading or
trailing spaces. This makes the comparison evaluate to false, which is a
problem.
{if $string1|replace:" ":"" == $string2|replace:" ":""}
{assign var=s1 value=$string1|replace:" ":""}
{assign var=s2 value=$string2|replace:" ":""}
{if $s1 == $s2}
...
{/if}
but I got an error that way too. Any suggestions?
Thanks in advance.
cheers,
Travis
--
Travis Low
<http://dawnstar.org/travis>
--
Travis Low
<http://dawnstar.org/travis>
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Travis Low
2003-01-15 16:37:20 UTC
Permalink
Vielen Dank, that's exactly what it was.

Travis
Post by M Mohr
Hello,
Post by Travis Low
Damn, I can't reproduce the error. It might have been a typo. It was
something about missing parenthesis. I assumed it was bad syntax because I
wasn't using any parenthesis.
$x._NODE_.table80[foo80]._NODE_.EmployeeLastNameCont[0]._NODE_
so assigning them first makes the code a lot easier to read. As always,
thanks for your help.
cheers,
Travis
Post by Monte Ohrt
what error did you get? the syntax looks ok.
hmm, i get an error on
{if $string1|replace:" ":"" == $string2|replace:" ":""}
with smarty-2.3.1
<?php if ($this->_run_mod_handler('replace', true, $this->_tpl_vars['string1'], ") ":" " == $string2|replace:" ":" "): ?>
using the same template with the latest cvs checkout,
<?php if ($this->_run_mod_handler('replace', true, $this->_tpl_vars['string1'], " ", "") == $this->_run_mod_handler('replace', true, $this->_tpl_vars['string2'], " ", "")): ?>
HTH
messju
Post by Travis Low
Post by Monte Ohrt
{if $string1|trim eq $string2|trim}
...
{/if}
Post by Travis Low
I'd like to compare two strings in the template, and take one action if
they are the same, and another action if they are not.
One string comes from an XML file. The other string comes from a CSV
file. I cannot (easily) manipulate the strings from within PHP, because
I'd have to do all of them, or none of them.
It turns out that sometimes one or the other string has leading or
trailing spaces. This makes the comparison evaluate to false, which is a
problem.
{if $string1|replace:" ":"" == $string2|replace:" ":""}
{assign var=s1 value=$string1|replace:" ":""}
{assign var=s2 value=$string2|replace:" ":""}
{if $s1 == $s2}
...
{/if}
but I got an error that way too. Any suggestions?
Thanks in advance.
cheers,
Travis
--
Travis Low
<http://dawnstar.org/travis>
--
Travis Low
<http://dawnstar.org/travis>
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
Travis Low
<mailto:***@dawnstar.org>
<http://dawnstar.org/travis>
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Monte Ohrt
2003-01-15 17:06:10 UTC
Permalink
Yep, a lot of cleanup has been done in the compiling code and as a
consequence, a lot of minor things that weren't working now work
properly.

Monte
Post by M Mohr
Hello,
Post by Travis Low
Damn, I can't reproduce the error. It might have been a typo. It was
something about missing parenthesis. I assumed it was bad syntax because I
wasn't using any parenthesis.
$x._NODE_.table80[foo80]._NODE_.EmployeeLastNameCont[0]._NODE_
so assigning them first makes the code a lot easier to read. As always,
thanks for your help.
cheers,
Travis
Post by Monte Ohrt
what error did you get? the syntax looks ok.
hmm, i get an error on
{if $string1|replace:" ":"" == $string2|replace:" ":""}
with smarty-2.3.1
<?php if ($this->_run_mod_handler('replace', true, $this->_tpl_vars['string1'], ") ":" " == $string2|replace:" ":" "): ?>
using the same template with the latest cvs checkout,
<?php if ($this->_run_mod_handler('replace', true, $this->_tpl_vars['string1'], " ", "") == $this->_run_mod_handler('replace', true, $this->_tpl_vars['string2'], " ", "")): ?>
HTH
messju
Post by Travis Low
Post by Monte Ohrt
{if $string1|trim eq $string2|trim}
...
{/if}
Post by Travis Low
I'd like to compare two strings in the template, and take one action if
they are the same, and another action if they are not.
One string comes from an XML file. The other string comes from a CSV
file. I cannot (easily) manipulate the strings from within PHP, because
I'd have to do all of them, or none of them.
It turns out that sometimes one or the other string has leading or
trailing spaces. This makes the comparison evaluate to false, which is a
problem.
{if $string1|replace:" ":"" == $string2|replace:" ":""}
{assign var=s1 value=$string1|replace:" ":""}
{assign var=s2 value=$string2|replace:" ":""}
{if $s1 == $s2}
...
{/if}
but I got an error that way too. Any suggestions?
Thanks in advance.
cheers,
Travis
--
Travis Low
<http://dawnstar.org/travis>
--
Travis Low
<http://dawnstar.org/travis>
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
Monte Ohrt <***@ispi.net>
--
Smarty General Mailing List (http://smarty.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...