How do I access variables with hyphenated names in Smarty?
I've got a PHP page that parses an XML file with SimpleXml, then passes that object to a Smarty template. My problem is that the XML file has hyphens in its tag names, e.g. video-player
. In PHP, this is no problem, I just use $xml->{'video-player'}
and everything's fine. Smart开发者_开发技巧y, on the other hand, throws a fit when I try to use that syntax.
The only solution I've come up with so far is to use a variable to store the name, e.g.,
{ assign var=name value="video-player" }
{ $xml->$name }
But this isn't terribly graceful to say the least. Is there another, better, approach to referring to a hyphenated variable name in Smarty?
{php}
echo $xml->{'video-player'};
{/php}
In Dwoo
I'll try with
{$xml->`video-player`}
maybe in Smarty
it will work too.
You just need to put it inside single quotes.
It also works on comparission blocks like:
{if $variable == 'hyphenated-value'} it works! {/if}
instead of
{if $variable == hyphenated-value} do not work! {/if}
精彩评论