>  기사  >  백엔드 개발  >  smarty loop 중첩 사용법에 대해

smarty loop 중첩 사용법에 대해

不言
不言원래의
2018-06-19 10:17:011589검색

이 글은 주로 smarty loop 중첩 사용법을 소개하고, Smarty 템플릿 중첩 루프의 구현 기술과 관련 주의사항을 예시 형태로 분석합니다. 필요한 친구들이 참고할 수 있습니다.

본 글의 예시는 smarty 사용법을 설명합니다. 루프 중첩. 참조를 위해 모든 사람과 공유하세요. 세부 사항은 다음과 같습니다.

test3.php:

<?php
require "main.php";
$forum = array(
    array("category_id" => 1, "category_name" => "公告区",
      "topic" => array(
        array("topic_id" => 1, "topic_name" => "站务公告")
      )
    ),
    array("category_id" => 2, "category_name" => "文学专区",
      "topic" => array(
        array("topic_id" => 2, "topic_name" => "好书介绍"),
        array("topic_id" => 3, "topic_name" => "奇文共赏")
      )
    ),
    array("category_id" => 3, "category_name" => "电脑专区",
      "topic" => array(
        array("topic_id" => 4, "topic_name" => "硬件周边"),
        array("topic_id" => 5, "topic_name" => "软件讨论")
      )
    )
);
$tpl->assign("forum", $forum);
$tpl->display("test3.htm");
?>

샘플 템플릿은 다음과 같이 작성되었습니다.

templates/test3.htm:

<html>
<head>
<title>循环嵌套测试</title>
</head>
<body>
<table width="200" border="0" align="center" cellpadding="3" cellspacing="0">
  <{section name=sec1 loop=$forum}>
  <tr>
    <td colspan="2"><{$forum[sec1].category_name}></td>
  </tr>
  <{section name=sec2 loop=$forum[sec1].topic}>
  <tr>
    <td width="25"> </td>
    <td width="164"><{$forum[sec1].topic[sec2].topic_name}></td>
  </tr>
  <{/section}>
  <{/section}>
</table>
</body>
</html>

test2.php:

<?php
require_once(&#39;./include/db_fns.php&#39;);
include_once("./Smarty/libs/Smarty.class.php"); //包含Smarty类文件
$smarty = new Smarty(); //建立Smarty实例对象$Smarty
$smarty->template_dir = "./templates/dedecms";//设置模板目录
$smarty->compile_dir = "templates/templates_c"; //设置编译目录
$smarty->assign("template_url", "./");
$smarty->assign("$site_url", "//www.jb51.net/");
$smarty->assign("$site_name", "文章管理系统");
$smarty->left_delimiter = "<{"; //设置左边界符
$smarty->right_delimiter = "}>"; //设置右边界符
$db_conn = db_connect();
$query = "SELECT cat_ID,cat_name FROM categories ORDER BY cat_ID DESC";
$result = mysql_query($query);
$i = 5;
while(($row = mysql_fetch_array($result)) && $i > 0)
{
        $query2="SELECT ID, post_title, post_date
                FROM post
                WHERE post.post_category =$row[cat_ID]
                AND post_status <> &#39;unpbulish&#39;
                ORDER BY post_date DESC";
        $result2=mysql_query($query2);
        $i = 5; 
        while(($row2 = mysql_fetch_array($result2)) && $i > 0)
        {
            $row2[post_date]=date(&#39;m-d&#39;,strtotime($row2[post_date]));
            $category = array("cat_ID"=>"$row[cat_ID]","cat_name"=>"$row[cat_name]",
            "post"=>array("ID"=>"$row2[ID]",
            "post_title"=>"$row2[post_title]" ,
            "post_category"=>"$row2[post_category]" ,
            "post_date"=>"$row2[post_date]"));
            $i--;
        }
}
$smarty->assign("forum", $category);
$smarty->display("test2.htm");
?>

test2.htm:

<html>
  <head>
  <title>嵌套循环测试</title>
  </head>
  <body>
  <table width="200" border="0" align="center" cellpadding="3" cellspacing="0">
  <{section name=sec1 loop=$forum}>
      <tr>
      <td colspan="2"><{$forum[sec1].cat_id}></td>
      </tr>
        <{section name=sec2 loop=$forum[sec1].post}>
          <tr>
          <td width="25"> </td>
          <td width="164"><{$forum[sec1].post[sec2].post_title}></td>
          </tr>
        <{/section}>
  <{/section}>
  </table>
</body>
</html>

test4.php:

<?php
require "main.php";
$my_array = array(
array("value" => "0"),
array("value" => "1"),
array("value" => "2"),
array("value" => "3"),
array("value" => "4"),
array("value" => "5"),
array("value" => "6"),
array("value" => "7"),
array("value" => "8"),
array("value" => "9"));
$tpl->assign("my_array", $my_array);
$tpl->display(&#39;test4.htm&#39;);
?>

템플릿은 다음과 같이 작성됩니다. :

<html>
<head>
<title>横向重复表格测试</title>
</head>
<body>
<table width="500" border="1" cellspacing="0" cellpadding="3">
<tr>
<{section name=sec1 loop=$my_array}>
<td><{$my_array[sec1].value}></td>
<{if $smarty.section.sec1.rownum is p by 2}>
</tr>
<tr>
<{/if}>
<{/section}>
</tr>
</table>
</body>
</html>

포인트는 $ smarty.section.sec1.rownum 입니다. 섹션 루프에서 이 변수는 1부터 시작하는 인덱스 값을 가져오므로 rownum을 2로 나눌 수 있으면 <가 출력됩니다. ;/tr>a34de1251f0d9fe1e645927f19a896e8 열을 변경합니다(참고! 앞에 a34de1251f0d9fe1e645927f19a896e8 뒤에 있습니다). 따라서 숫자 2는 열에 표시하려는 레코드 수입니다. 이를 사용하여 다른 프레젠테이션 방법을 변경할 수 있습니다.

연산자에는 다음이 포함됩니다

: eq, ne, neq, gt, lt, lte, le, gte, ge, is even, is 홀수, is not even, is not 홀수, not, mod, p by , even by,odd by

예:

<!--{if $bigsize ge &#39;650&#39;}-->
   <img src="photo/<!--{$photo}-->" border="0" width="650" class="product_photo" />
<!--{else}-->
    <img src="photo/<!--{$photo}-->" border="0" class="product_photo" />
<!--{/if}-->

예전에는 smarty를 자주 사용하지 않았지만, 지난 이틀 동안 친구의 웹사이트를 수정해야 해서 사용해 봤는데 꽤 흥미롭습니다. .

캡처에 대한 참고사항:

캡처 기능의 기능은 템플릿에서 출력된 데이터를 페이지에 출력하는 대신 변수로 수집하는 것입니다.

{capture name="foo"}와 { 사이의 모든 것. /capture }는 함수의 name 속성($foo)에 지정된 변수로 전달됩니다.

수집된 정보는 특수 변수 $smarty에서 사용할 수 있습니다.

예를 들어, Capture.foo는 위의 데이터를 수집합니다. 이 함수에는 이름 속성이 없으며 "default"가 사용됩니다.

각 {capture}는 {/capture}에 해당해야 하며 캡처 함수는 중첩될 수 없습니다.

위 내용이 이 기사의 전체 내용이기를 바랍니다. 모든 사람의 학습에 도움이 될 것입니다. 도움말, 더 많은 관련 내용을 보려면 PHP 중국어 웹사이트에 주목하세요!

관련 권장 사항:

PHP 템플릿 엔진 Smarty 내장 함수 사용에 대한 섹션 및 Else


위 내용은 smarty loop 중첩 사용법에 대해의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.