简体   繁体   English

Smarty中的动态变量名称

[英]Dynamic variable names in Smarty

I want to be access some variables I've assigned dynamically from PHP in Smarty, here is an example: 我想访问我在Smarty中从PHP动态分配的一些变量,这是一个例子:

$content_name = 'body'
$smarty->assign('content_name',$content_name);
$smarty->assign($content_name.'_title',$title);
$smarty->assign($content_name.'_body',$body);

// assigned values
// $content_name = home
// $home_title = $title
// $home_body = $body

The reason I want to access these dynamically is because I call multiple versions of a function that includes the code above, they all use the same template and therefore don't want to simply use $title, $body etc as their valus will conflict with each other. 我想动态访问这些的原因是因为我调用了包含上面代码的函数的多个版本,它们都使用相同的模板,因此不想简单地使用$ title,$ body等因为它们的valus会与之冲突彼此。

Given that I know I want to access the title and body variables based on the content_name I set, how can I achieved this within smarty? 鉴于我知道我想根据我设置的content_name访问标题和正文变量,我怎样才能在smarty中实现这一点?

As per my comment on using an array instead of dynamic variables, here's an example of how to add the vars to an array: 根据我对使用数组而不是动态变量的评论,这里是一个如何将变量添加到数组的示例:

php: PHP:

$vars = array();

function whatever() {
    global $vars;


    $vars[] = array(
        'firstname' => 'Mike',
        'surname' => 'Smith'
    );
}

$smarty->assign('vars', $vars);

smarty: 智者:

{section name=loop loop=$vars}
    Name: {$vars[loop].firstname} {$vars[loop].surname}
{/section}

Even if this post is very old, the given answere is accpeted but not an answere for the question. 即使这篇文章很老,但是给出的回答是有问题的,但不是对这个问题的回答。 Its only an other oppertunity to handle main problem. 它只是处理主要问题的另一个机会。

The question is how to use dynamic variables... 问题是如何使用动态变量......

for the given sample, it should be something like 对于给定的样本,它应该是类似的

PHP PHP

$content_name = 'body';
$title        = 'hello ';
$body         = 'world';
$smarty->assign('content_name',$content_name);
$smarty->assign($content_name.'_title',$title);
$smarty->assign($content_name.'_body',$body);

Smarty Smarty的

{$content_name}          //body
{${$content_name}_title} //hello
{${$content_name}_body}  //world

{${$content_name}_title}{${$content_name}_body} my {$content_name} is awesome
//hello world my body is awesome

This is the dynamic way to use it. 这是使用它的动态方式。 Even if its not the best way in this case :) 即使它不是这种情况下最好的方式:)

If you have kind of objects or multidimensional array... and you like to iterate them, you have to care about the whole string and not just the number... 如果你有一些对象或多维数组...并且你想迭代它们,你必须关心整个字符串而不仅仅是数字......

For example: 例如:

PHP PHP

$arr = [
    "attr1" => "hello ",
    "attr2" => "world ",
    "attr3" => "my body is awesome"
];
$smarty->assign('foobar', $arr);

Smarty Smarty的

{for $i=1 to 3}
    {$foobar.{"attr$i"}}   //works (whole name must be string var mix)
    //{$foobar.attr{"$i"}} //fails
    //{$foobar.attr{$i}}   //fails
{/for}

But using {$foobar{$i}} on a simple array would work. 但是在一个简单的数组上使用{$foobar{$i}}会有效。

For all who need it, enjoy. 对于所有需要它的人,享受吧。

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

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