新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

PHP模板smarty练习-创新互联

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

创新互联建站主要从事成都网站设计、网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务涞水,十多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:028-86922220

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}

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前标题:PHP模板smarty练习-创新互联
URL地址:http://www.wjwzjz.com/article/pdjei.html
在线咨询
服务热线
服务热线:028-86922220
TOP