• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 3年前 (2022-01-24) 29次浏览 已收录 0个评论

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","[email protected]", 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:

					<style>		body {color:red;}		</style>		<body>		   content:
content:
content:
author:
time:
time:
time:




name}>
say()}>

</script>

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

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

test.conf:

bodycolor=#3e3e3ebodybgcolor=yellowborder=10align=centerwidth=800bgcolor=gray[index]one=11111
    two=22222[content]three=来@源gao*daima.com搞@代#码网搞gaodaima代码33333

    在demo.php 中开启session

    <?php    //session_start();    include "init.inc.php";    $_SESSION["username"]="admin";    $smarty->display("demo.html");  //显示的模板

    注意的是:加载配置文件:
    读取:
    加载区域文件要用到第二个参数:

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

              <style>    body {color:red;}    </style>      <body text="" bgcolor="">    .......   .......  


    ......  

    三、保留变量

    主要有: $_GET
    $_POST
    $_SESSION
    $_SERVER
    $_ENV

     







    搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
    转载请注明原文链接:php之Smarty根本语法和三大变量

    喜欢 (0)
    [搞代码]
    分享 (0)
    发表我的评论
    取消评论

    表情 贴图 加粗 删除线 居中 斜体 签到

    Hi,您需要填写昵称和邮箱!

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址