简体   繁体   English

smarty code - 访问{php}标签内的变量

[英]smarty code - access variables inside {php} tag

I know we should avoid the {php} tag inside any template engine, still, let's assume that it's the only way on this case. 我知道我们应该避免使用任何模板引擎中的{php}标记,但是,我们假设它是这种情况下的唯一方法。

Considering this, I would like to request some help in order to grab smarty string for php consumption. 考虑到这一点,我想请求一些帮助,以获取智能字符串的PHP消费。

On this case, I would like to access $result.domain inside the srtstr php function. 在这种情况下,我想在srtstr php函数中访问$ result.domain。

What am I doing wrong? 我究竟做错了什么?

{foreach key=period item=regoption from=$result.regoptions}
{if $regoption.$domain}
{if $domain eq "transfer"}
{php}
   $domainName = $result.domain;
   $tld = strstr($domainName, '.');
{/php}

{if $tld eq '.gt'}
   <p>Something</p>
{else}
<option value="{$period}">{$period} {$LANG.orderyears} @ {$regoption.$domain}</option>
{/if}
{/if}
{/if}
{/foreach}

Thanks in advance, 提前致谢,
MEM MEM

You can use get_template_vars , just make sure you use $this instead of $smarty 您可以使用get_template_vars ,只需确保使用$this而不是$smarty

$this->get_template_vars('foo')

It would be a much better idea to rewrite what you are doing as a smarty plugin though 尽管如此 ,重写你正在做的聪明的插件会更好

EDIT: 编辑:

In your example you could do something like 在你的例子中,你可以做类似的事情

$result = $this->get_template_vars('result');
$domainName = $result.domain;

Don't {PHP} tags reference PHP Variables, not variables set through $smarty->assign ? 不要{PHP}标签引用PHP变量,而不是通过$smarty->assign设置的变量?

Perhaps you are trying to access something inside a {PHP} that you set through $smarty->assign ? 也许您正在尝试访问通过$smarty->assign设置的{PHP}内的内容?

Try accessing the object as it is named in your .php file that calls $smarty->dispay() . 尝试访问在.php文件中命名的对象,该文件调用$smarty->dispay()

$this对我们不起作用,我们最终使用$GLOBALS变量得到它,如下所示:

$variable= $GLOBALS['smarty']->getTemplateVars('variable');

In later versions of smarty you have to use $template, instead of $this. 在smarty的更高版本中,您必须使用$ template,而不是$ this。

For example: 例如:

$template->get_template_vars('foo')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM