Home Backend Development PHP Tutorial Smarty Quick Start Part 2_PHP Tutorial

Smarty Quick Start Part 2_PHP Tutorial

Jul 13, 2016 pm 05:43 PM
smarty for use getting Started load Can Will fast us mold of

In order to use smarty more conveniently in the future, we can put the three steps of "loading the Smarty template engine", "creating the Smarty object" and "setting the parameters of the Smarty object" into a public php file. We can just reuqire the place where it is used, for example:

1. Create a main.php

<?php
include smarty/Smarty.class.php;
//You only need to modify the ROOT pointing position next time the program is transplanted~
const DIR_SEP = DIRECTORY_SEPARATOR;
define (ROOT, D:.DIR_SEP._PHP_Test.DIR_SEP.Test2.DIR_SEP); $tpl = new Smarty();
$tpl->template_dir = ROOT.templates.DIR_SEP;
$tpl->complie_dir = ROOT.templates_c.DIR_SEP;
$tpl->config_dir = ROOT.configs.DIR_SEP;
$tpl->cache_dir = ROOT.cache.DIR_SEP;
$tpl->left_delimiter = &lt ;{;
$tpl->right_delimiter = }>;
?>

Note 1: Why use DIRECTORY_SEPARATOR here? (Click to view)

Note 2: left_delimiter and right_delimiter are left and right terminator variables, which can be defined as other strings (default is {}).

2. Under templates, create a new test.htm

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> ;<{$title}></title>
</head>
<body>
<{$content}>
</body>
</html>

3. Call the template page to fill in the title and content, and create a new test_smarty_1.php

<?php
require main.php;
$tpl->assign(title, New Message);
$tpl->assign(content, This is test smarty!);
$tpl->display(test.htm);
?>

You can also write like this

<?php
require main.php;
$tpl->assign(array(title=>NewMessage, content=>This is test smarty!));
$tpl ->display(test.htm);
?>

Output result:

The page title displays: NewMessage

The page content shows: This is test smarty!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478801.htmlTechArticleIn order to use smarty more conveniently in the future, we can change "Load Smarty Template Engine", "Create Smarty Object", Put these three steps of "setting parameters of Smarty object" into a common...
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 Article Tags

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)

A Diffusion Model Tutorial Worth Your Time, from Purdue University A Diffusion Model Tutorial Worth Your Time, from Purdue University Apr 07, 2024 am 09:01 AM

A Diffusion Model Tutorial Worth Your Time, from Purdue University

What software is crystaldiskmark? -How to use crystaldiskmark? What software is crystaldiskmark? -How to use crystaldiskmark? Mar 18, 2024 pm 02:58 PM

What software is crystaldiskmark? -How to use crystaldiskmark?

Generate PPT with one click! Kimi: Let the 'PPT migrant workers' become popular first Generate PPT with one click! Kimi: Let the 'PPT migrant workers' become popular first Aug 01, 2024 pm 03:28 PM

Generate PPT with one click! Kimi: Let the 'PPT migrant workers' become popular first

How to download foobar2000? -How to use foobar2000 How to download foobar2000? -How to use foobar2000 Mar 18, 2024 am 10:58 AM

How to download foobar2000? -How to use foobar2000

All CVPR 2024 awards announced! Nearly 10,000 people attended the conference offline, and a Chinese researcher from Google won the best paper award All CVPR 2024 awards announced! Nearly 10,000 people attended the conference offline, and a Chinese researcher from Google won the best paper award Jun 20, 2024 pm 05:43 PM

All CVPR 2024 awards announced! Nearly 10,000 people attended the conference offline, and a Chinese researcher from Google won the best paper award

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

How to use Baidu Netdisk app

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

How to use NetEase Mailbox Master

A must-read for technical beginners: Analysis of the difficulty levels of C language and Python A must-read for technical beginners: Analysis of the difficulty levels of C language and Python Mar 22, 2024 am 10:21 AM

A must-read for technical beginners: Analysis of the difficulty levels of C language and Python

See all articles