I'm working on a blog project in Movable Type and I'm absolutely stuck trying to get multiple conditions in an if statement to work.
The end goal is something the equivalent of this:
<mt:SetVar name="foo" value="bar">
<mt:SetVar name="foo1" value="bar1">
<mt:If name="foo" eq="bar">
<mt:If name="foo1" eq="bar1">
<div>Foobar Hooray</div>
</mt:If>
</mt:If>
I have tried as many of the usual combinations as I could think of, but is there one that I am missing? Or is this just not possible in MT?
Even the documentation doesn't reference multiple conditions. https://movabletype.org/documentation/appendices/tags/if.html
These are the attempts made (not in order of attempt) and all will show as true, even though the first condition is false:
<mt:If name="foo" eq="barf" && name="foo1" eq="bar1">
<mt:If (name="foo" eq="barf") && (name="foo1" eq="bar1")>
<mt:If name="foo" eq="barf" & name="foo1" eq="bar1">
<mt:If (name="foo" eq="barf") & (name="foo1" eq="bar1")>
<mt:If name="foo" eq="barf" AND name="foo1" eq="bar1">
<mt:If (name="foo" eq="barf") AND (name="foo1" eq="bar1")>
<mt:If name="foo" eq="barf" And name="foo1" eq="bar1">
<mt:If (name="foo" eq="barf") And (name="foo1" eq="bar1")>
<mt:If name="foo" eq="barf" + name="foo1" eq="bar1">
<mt:If (name="foo" eq="barf") + (name="foo1" eq="bar1")>
Your first example with nested conditionals is most commonly used in my experience, but there are a couple ways you can achieve multiple tests in a single condition block.
First, we set the variables as you did:
The simplest test might be in Perl using the
testattribute:Extra caution would be prudent when using Perl in templates, and doing so will also require the server to have installed the Perl module Safe.
That said, you could perhaps make that slightly easier to maintain if you have a large number of variables to test by relying on interpolation:
Or you can do something similar with normal template tags by first combining the variables (or not setting
fooandfoo1in the first place, depending where their values come from):The output of those tests should be:
You should even be able to construct a
SetVarBlockwith each component variable on separate lines for readability with judicious use of modifiers likestrip_linefeedsandtrim.It has been quite some time since I have written much Movable Type code, so there are probably other ways. MT (and Perl) is quite flexible, and usually There Is More Than One Way To Do It. Hope that gets you closer!