php 在 smarty 模板中创建一个数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8929140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create an array in smarty template?
提问by sachin sawant
I need to create a new array from other one dimensional array in smarty template. So, what are the best possibilities to create an array in template file.
我需要从 smarty 模板中的其他一维数组创建一个新数组。那么,在模板文件中创建数组的最佳可能性是什么。
Thanks, Sachin
谢谢,萨钦
回答by rodneyrehm
Smarty3 allows you to {$var = ['foo' => 'bar', 'sub' => [1, 2, 3]]}
and {$var.foo = 'other'}
Smarty3 允许您{$var = ['foo' => 'bar', 'sub' => [1, 2, 3]]}
和{$var.foo = 'other'}
if those options don't suffice, write a plugin function.
如果这些选项不够用,请编写一个插件函数。
回答by DerVO
In the past, I have used two approaches - an evil and a dirty one - to quickly assign an array inside a tpl:
过去,我使用了两种方法 - 一种邪恶的方法和一种肮脏的方法 - 在 tpl 中快速分配数组:
{* Am I evil? *}
{php}
$array = array("cat", "dog", "rabbit");
$this->assign("myArray", $array);
{/php}
{* Am I dirty? *}
{assign var='myArray' value=','|explode:"cat,dog,rabbit"}
Both result in your array available inside the template to build a simple loop. Anyway I always ended up changing my code this way, so I did not need this stuff at all.
两者都会导致您的数组在模板内可用以构建一个简单的循环。无论如何,我总是以这种方式最终改变我的代码,所以我根本不需要这些东西。
回答by Eugene Kuzmenko
It's actually very simple:
其实很简单:
{assign 'myArray' ['cat', 'dog', 'rabbit']}
回答by Catalin
From a MVC point of view, the View part of it is only responsible with displaying the data. I would encourage you to rethink the application in such a way that it will allow you to process the data in the Model and pass it for display only in the View.
从 MVC 的角度来看,它的 View 部分只负责显示数据。我鼓励您以这样的方式重新考虑应用程序,它允许您处理模型中的数据并将其传递以仅在视图中显示。
回答by Jeremy
I advise against this but this plugin allows this: http://smarty.incutio.com/?page=set
我建议不要这样做,但这个插件允许这样做:http: //smarty.incutio.com/?page=set