Php Smarty foreach不工作

Php Smarty foreach不工作,php,html,smarty,Php,Html,Smarty,我正在用Smarty做一些实验,但是foreach循环有问题,它不工作,我不明白为什么。这是我的密码: Default.tpl <select name="user"> {html_options values=$id output=$names selected="5"} </select> <table> {foreach $names as $name} {strip} <tr bgcolor="{cycle values=

我正在用Smarty做一些实验,但是foreach循环有问题,它不工作,我不明白为什么。这是我的密码:

Default.tpl

    <select name="user">
    {html_options values=$id output=$names selected="5"}
</select>

<table>
{foreach $names as $name}
{strip}
   <tr bgcolor="{cycle values='#eeeeee,#dddddd'}">
      <td>{$name}</td>
   </tr>
{/strip}
{/foreach}
</table>

<table>
{foreach $users as $user}
{strip}
   <tr bgcolor="{cycle values='#aaaaaa,#bbbbbb'}">
      <td>{$user.name}</td>
      <td>{$user.phone}</td>
   </tr>
{/strip}
{/foreach}
</table>

{html_options values=$id output=$names selected=“5”}
{foreach$names as$name}
{strip}
{$name}
{/strip}
{/foreach}
{foreach$users作为$user}
{strip}
{$user.name}
{$user.phone}
{/strip}
{/foreach}
和default.php

<?php

include('Smarty.class.php');

//create object
$smarty = new Smarty;

$smarty->template_dir = 'C:\xampp\htdocs\smarty\templates';
$smarty->config_dir = 'C:\xampp\htdocs\smarty\config';
$smarty->cache_dir = 'C:\xampp\php\smarty\cache';
$smarty->compile_dir = 'C:\xampp\php\smarty\templates_c';

$smarty->assign('names', array('Bob', 'Jimmy', 'Freddy', 'Walter', 'Jerry'));

$smarty->assign('users', array(
                        array('name' => 'bob', 'phone' => '555-3425'),
                        array('name' => 'jim', 'phone' => '555-4364'),
                        array('name' => 'joe', 'phone' => '555-3422'),
                        array('name' => 'jerry', 'phone' => '555-4973'),
                        array('name' => 'fred', 'phone' => '555-3235')
));

//display information
$smarty->display('default.tpl');
?>

测试时,我得到以下错误:

致命错误:Smarty错误:[在default.tpl第16行]:语法错误:在C:\xampp\php\Smarty\libs\Smarty.class.php的第1094行上,属性名“$names”(Smarty_Compiler.class.php,第1550行)无效

美元用户也是如此。因为我知道这些值正在传递,因为正在工作,所以我无法理解发生了什么

提前通知我

编辑:我从smarty网站上获取了这个示例。

{foreach name=$names}
{foreach name=$names}
.
.
.
.
<td> {$name} </td>
. . . . {$name}

这就是它的工作原理。我从未尝试过您在smarty中使用foreach的方式。

smarty网站上的示例似乎不起作用。我必须这样做才能让它工作:

    <table>
    {foreach from=$names item=name}
    {strip}
       <tr bgcolor="{cycle values='#eeeeee,#dddddd'}">
          <td>{$name}</td>
       </tr>
    {/strip}
    {/foreach}
</table>

<table>
    {foreach from=$users item=user}
    {strip}
       <tr bgcolor="{cycle values='#aaaaaa,#bbbbbb'}">
          <td>{$user.name}</td>
          <td>{$user.phone}</td>
       </tr>
    {/strip}
    {/foreach}
</table>

{foreach from=$names item=name}
{strip}
{$name}
{/strip}
{/foreach}
{foreach from=$users item=user}
{strip}
{$user.name}
{$user.phone}
{/strip}
{/foreach}

它告诉我它缺少“from”属性。我添加了“from”和“item”,现在它正在运行。他们网站上的信息不正确。可能是因为您使用了其他版本的smarty????而不是写教程的那个。smarty 3有很多更改。我正在使用smarty 2.6.27,我不想下载smarty 3,因为我将在工作中处理2.6。您正在使用smarty 3引入的语法,但实际上正在运行Smarty2。升级到Smarty3(3.1.12是最新版本),或者将foreach更改为
{foreach from=$users item=user}