Smarty Quick Start Part 2_PHP Tutorial
Jul 13, 2016 pm 05:43 PM
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 = < ;{;
$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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

A Diffusion Model Tutorial Worth Your Time, from Purdue University

What software is crystaldiskmark? -How to use crystaldiskmark?

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

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

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