Home > Backend Development > PHP Tutorial > Smarty half-hour quick start tutorial_PHP tutorial

Smarty half-hour quick start tutorial_PHP tutorial

WBOY
Release: 2016-07-13 17:37:52
Original
838 people have browsed it

Smarty’s programming part:

In the template design part of smarty, I briefly introduced some common settings of smarty in the template. This section mainly introduces how to start our process in smarty

Program design.

PHP code:-------------------------------------------------- ----------------------------------

First, let’s introduce some elements in the .php file we used in the previous section. Similarly, let’s take the index.php file at the beginning of the previous section to illustrate:

================================================== =
index.php
================================================
/*********************************************
*
* File name: index.php
* Function: Display example program
*
**********************************************/
include_once("./comm/Smarty.class.php"); //Include smarty class files

$smarty = new Smarty(); //Create smarty instance object $smarty
$smarty->templates("./templates"); //Set template directory
$smarty->templates_c("./templates_c"); //Set the compilation directory


//****Attention everyone, I am the new member here****//
$smarty->cache("./cache"); //Set the cache directory
$smarty->cache_lifetime = 60 * 60 * 24; //Set cache time
$smarty->caching = true; //Set caching method

//--------------------------------------------- -------
//The left and right boundary characters, the default is {}, but in actual application it is easy to use JavaScript
//Conflict, so it is recommended to set it to <{}> or other.
//------------------------------------------------ ----
$smarty->left_delimiter = "<{";
$smarty->right_delimiter = "}>";

$smarty->assign("name", "Li Xiaojun"); //Replace template variables

//Compile and display the index.tpl template located under ./templates
$smarty->display("index.tpl");
?>

We can see that the program part of smarty is actually a set of codes that conform to the PHP language specification. Let’s explain them in turn:
1. /**/Statement:
The included part is the program header comments. The main content should be a brief introduction to the function of the program, copyright, author and writing time. This is not necessary in smarty

Necessary, but in terms of program style, this is a good style.

2. include_once statement:
It will include the smarty file installed on the website into the current file. Note that the included path must be written correctly.

3. $smarty = new Smarty():
This sentence creates a new Smarty object $smarty, which is a simple instantiation of an object.

4. $smarty->templates(""):
This sentence specifies the path when the $smarty object uses the tpl template. It is a directory. Without this sentence, Smarty's default template path is templates

in the current directory.

Directory, when actually writing a program, we need to specify this sentence. This is also a good programming style.
5. $smarty->templates_c(""):
This sentence specifies the directory where the $smarty object is compiled. In the template design chapter, we already know that Smarty is a compiled template language, and this directory is what it compiles

Template directory, please note here that if the site is located on a *nix server, please ensure that the directory defined in teamplates_c has writable and readable permissions. By default, its compilation directory

is templates_c in the current directory. For the same reason, we write it out explicitly.

6. $smarty->left_delimiter and $smarty->right_delimiter:
Specifies the left and right separators when looking for template variables. By default, it is "{" and "}", but in practice, because we need to use



An array will be output here:

{foreach from=$newsArray item=newsID}
News number: {$newsID}

News content: {$newsTitle}


{foreachelse}
Sorry, there is no news output in the database!
{/foreach}

==========================================
example6.php
==========================================
/*********************************************
*
* File name: example6.php
* Function: Display example program 2

**********************************************/
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"=>"News No. 1");
$array[] = array("newsID"=>2, "newsTitle"=>"News No. 2");
$array[] = array("newsID"=>3, "newsTitle"=>"News No. 3");
$array[] = array("newsID"=>4, "newsTitle"=>"News No. 4");
$array[] = array("newsID"=>5, "newsTitle"=>"News No. 5");
$array[] = array("newsID"=>6, "newsTitle"=>"News No. 6");

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

//Compile and display the index.tpl template located under ./templates
$smarty->display("example6.tpl"

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486538.htmlTechArticlesmarty’s programming part: In the smarty template design part, I simply put some common settings of smarty in the template After a brief introduction, this section mainly introduces how to use sm...
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