2012-09-27 8 views
0

Smarty로 실험을하고 있는데 foreach 루프에 문제가 있습니다. 작동하지 않고 이유를 이해할 수 없습니다.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> 

하고

<?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'); 
?> 

을 default.php : 여기 내 코드는

치명적인 오류 : 멋지 오류 : [기본에 .tpl line 16] : 구문 오류 : 1094 줄의 C : \ xampp \ php \ Smarty \ libs \ Smarty.class.php에있는 '$ names'(Smarty_Compiler.class.php, 1550 줄)이 잘못되었습니다.

$ 사용자에게도 마찬가지입니다. 값이 지나가고 있다는 것을 알고 있기 때문에, 작동하고 있기 때문에, 나는 무슨 일이 일어나고 있는지 이해할 수 없다.

Thnx.

편집 : 나는 멋진 웹 사이트에서이 예를 들었습니다.

+1

Smarty3에서 도입 한 구문을 사용하고 있지만 실제로는 Smarty2가 실행 중입니다. Smarty3로 업그레이드 (현재 3.1.12) 또는 foreach를'{foreach from = $ users item = user} '로 변경하십시오. – rodneyrehm

답변

1

멋진 웹 사이트의 예제가 작동하지 않는 것처럼 보입니다. 이것이 제대로 작동하기 위해서해야 할 일입니다.

<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> 
+0

은 다른 버전의 smarty를 사용했기 때문에있을 수 있습니다 ???? 튜토리얼이 쓰여진 것보다 똑똑한 3에는 많은 변화가있었습니다. – geekman

+0

Smarty 2.6.27을 사용하고 있습니다. 직장에서 2.6을 다루기 때문에 smarty 3을 다운로드하고 싶지 않았습니다. –

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

이것이 작동하는 방식입니다. 나는 foreach를 현명하게 사용하는 방식을 한번도 시도한 적이 없습니다.

+0

나는이 멋진 예를 http://www.smarty.net/crash_course에서 가져 왔습니다. –

+0

이런 식으로 사용해보세요 ??? – geekman

+0

"보낸 사람"속성이 누락되었다고했습니다. 나는 'from'과 'item'을 추가했고 이제는 실행 중입니다. 해당 웹 사이트의 정보가 올바르지 않습니다. –

관련 문제