如何在 Smarty 中访问带有连字符名称的变量?

发布于 2024-08-28 07:57:10 字数 389 浏览 10 评论 0原文

我有一个 PHP 页面,它使用 SimpleXml 解析 XML 文件,然后将该对象传递给 Smarty 模板。我的问题是 XML 文件的标签名称中包含连字符,例如 video-player。在 PHP 中,这没问题,我只使用 $xml->{'video-player'} ,一切都很好。另一方面,当我尝试使用该语法时,Smarty 会很生气。

到目前为止,我想出的唯一解决方案是使用变量来存储名称,例如,

{ assign var=name value="video-player" }
{ $xml->$name }

但这至少可以说不是很优雅。在 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. Smarty, 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

救赎№ 2024-09-04 07:57:11
{php}
    echo $xml->{'video-player'};
{/php}
{php}
    echo $xml->{'video-player'};
{/php}
请帮我爱他 2024-09-04 07:57:11

Dwoo中我会尝试一下,

{$xml->`video-player`}

也许在Smarty中它也能工作。

In Dwoo I'll try with

{$xml->`video-player`}

maybe in Smarty it will work too.

乱世争霸 2024-09-04 07:57:11

您只需将其放在单引号内即可。

它也适用于比较块,例如:

{if $variable == 'hyphenated-value'} it works! {/if}

而不是

{if $variable == hyphenated-value} do not work! {/if}

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}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文