loading...

1

smarty

php读完大概需要5分钟

  • 发布时间:2017-11-18 15:42 星期六
  • 刘伟波
  • 798
  • 更新于2017-11-18 15:46 星期六

1.下载smarty的安装包解压后,把libs文件夹放在网站根目录下,然后创建几个文件夹

templates       存放模板文件

templates_c    存放编译后的文件

configs           存放配置文件

cache             存放缓存文件再创建初始化文件smarty.init.php

<?php
    include "./libs/Smarty.class.php";//包含Smarty类库所在的文件
    $smarty=new Smarty();//创建一个Smarty类的对象$smarty
    $smarty->template_dir="./templates/";//设置所有模板文件存放目录
    $smarty->compile_dir="./templates_c/";//设置所有编译过的模板文件存放目录
    $smarty->config_dir="./configs/";//设置模板中特殊配置文件存放的目录
    $smarty->cache_dir="./cache/";//设置存放Smarty缓存文件的目录
    $smarty->caching=1;//设置开启Smarty缓存模板功能
    $smarty->cache_lifetime=60*60*24;//设置模板缓存有效时间为1天
    $smarty->left_delimiter='<{';//设置模板语言中的左结束符
    $smarty->right_delimiter='}>';//设置模板语言中的右结束符
?>


2、新建模板文件index.tpl及项目中所需的配置文件my.conf,

复制代码

index.tpl

<{config_load file="../configs/my.conf"}><!-- 加载配置文件 -->
<html>
    <head>
        <meta charset="utf-8">
        <title><{$title}></title>
    </head>
    <body bgcolor="<{#bgColor#}>">
        <{$content}>
    </body>
</html>

复制代码

my.conf

title = Welcome to Smarty!
cutoff_size = 40

[setup]
bold = true

3、新建程序入口文件index.php,引入控制器文件,传值并分配变量,并显示模板index.tpl

复制代码

<?php
    //引入smarty.init.php
    include 'smarty.init.php';
    $smarty->assign("title","我的第一个文件标题");
    $smarty->assign("content","我的第一个文件内容");
    $smarty->display("index.tpl");
?>

复制代码

3、运行index.php



4.数组的遍历


$smarty->assign("FirstName", array("John", "Mary", "James", "Henry"));
section name=outer
loop=$FirstName}

<li>{$FirstName[outer]}</li>

{/section}


5. 对象的遍历


$smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"),
array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")));
{section name=sec1 loop=$contacts}
phone: {$contacts[sec1].id}
<br>

fax: {$contacts[sec1].title}
<br>

cell: {$contacts[sec1].createTime}
<br>
{/section}



你可能感兴趣的文章

    发表评论

    评论支持markdown,评论内容不能超过500字符
    关于技术问题或者有啥不懂的都可以来 我的视频空间 提问, 推荐作者总结知识点 前端知识体系, 感謝支持!