资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

PHP模板smarty练习

PHP 模板smarty练习
一.练习环境
PHP 模板smarty练习

目前成都创新互联已为千余家的企业提供了网站建设、域名、网站空间、网站托管、服务器租用、企业网站设计、兴业网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

smarty_inc为smarty lib库
smarty_inc.php导入库文件

config_dir="Smarty/Config_File.class.php";
$smarty->caching=false;  //缓存
$smarty->template_dir = "./templates";  //模板文件
$smarty->compile_dir = "./templates_c"; // 设定编译文件的存储路径
//$smarty->cache_dir = "./smarty_cache";
$smarty->left_delimiter = "{";
$smarty->right_delimiter = "}";
?>

二.smarty变量练习
$smarty->assign 设置传输变量
$smarty->display 加载模板
index.php

assign('str',$str);
$smarty->display("index.html");
?>

index.html




    
    smarty test


    {$str}


访问localhost/smarty/index.php 直接转到index.html
this is my home!

三.smarty数组练习
index.php

assign('name',$students);

$smarty->display("index.html");

index.html




    
    smarty test



    {section name=stu loop=$name}
        {$name[stu]}
    {sectionelse}
        无内容
    {/section}

PHP 模板smarty练习

四.smarty二维数组练习
index.php

'sunqiang'];
$students[] = ['stu_name'=>'liuyao'];
$students[] = ['stu_name'=>'yuxx'];

$smarty->assign('name',$students);
$smarty->display("index.html");

index.html




    
    smarty test



    {section name=stu loop=$name}
        {$name[stu].stu_name}
    {sectionelse}
        无内容
    {/section}

PHP 模板smarty练习

五.smarty标量操作符练习
index.php

assign('str',$str);
$smarty->assign('time',time());
$smarty->assign('score',123.456);

$smarty->display("index.html");

index.html




    
    smarty test



原始字符:{$str}

首字母大写:{$str|capitalize}
计算字符数:{$str|count_characters}
连接内容:{$str|cat:' i love study php'}
段落数:{$str|count_paragraphs}
计算句数:{$str|count_sentences}
计算单词数:{$str|count_words}
日期显示:{$time|date_format:'%Y-%m-%d %H:%M:%S'} | {$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'} | {$smarty.now}
默认显示值:{$str1|default:'no content'}
转码:{$str|escape:url} | {$str|escape:html}
缩进:{$str|indent:5:'.'} | {$str|indent:5:' '}
大写:{$str|upper}
小写:{$str|lower}
替换:{$str|replace:'my':'your'} | {$str|replace:'my':'**'}
字符串格式化:{$score|string_format:'%.2f'} | {$score|string_format:'%d'}
去空格:{$str|strip:''} | {$str|strip:'_'}
去html标签:{$str|strip_tags}
截取:{$str|truncate:10:'...'}
行宽约束:{$str|wordwrap:10:'
'}

PHP 模板smarty练习

六.smarty内置函数练习

1.有键值数组
index.php

'sq','teacher2'=>'ly','teacher3'=>'yxx'];
$smarty->assign('teacher_name',$arr_str);

$smarty->display("index.html");

index.html




    
    smarty test



    {foreach from=$teacher_name item=name key=teach}
        数组内容:{$teach} - {$name}
{/foreach}

PHP 模板smarty练习

2.无键值数组

index.php

assign('teacher_name',$arr_str);

$smarty->display("index.html");

index.html




    
    smarty test



    {foreach from=$teacher_name item=name key=teach}
        数组内容:{$teach} - {$name}
{/foreach}

PHP 模板smarty练习

index.php

assign('teacher_name',$arr_str);

$smarty->display("index.html");

index.html




    
    smarty test



    {foreach from=$teacher_name item=name}
        数组内容:{$name}
{/foreach}

PHP 模板smarty练习

3.if条件语句
index.html




    
    smarty test



条件语句:
    {if name==''}
        没有设置内容
    {else}
        有设置内容
    {/if}

因为index.php没有设置这个变量,所有显示没有设置内容
PHP 模板smarty练习

4.include的使用
在模板下创建一个拼接文件head.html
PHP 模板smarty练习

index.html




    
    smarty test


{include file='head.html'}
条件语句:
    {if name==''}
        没有设置内容
    {else}
        有设置内容
    {/if}

PHP 模板smarty练习

注:
{include file=‘d:\www\index2.php’} 可以是别的目录下的引入文件
{include file='head.html' title=‘menu’}引入head.html,将title的值传给head.html的中的变量title

5.literal的使用
literal数据将被当作文本处理,此时模板将忽略其内部的所有字符信息,该特性用于显示有可能包含大括号等字符信息的javascript脚本,因为在模板设置中php的边界设定为大括号,防止冲突。

{literal}
    
{/literal}

6.Strip的使用
strip标记中数据的首尾空格和回车,这样可以保证模板容易理解且不用担心多余的空格导致问题,使用会做整行的处理,但内容不变,同时节省了流量,还可以保护代码。
使用时在html代码 头和尾
{strip}


{/strip}


分享文章:PHP模板smarty练习
网页路径:http://cdkjz.cn/article/jhjdce.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220