Php 使用Smarty模板显示阵列?

Php 使用Smarty模板显示阵列?,php,smarty,Php,Smarty,我正在尝试使用Smarty将数据自动插入文本区域 将插入两行。下面是一个插入内容的示例: John Doe Level 1, Support Tech 显然,每个名字都有一个与之相关的位置 我想应该是这样的: $array-names = array("John Joe", "Jane Doe", "Random Name"); $array-positions = array("Level 1, Support Tech", "Level 2, Sales Staff", "Level 2,

我正在尝试使用Smarty将数据自动插入
文本区域

将插入两行。下面是一个插入内容的示例:

John Doe
Level 1, Support Tech
显然,每个名字都有一个与之相关的位置

我想应该是这样的:

$array-names = array("John Joe", "Jane Doe", "Random Name");
$array-positions = array("Level 1, Support Tech", "Level 2, Sales Staff", "Level 2, Billing Team");
然后我只需要随机选择一个自动插入,但它们需要匹配。例如,
John Doe
应始终具有下面列出的
1级技术支持


在Smarty中这样做是可能的还是我完全走错了方向…?

您可以在php中生成
rand(0,count($array names)-1)
,将其传递给Smarty,并将其用作Smarty中数组的键。

您不能按照描述的顺序在Smarty中创建数组吗

然后在smarty中使用以下方法显示阵列,例如:

<?php
//an organized array with names and positions that match
$array_names_positions = array(1 => array('Name' => 'John Doe', 'position' => 'Level 1, Support Tech'),
                              2 => array('Name' => 'Jane Doe', 'position' => 'Level 2, Sales Staff')
                              3 => array('Name' => 'Random Name', 'position' => 'Level 2, Billing Team')
                    );

//assign the array to $items var
$smarty->assign('items', $array_names_positions);

//draw a textarea and display the array, (you can work on your display mechanism to limit or to display all here
<textarea>
{foreach from=$items key=myId item=i}
  {$i.name}: {$i.position}
{/foreach}
</textarea>

在smarty模板中。不要在模板文件中混用太多php代码;它违背了模板保持平衡的目的。如果php代码有大量的逻辑,那么应该将其放在smarty控制器中

实际上,我想这样做,问题是与smarty模板关联的PHP文件是加密的,因此我无法对其进行任何更改:(然后在smarty中生成随机数。
{math equation='rand(0,count(数组名称))'assign='selection'}
这看起来几乎和我想要的一模一样,但它似乎要求将其输入到.php文件中,而我只能访问Smarty的(.tpl)模板文件。是否可以将其修改为在.tpl中工作,因为这正是我想要的。。。
{php}
//php code here
{/php}