php之Smarty基本语法和三大变量
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.=''.$args["content"].' '; } 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"); //显示的模板
demo.html:
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
在demo.php 中开启session
<!--?php include="" smarty-=""-->display("demo.html"); //显示的模板 <!--?php-->
注意的是:加载配置文件:<{config_load>
读取:<{#内容#}>
加载区域文件要用到第二个参数:<{config_load section="index"><{config_load><{#内容#}><{config_load section="index">
demo.html :中,这里写一些头文件,以及加载代码
<{#two#}>
<{#three#}>
......<{ title="">
三、保留变量
主要有: $_GET
$_POST
$_SESSION
$_SERVER
$_ENV
<{$smarty()}>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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



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 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

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

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:

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

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 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 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
