Home > Backend Development > PHP Problem > What are the data variables of smarty template and how to call them?

What are the data variables of smarty template and how to call them?

autoload
Release: 2023-03-08 14:10:02
Original
1396 people have browsed it

Definition: Template variables, which are variables assigned in the template, and how to use Smarty rules to parse variables in the template.

In the Smarty template, we divide the variables in the template into three categories.

  • PHP assigns variables, that is, variables assigned using the assign method.

  • Smarty retains variables, including super-global predefined variables and smarty’s built-in variables.

  • Custom variables, users define variables in the template.

1. PHP allocates variables. In theory, PHP can allocate any data type to the template for parsing. Usually, there are only three types of data:

  • Scalar data: directly use the data output by the mark.

  • Array data: You can use subscripts in the smarty template or pass "." Subscript.

  • Object data: In the smarty template, access is achieved through object accessor.

<?php
    require &#39;smarty/Smarty.class.php&#39;;
    $smarty=new Smarty();
    // $smarty->left_delimiter="<{";
    // $smarty->right_delimiter="}>";
    $smarty->template_dir = &#39;templates/&#39;;   //实际模板所在目录,如果没有会在根目录下查找
    
    
    //普通数据
    $smarty->assign(&#39;hello&#39;,"hello world");
    //数组
    $smarty->assign(&#39;arr1&#39;,array(1412,14,23,456));
    $smarty->assign(&#39;arr2&#39;,array(&#39;name&#39;=>&#39;张三&#39;,&#39;sex&#39;=>&#39;男&#39;));
    //对象
    class Person{
        public $name=&#39;陈平安&#39;;
        public $perr=&#39;saber&#39;;
    }
    $smarty->assign(&#39;object1&#39;,new Person());
    $smarty->display(&#39;model.html&#39;);
?>
Copy after login
<!DOCTYPE html>//模板 model.html
<html>
<head>
    <title></title>
</head>
<body>
     {$hello}这是templates下面的模板 <br>
     这是索引数组:{$arr1[0]}---{$arr1[1]}------{$arr1[2]}<br>
     这是索引数组:{$arr1.0}---{$arr1.1}------{$arr1.2}<br>
     这是关联数组:{$arr2.name}-----{$arr2.sex}<br>
     这是对象:{$object1->name}-----------{$object1->perr}<br>
</body>
</html>
Copy after login

2.Smarty reserved variables: Smarty considers system variables or internal variables that users will need to use frequently. Such variables usually start with $smarty, followed by various keywords, which can be accessed multiple times.

  • GET data: {$smarty.get.name}

  • ##POST data: {$smarty.post.name}

  • session data: {$smarty.session.name}

  • cookie data: {$smarty.cookies.name}

  • REQUEST data: {$smarty.request.name}

  • server data: {$smarty.server.uppercase name}

  • Time stamp: {$smarty.now}

  • Template path: {$smarty.current_dir}

  • Template name: { $smarty.template}

  • Configuration file: {$smarty.config.Configuration name}

  • <html>
       	<header></header>
        <body>
            	GET数据:{$smarty.get.name}
    	  	POST数据:{$smarty.post.name}
    		session数据:{$smarty.session.username}
    		cookie数据:{$smarty.cookies.username}
    		REQUEST数据:{$smarty.request.name}
    		server数据:{$smarty.server.SERVER_NAME}
    		时间戳:{$smarty.now}
    		模板路径:{$smarty.current_dir}
    		模板名字:{$smarty.template}
        </body>
    </html>
    Copy after login

3. Custom variables : In order to flexibly process data in the template, Smarty allows setting variables: {assign var='variable name' value='variable value'}.

<html>
    <header></header>
    <body>
        {assign var=&#39;name&#39; value=&#39;Sun&#39;}
        {$name}
    </body>
</html>
Copy after login
Recommended:

php tutorial,php video tutorial

The above is the detailed content of What are the data variables of smarty template and how to call them?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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