Table of Contents
php之Smarty基本语法和三大变量
Home php教程 php手册 php之Smarty基本语法和三大变量

php之Smarty基本语法和三大变量

Jun 13, 2016 am 09:09 AM
variable grammar

php之Smarty基本语法和三大变量

在Smarty模板中主要都是以变量为主,下面就是有关Smarty的三大变量以及一些普通的用法

 

首先在根目录下建立要用到的文件基本和上一次一样,configs是配置文件夹

 

 

一、从PHP中分配的变量

$smarty->assign()

$smarty->display()

 

首先先编写初始化的php代码,和上一篇的 conn.inc.php 一样

 

<!--?php
    include "init.inc.php";
    //assign方法
    $smarty--->assign("title","11111111");
    $smarty->assign("content","2222222222");
    //分配一个关联数组,用的较少
    $smarty->assign(array("author"=>"何栘烽","time"=>date("Y-m-d H:i:s")));

    //注册函数
    $smarty->registerPlugin("function","myfun","test");  //注册插件,将test函数注册成myfun
    function test($args){
    	//args=array("size"=>7, "color"="yellow", "num"=>5, "connect"=>"3333333")
    	//循环遍历出
    	$str="";
    	for($i=0; $i<$args["num"]; $i++){
    		$str.=&#39;&#39;.$args["content"].&#39;
&#39;;
    	}
    	return $str;
    }
    //数组
    $smarty->assign("contacts",array("0575-1241243","kefeng.hyf@gmail.com", array("243214124", "45345")));
    //对象
    class Person{
    	public $name="kefeng";
    	function say(){
    		return $this->name."hhhhhhhh";
    	}
    }
    $smarty->assign("p",new Person());
    $smarty->display("demo.html");  //显示的模板
Copy after login

demo.html:

Copy after login
Copy after login
content:<{$content}>
content:<{$content}>
content:<{$content}>
author:<{$author}>
time:<{$time}>
time:<{myfun()}>
time:<{date("y-m-d")}>

<{myfun size="7" color="yellow" num="5" content="3333333"> <{$contacts[0]}>
<{$contacts[2][0]}>

<{$p->name}>
<{$p->say()}>



<{$content}><{$content}><{$content}><{$author}><{$time}><{myfun()}><{date("y-m-d")}><{myfun size="7" color="yellow" num="5" content="3333333"><{$contacts[0]}><{$contacts[2][0]}><{$p-><{$p->

二、从配置文件中读取变量

这里需要创一个 configs 文件夹,里面配置文件 test.conf

test.conf:

bodycolor=#3e3e3e
bodybgcolor=yellow
border=10
align=center
width=800
bgcolor=gray

[index]
one=11111

[list]
two=22222

[content]
three=33333
Copy after login

在demo.php 中开启session

<!--?php include="" smarty-=""-->display("demo.html");  //显示的模板
<!--?php-->
Copy after login

注意的是:加载配置文件:<{config_load>
读取:<{#内容#}>
加载区域文件要用到第二个参数:<{config_load section="index">
<{config_load><{#内容#}><{config_load section="index">

demo.html :中,这里写一些头文件,以及加载代码

Copy after login
Copy after login
<{config_load "test.conf" section="index"}>....... ....... <{#one#}>
<{#two#}>
<{#three#}>
......<{ title="">

三、保留变量

主要有: $_GET
$_POST
$_SESSION
$_SERVER
$_ENV
<{$smarty()}>

 
Copy after login







 

 

 

 

 

 

 

 

 

 

 

 

 

 

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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

A guide to using Windows 11 and 10 environment variables for profiling A guide to using Windows 11 and 10 environment variables for profiling Nov 01, 2023 pm 08:13 PM

Environment variables are the path to the location (or environment) where applications and programs run. They can be created, edited, managed or deleted by the user and come in handy when managing the behavior of certain processes. Here's how to create a configuration file to manage multiple variables simultaneously without having to edit them individually on Windows. How to use profiles in environment variables Windows 11 and 10 On Windows, there are two sets of environment variables – user variables (apply to the current user) and system variables (apply globally). However, using a tool like PowerToys, you can create a separate configuration file to add new and existing variables and manage them all at once. Here’s how: Step 1: Install PowerToysPowerTo

Strict mode for variables in PHP7: how to reduce potential bugs? Strict mode for variables in PHP7: how to reduce potential bugs? Oct 19, 2023 am 10:01 AM

Strict mode was introduced in PHP7, which can help developers reduce potential errors. This article will explain what strict mode is and how to use strict mode in PHP7 to reduce errors. At the same time, the application of strict mode will be demonstrated through code examples. 1. What is strict mode? Strict mode is a feature in PHP7 that can help developers write more standardized code and reduce some common errors. In strict mode, there will be strict restrictions and detection on variable declaration, type checking, function calling, etc. Pass

What are instance variables in Java What are instance variables in Java Feb 19, 2024 pm 07:55 PM

Instance variables in Java refer to variables defined in the class, not in the method or constructor. Instance variables are also called member variables. Each instance of a class has its own copy of the instance variable. Instance variables are initialized during object creation, and their state is saved and maintained throughout the object's lifetime. Instance variable definitions are usually placed at the top of the class and can be declared with any access modifier, which can be public, private, protected, or the default access modifier. It depends on what we want this to be

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

Mind map of Python syntax: in-depth understanding of code structure Mind map of Python syntax: in-depth understanding of code structure Feb 21, 2024 am 09:00 AM

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

Deep understanding of const in C language Deep understanding of const in C language Feb 18, 2024 pm 12:56 PM

Detailed explanation and code examples of const in C In C language, the const keyword is used to define constants, which means that the value of the variable cannot be modified during program execution. The const keyword can be used to modify variables, function parameters, and function return values. This article will provide a detailed analysis of the use of the const keyword in C language and provide specific code examples. const modified variable When const is used to modify a variable, it means that the variable is a read-only variable and cannot be modified once it is assigned a value. For example: constint

jQuery usage practice: several ways to determine whether a variable is empty jQuery usage practice: several ways to determine whether a variable is empty Feb 27, 2024 pm 04:12 PM

jQuery is a JavaScript library widely used in web development. It provides many simple and convenient methods to operate web page elements and handle events. In actual development, we often encounter situations where we need to determine whether a variable is empty. This article will introduce several common methods of using jQuery to determine whether a variable is empty, and attach specific code examples. Method 1: Use the if statement to determine varstr="";if(str){co

Detailed explanation of variable scope in Golang functions Detailed explanation of variable scope in Golang functions Jan 18, 2024 am 08:51 AM

Detailed explanation of variable scope in Golang functions In Golang, the scope of a variable refers to the accessible range of the variable. Understanding variable scope is important for code readability and maintainability. In this article, we will take a deep dive into variable scope in Golang functions and provide concrete code examples. In Golang, the scope of variables can be divided into global scope and local scope. The global scope refers to variables declared outside all functions, that is, variables defined outside the function. These variables can be

See all articles