国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Backend Development PHP Tutorial php smarty section loop

php smarty section loop

Jun 23, 2016 pm 02:36 PM

==========================================example6.php
==========================================
/*********************************************
*
* 文件名: example6.php
* 作 用: 顯示實例程序2
*
* 作 者: 大師兄
* Email: teacherli@163.com
*
*********************************************/
include_once("./comm/Smarty.class.php");?

$smarty = new Smarty();?
$smarty->templates("./templates");?
$smarty->templates_c("./templates_c");?
$smarty->cache("./cache");
$smarty->cache_lifetime = 0;
$smarty->caching = true;
$smarty->left_delimiter = "{";?
$smarty->right_delimiter = "}";

$array[] = array("newsID"=>1, "newsTitle"=>"第1條新聞");?
$array[] = array("newsID"=>2, "newsTitle"=>"第2條新聞");?
$array[] = array("newsID"=>3, "newsTitle"=>"第3條新聞");?
$array[] = array("newsID"=>4, "newsTitle"=>"第4條新聞");?
$array[] = array("newsID"=>5, "newsTitle"=>"第5條新聞");?
$array[] = array("newsID"=>6, "newsTitle"=>"第6條新聞");?

$smarty->assign("newsArray", $array);

//編譯并顯示位于./templates下的index.tpl模板
$smarty->display("example6.tpl");?
?>

=================================================
example6.php 輸出文件
=================================================

foreach使用的例子

這里將輸出一個數(shù)組:


新聞編號:1

新聞內(nèi)容:第1條新聞



新聞編號:2

新聞內(nèi)容:第2條新聞



新聞編號:3

新聞內(nèi)容:第3條新聞



新聞編號:4

新聞內(nèi)容:第4條新聞



新聞編號:5

新聞內(nèi)容:第5條新聞



新聞編號:6

新聞內(nèi)容:第6條新聞





foreach還可以用foreachelse來匹配,用foreachelse來表示當(dāng)傳遞給foreach的數(shù)組為空值時程序要執(zhí)行的操作,具體的使用方法,請參考手冊的說明。

2. section:?
section的產(chǎn)生是為解決foreach的不足的,與foreach一樣,它用于設(shè)計模板內(nèi)的循環(huán)塊,它較為復(fù)雜,可極大程序上滿足程序需要,所以在程序中我習(xí)慣使用它而不使用foreach,基本原形為:

{section name = name loop = $varName[, start = $start, step = $step, max = $max, show = true]}

name: section的名稱,不用加$
$loop: 要循環(huán)的變量,在程序中要使用assign對這個變量進(jìn)行操作。
$start: 開始循環(huán)的下標(biāo),循環(huán)下標(biāo)默認(rèn)由0開始
$step: 每次循環(huán)時下標(biāo)的增數(shù)
$max: 最大循環(huán)下標(biāo)
$show: boolean類型,決定是否對這個塊進(jìn)行顯示,默認(rèn)為true

這里有個名詞需要說明:
循環(huán)下標(biāo):實際它的英文名稱為index,是索引的意思,這里我將它譯成"下標(biāo)",主要是為了好理解。它表示在顯示這個循環(huán)塊時當(dāng)前的循環(huán)索引,默認(rèn)從0開始,受$start的影響,如果將$start設(shè)為5,它也將從5開始計數(shù),在模板設(shè)計部分我們使用過它,這是當(dāng)前{section}的一個屬性,調(diào)用方式為Smarty.section.sectionName.index,這里的sectionName指的是函數(shù)原型中的name屬性。
{section}塊具有的屬性值,分別為:
1. index: 上邊我們介紹的"循環(huán)下標(biāo)",默認(rèn)為0
2. index_prev: 當(dāng)前下標(biāo)的前一個值,默認(rèn)為-1
3. index_next: 當(dāng)前下標(biāo)的下一個值,默認(rèn)為1
4. first: 是否為第一下循環(huán)
5. last: 是否為最后一個循環(huán)
6. iteration: 循環(huán)次數(shù)
7. rownum: 當(dāng)前的行號,iteration的另一個別名
8. loop: 最后一個循環(huán)號,可用在section塊后統(tǒng)計section的循環(huán)次數(shù)
9. total: 循環(huán)次數(shù),可用在section塊后統(tǒng)計循環(huán)次數(shù)
10. show: 在函數(shù)的聲明中有它,用于判斷section是否顯示

它們的具體屬性大家可以參考手冊,在程序中可靈活使用它的這些屬性,模板部分我就使用過index屬性,大家可以回過頭去看看。

同樣,{section}也可以配合使用{sectionelse},用來表示傳入的數(shù)組變量為空時對模板進(jìn)行的處理。

我們把上邊的那個例子使用{section}來替代{foreach}來實現(xiàn)現(xiàn)樣的功能,注意,在這個例子中我只將tpl模板中的{foreach}用

{section}來實現(xiàn),php程序文件中沒有任何改動,同時加了{(lán)sectionelse}處理塊:

===========================================
example7.tpl
===========================================

這是一個foreach使用的例子

這里將輸出一個數(shù)組:


新聞編號:

新聞標(biāo)題:



對不起,沒有任何新聞輸入!




==========================================
example7.php
==========================================
/*********************************************
*
* 文件名: example7.php
* 作 用: 顯示實例程序2
*
* 作 者: 大師兄
* Email: teacherli@163.com
*
*********************************************/
include_once("./comm/Smarty.class.php");?

$smarty = new Smarty(); //建立smarty實例對象$smarty
$smarty->template_dir = "./templates";//設(shè)置模板目錄
$smarty->compile_dir = "./templates_c"; //設(shè)置編譯目錄

$smarty->cache_dir = "./cache"; //設(shè)置緩存目錄
$smarty->cache_lifetime = 0;
$smarty->caching = true;
$smarty->left_delimiter = " $smarty->right_delimiter = "}>";

$array[] = array("newsID"=>1, "newsTitle"=>"第1條新聞");?
$array[] = array("newsID"=>2, "newsTitle"=>"第2條新聞");?
$array[] = array("newsID"=>3, "newsTitle"=>"第3條新聞");?
$array[] = array("newsID"=>4, "newsTitle"=>"第4條新聞");?
$array[] = array("newsID"=>5, "newsTitle"=>"第5條新聞");?
$array[] = array("newsID"=>6, "newsTitle"=>"第6條新聞");?

$smarty->assign("News", $array);

//編譯并顯示位于./templates下的index.tpl模板
$smarty->display("example7.tpl");?
?>

=================================================
example7.php 輸出文件
=================================================

foreach使用的例子

這里將輸出一個數(shù)組:


新聞編號:1

新聞內(nèi)容:第1條新聞



新聞編號:2

新聞內(nèi)容:第2條新聞



新聞編號:3

新聞內(nèi)容:第3條新聞



新聞編號:4

新聞內(nèi)容:第4條新聞



新聞編號:5

新聞內(nèi)容:第5條新聞



新聞編號:6

新聞內(nèi)容:第6條新聞





這里的{section}塊的對于變量的命名方式感覺有些別扭,不過沒關(guān)系,你只要記住模板變量使用:
$loopName[name].var這種模式就行了,loopName為loop處賦予的變量名,[name]為name處賦予的字符串,.后為為你要在程序數(shù)組中設(shè)定要與值相對應(yīng)的下標(biāo)名稱就行了。


好了,smarty學(xué)習(xí)指南---程序設(shè)計篇就寫到這里,對于一般的應(yīng)用,這些知識已經(jīng)夠用了,其它的一些高級技巧大家請參看手冊中的例子,下一節(jié)將講講Smarty在實際應(yīng)用中的例子,將分別以php內(nèi)置的mysql語句,phplib中的DB類,來分別講一下各個類庫在同一個例子中的實現(xiàn)。

http://blog.163.com/alex_kame/blog/static/1454674820094611141679/


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do I implement authentication and authorization in PHP? How do I implement authentication and authorization in PHP? Jun 20, 2025 am 01:03 AM

TosecurelyhandleauthenticationandauthorizationinPHP,followthesesteps:1.Alwayshashpasswordswithpassword_hash()andverifyusingpassword_verify(),usepreparedstatementstopreventSQLinjection,andstoreuserdatain$_SESSIONafterlogin.2.Implementrole-basedaccessc

How can you handle file uploads securely in PHP? How can you handle file uploads securely in PHP? Jun 19, 2025 am 01:05 AM

To safely handle file uploads in PHP, the core is to verify file types, rename files, and restrict permissions. 1. Use finfo_file() to check the real MIME type, and only specific types such as image/jpeg are allowed; 2. Use uniqid() to generate random file names and store them in non-Web root directory; 3. Limit file size through php.ini and HTML forms, and set directory permissions to 0755; 4. Use ClamAV to scan malware to enhance security. These steps effectively prevent security vulnerabilities and ensure that the file upload process is safe and reliable.

What are the differences between == (loose comparison) and === (strict comparison) in PHP? What are the differences between == (loose comparison) and === (strict comparison) in PHP? Jun 19, 2025 am 01:07 AM

In PHP, the main difference between == and == is the strictness of type checking. ==Type conversion will be performed before comparison, for example, 5=="5" returns true, and ===Request that the value and type are the same before true will be returned, for example, 5==="5" returns false. In usage scenarios, === is more secure and should be used first, and == is only used when type conversion is required.

How do I perform arithmetic operations in PHP ( , -, *, /, %)? How do I perform arithmetic operations in PHP ( , -, *, /, %)? Jun 19, 2025 pm 05:13 PM

The methods of using basic mathematical operations in PHP are as follows: 1. Addition signs support integers and floating-point numbers, and can also be used for variables. String numbers will be automatically converted but not recommended to dependencies; 2. Subtraction signs use - signs, variables are the same, and type conversion is also applicable; 3. Multiplication signs use * signs, which are suitable for numbers and similar strings; 4. Division uses / signs, which need to avoid dividing by zero, and note that the result may be floating-point numbers; 5. Taking the modulus signs can be used to judge odd and even numbers, and when processing negative numbers, the remainder signs are consistent with the dividend. The key to using these operators correctly is to ensure that the data types are clear and the boundary situation is handled well.

How can you interact with NoSQL databases (e.g., MongoDB, Redis) from PHP? How can you interact with NoSQL databases (e.g., MongoDB, Redis) from PHP? Jun 19, 2025 am 01:07 AM

Yes, PHP can interact with NoSQL databases like MongoDB and Redis through specific extensions or libraries. First, use the MongoDBPHP driver (installed through PECL or Composer) to create client instances and operate databases and collections, supporting insertion, query, aggregation and other operations; second, use the Predis library or phpredis extension to connect to Redis, perform key-value settings and acquisitions, and recommend phpredis for high-performance scenarios, while Predis is convenient for rapid deployment; both are suitable for production environments and are well-documented.

How do I stay up-to-date with the latest PHP developments and best practices? How do I stay up-to-date with the latest PHP developments and best practices? Jun 23, 2025 am 12:56 AM

TostaycurrentwithPHPdevelopmentsandbestpractices,followkeynewssourceslikePHP.netandPHPWeekly,engagewithcommunitiesonforumsandconferences,keeptoolingupdatedandgraduallyadoptnewfeatures,andreadorcontributetoopensourceprojects.First,followreliablesource

What is PHP, and why is it used for web development? What is PHP, and why is it used for web development? Jun 23, 2025 am 12:55 AM

PHPbecamepopularforwebdevelopmentduetoitseaseoflearning,seamlessintegrationwithHTML,widespreadhostingsupport,andalargeecosystemincludingframeworkslikeLaravelandCMSplatformslikeWordPress.Itexcelsinhandlingformsubmissions,managingusersessions,interacti

How to set PHP time zone? How to set PHP time zone? Jun 25, 2025 am 01:00 AM

TosettherighttimezoneinPHP,usedate_default_timezone_set()functionatthestartofyourscriptwithavalididentifiersuchas'America/New_York'.1.Usedate_default_timezone_set()beforeanydate/timefunctions.2.Alternatively,configurethephp.inifilebysettingdate.timez

See all articles