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

php学习??smarty

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

smary文件配置步骤

//步骤1
require ‘/libs/Smarty.class.php’;
//步骤2
$smarty = new Smarty;
//步骤3
/*
$smarty->template_dir = “”;
$smarty->compile_dir = “”;
$smarty->cache_dir = “”;
$smarty->caching = 1;
$smarty->cache_lifetime = 60*60;//秒,这里的值是一个小时
$smarty->left_delimiter ='{#’;
$smarty->right_delimiter = ‘#}’;
$smarty->compile_check = true;
$smarty->debugging = true;
*/
//步骤4
$smarty->assign(“demo1”, “fengsulei”);
$smarty->assign(“demo2”, “cool is working”);
$smarty->assign(array(“demo3″=>”string1″,”demo4″=>”smarty is worked”));
//assign 赋值,有两种形式
//$smarty->assign(“demo1”, “fengsulei”);
//$smarty->assign(“demo1”, $str);
//$smarty->assign(array(“city”=>”string1″,”state”=>”string2”));
$smarty->assign(“p1″,”kloyu”);
$farray=array(“a”,”b”,”c”,”d”,”e”,”ss”=>99);
$smarty->assign(“farray”,$farray);
//步骤5
$smarty->display(‘index.tpl’);
//显示有三种方式
//使用文件$smarty->display(‘index.tpl’)
//使用路径$smarty->display(‘admin/index.tpl’)
//使用绝对路径$smarty->display(‘www/templates/index.tpl’),$smarty->display(‘file:D:/wamp/www/templates/index.tpl’)

模板代码示例

{#$demo1#}

{#$demo2#}
{#$demo3#}
{#$demo4#}
{#$p1|upper|truncate:4#}
get
{#$smarty.get.att#}
post
{#$smarty.post.att#}
cookies
{#$smarty.cookies.att#}
session
{#$smarty.session.att#}
request
{#$smarty.request.att#}
server_name
{#$smarty.server.SERVER_NAME#}
env.path
{#$smarty.env.PATH#}

foreach exp1

{#foreach from=$farray item=row#}
{#$row#}

{#foreachelse#}
$farray is empty
{#/foreach#}

foreach exp2

{#foreach key=kk item=vv from=$farray #}
{#$kk#}-{#$vv#}

{#foreachelse#}
$farray is empty
{#/foreach#}

{#section name=line loop=$farray #}
{#$farray[line]#}

{#if $smarty.section.line.iteration==5#}
{#$farray.ss#}

{#/if#}
{#sectionelse#}
$farray is empty
{#/section#}

配置文件实例

#global variable

page_titile=”xxxxxx”
body_color=”#333″
table_bgcolor=”#666″

[index]
index_page_titile=””
index_body_color=””
index_table_bgcolor=””


    list_page_titile=””
    list_body_color=””
    list_table_bgcolor=””

    [view]
    view_page_titile=””
    view_body_color=””
    view_table_bgcolor=””

    加载配置文件

    {config_load file=”head.conf” section|scope|global=””}

    config_load 参数

    类型 默认值
    section 加载配置文件特定的一节 string
    scope 加载数据的作用域,取值是local,parent,global。local指定变量作用域是当前模板,parent指定变量作用域是当前模板和当前模板的父模板 string local
    global 加载变量是否全局可见 bool false

    代码示例

    {#config_load file=”head.conf”#}

    引用配置文件代码示例

    1,{#$smarty.config.page_titile#}

    2,{##page_titile##}

    模板包含

    {include file=”模板文件名” assign=(本文来)源gaodaimacom搞#^代%!码&网(

    搞gaodaima代码

    “指定传递给的变量名(变量名不加$符号)”}

    常用变量

    capitalize 所有字符首字符大写
    count_characters 计算字符个数
    cat 将cat里的参数值连接到给定变量后
    count_paragraphs 计算段落数量
    count_sentences 计算句子数量
    count_words 计算单词数量
    date_format 时间格式化
    default 给空变量设置一个默认值
    escape 用于html转码
    indent 每行缩进字符个数
    lower 变量小写
    nl2br 换行符转换成
    regex_replace 使用正则替换
    replace 替换和搜索
    spacify 在字符之间插入字符,默认插入空格
    string_format
    strip 替换所有重复空格,换行,tab为指定字符
    strip_tags 去除html标签
    truncate 截取字符串
    upper 变量大写
    wordwrap 指定段落宽度,默认80个字符

    foreach循环参数和设置

    foreach

    form 使用数组 array
    item 当前处理元素的值 string
    key 当前处理元素的键名 string
    name 循环的名称 string
    foreach 可调用变量

    iteration 循环当前执行次数
    first 是否是循环第一次执行,是则返回true
    last 是否是循环最后一次执行,是则返回true
    show
    total 循环执行总数

    section循环参数和变量

    section

    name 指定循环变量名
    loop 使用数组名
    start 从第几次开始
    setp 每次增加值
    max 最多循环次数
    show 是否显示该循环值为bool型
    section 可调用的变量

    index 显示当前索引,如果指定了start则从该设置值开始每次加1,如果设置了setp则按设置值增加
    index_prev 显示上次循环的索引值,首次值为-1
    index_next 显示下一次循环索引值
    iteration 显示循环次数
    first 判断是否是循环执行的首次,是则返回true
    last 判断是否是循环执行的最后一次,是则返回true
    rownum 同iteration,互为别名
    loop 显示该循环上次的索引
    show
    total 显示循环执行总次数

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

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

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

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

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