Home > Backend Development > PHP Tutorial > Quick Start with Smarty Template_PHP Tutorial

Quick Start with Smarty Template_PHP Tutorial

WBOY
Release: 2016-07-21 15:58:06
Original
932 people have browsed it

Various template classes have appeared in the PHP world, but Smarty has always been in the lead in terms of functionality and speed. Because Smarty's functions are relatively powerful, it is a little more complicated to use than some other template classes. . Take 30 minutes to get started quickly.

1. Installation

First open the web page http://smarty.php.net/download.php and download the latest version of Smarty. Unzip the downloaded file (the directory structure is quite complex). Next, I will show you an installation example. After reading it, you should be able to draw inferences.
(1) I created a new directory learn/ in the root directory, and then created a directory smarty/ in learn/. Copy the libs/ of the directory you just decompressed to smarty/, and then create a new templates directory in smarty/. Create new cache/, templates/, templates_c/, and config/ in templates.

(2) New A template file: index.tpl. Place this file in the learn/smarty/templates/templates directory. The code is as follows:

Copy the code The code is as follows:



;/title>


{$hello}
>

Create a new index.php and place this file under learn/:


Copy the code

The code is as follows:
//Reference class files require 'smarty/libs/Smarty.class.php'; $smarty = new Smarty;
//Set each The path to the directory, here is the focus of installation
$smarty->template_dir = "smarty/templates/templates";

$smarty->compile_dir = "smarty/templates/templates_c";

$smarty->config_dir = "smarty/templates/config";
$smarty->cache_dir = "smarty/templates/cache";


//smarty template There is a cache function. If this is true, caching will be turned on, but it will cause the problem that the web page is not updated immediately. Of course, it can also be solved by other methods
$smarty->caching = false;

$hello = "Hello World!";
//Assignment
$smarty->assign("hello",$hello);

//Reference template file
$smarty ->display('index.tpl');

?>




(3) Execute index.php to see Hello World!.

2. Assignment

The value that needs to be replaced in the template file is enclosed in curly brackets {}, and a $ sign is added in front of the value. For example {$hello}. Here can be an array, such as {$ hello.item1}, {$ hello.item2}…
and only a simple function assign (var, value) in the php source file.
Simple example:
*.tpl:
Hello,{$exp.name}! Good {$exp.time}

*.php:
$hello[name ] = “Mr. Green”;

$hello[time]=”morning”;
$smarty->assign(“exp”,$hello);

output:
Hello, Mr. Green! Good morning

3. Reference
Generally, the header and footer of the web pages in the website can be shared, so you only need to quote them in each tpl.
Example: *.tpl:
{include file="header.tpl"}

{* body of template goes here *}

{include file="footer. tpl"}

4. Judgment
You can use if else and other judgment statements in the template file, that is, you can put some logic programs in the template. "eq", "ne", "neq", "gt", "lt", "lte", "le", "gte" "ge", "is even", "is odd", "is not even" , "is not odd", "not", "mod", "div by", "even by", "odd by","==","!=",">", "<", "<=",">="These are comparisons that can be used in if. Just take a look and you'll know what it means.

Example:
{if $name eq "Fred"}

Welcome Sir.

{elseif $name eq "Wilma"}

Welcome ma'am.


{el}
Welcom, whatver you are.

{/if}
>
The method of looping through arrays in Smarty is section. How to assign values ​​and traverse is solved in the template. Only one assign in the PHP source file can solve the problem.
Example:
{* this example will print out all the values ​​of the $custid array *}

{section name=customer loop=$custid}

id: { $custid[customer]}

{/section}

OUTPUT:

id: 1000

id: 1001

id : 1002


6. Frequently Asked Questions

Smarty treats everything in curly brackets {} as its own logic program, so we need to insert javascript functions into the web page. Literal helped. The function of literal is to ignore the curly brackets {}.
       示例:
{literal} 
        

             function isblank(field) { 

                       if (field.value == '')  
                               { return false; } 

                       else 
                               { 

                               document.loginform.submit(); 
                               return true; 

                               } 

             } 

        
{/literal} 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/317630.htmlTechArticle在PHP的世界里已经出现了各式各样的模板类,但就功能和速度来说Smarty还是一直处于领先地位,因为Smarty的功能相对强大,所以使用起来比...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template