学习smarty手记一,如何配置smarty

学习PHPSMARTY

下载

先在smarty.php.net下载最新smarty

 

一般做法:

1解压smarty.

2 libs文件夹放在你的WEB目录下,我是放在smarty下的,目录情况如下:e:/wwwroot/smarty/libs
smarty目录下新建如下文件夹:
templates
(这里放你的模板文件,即tpl文件)
configs
templates_c
(自动将编译完的文件转成PHP放在这)
cache
然后在wwwroot目录下建立index.php

<?php
     
     
include "smarty/libs/Smarty.class.php";
     
     
define('__SITE_ROOT', 'e:/wwwroot/smarty'); // 最後沒有斜線
      
      
$tpl = new Smarty();
     
     
$tpl->template_dir = __SITE_ROOT . "/templates/";
     
     
$tpl->compile_dir = __SITE_ROOT . "/templates_c/";
     
     
$tpl->config_dir = __SITE_ROOT . "/configs/";
     
     
$tpl->cache_dir = __SITE_ROOT . "/cache/";
     
     
//$tpl->left_delimiter = '<{';  可根据需要更改。
      
      
//$tpl->right_delimiter = '}>';
     
     
$tpl->assign('name','world!');
     
     
$tpl->display('index.tpl')
     
     

   
   
    
     
   
   
?>
     
     

template目录下建立index.tpl

<!DOCTYPE HTML PUBLIC "-//W 3C //DTD HTML 4.0 Transitional//EN">

<html>

<head>

<title> New Document </title>

<meta name="Generator" content="EditPlus">

<meta name="Author" content="">

<meta name="Keywords" content="">

<meta name="Description" content="">

</head>

 

<body>

hello,{$name}~.

</body>

</html>

 

问题一 代码里必须重新定义了绝对路径

 解决方法 (注明参考朋友发过来的code)得出一种解决方式

1、将libs文件夹放在你的WEB目录下,我是放在smarty下的,目录情况如下:e:/wwwroot/smarty/libs

改为。去掉libs目录,直接把里面所有文件直接放到 e/:wwwroot/lib ,这样解压所有的smarty文件直接放到/www/lib/smarty下。

2、在根目录下建立include目录,然后新建一个smarty.php文件

配置为

<?

include("lib/smarty/Smarty.class.php");

$tpl = new Smarty();

$tpl->template_dir = " templates /";

$tpl->compile_dir =  " templates_c /";

$tpl->config_dir =  "configs/";

$tpl->cache_dir = "cache/";

?>

 

3index.php文件为

 

 

<?php
     
     
include "include/Smarty.php";
     
     
$tpl->assign('name','world!');
     
     
$tpl->display('index.tpl')
     
     

   
   
    
     
   
   
?>
     
     
4.这样一来大家都看清楚了吧,这样就不需要写上绝对路径啦define('__SITE_ROOT', 'e:/wwwroot/smarty'); // 最後沒有斜線
      
      

 

 
阅读终点,创作起航,您可以撰写心得或摘录文章要点写篇博文。去创作
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
好的,关于Smarty模板引擎的学习笔记,我可以为您提供以下内容: 1. Smarty是什么? Smarty是一个用PHP编写的模板引擎,它将PHP代码与HTML代码分离,使得代码更易于维护和设计。Smarty使用一套模板标签来代替PHP代码,这些标签可以在模板中使用,以生成最终的HTML代码。 2. Smarty的优势 - 简单易用:Smarty的模板标签简单易懂,可以快速上手。 - 分离代码:Smarty将PHP代码与HTML代码分离,使得代码更易于维护和设计。 - 可扩展性:Smarty支持插件机制,可以方便地扩展和定制功能。 - 性能优化:Smarty支持缓存机制,可以提高页面加载速度。 3. Smarty的基本语法 Smarty的模板标签由大括号包围,例如:{tag}。以下是Smarty的一些基本标签: - 输出变量:{$variable} - 条件语句:{if $variable == 'value'}{/if} - 循环语句:{foreach $array as $key => $value}{/foreach} - 引入文件:{include file="filename.tpl"} 4. Smarty的高级功能 - 模板继承:Smarty支持模板继承,可以减少模板重复代码。 - 插件机制:Smarty支持插件机制,可以方便地扩展和定制功能。 - 缓存机制:Smarty支持缓存机制,可以提高页面加载速度。 - 自定义函数:Smarty支持自定义函数,可以方便地扩展其功能。 以上是我为您提供的Smarty模板引擎学习笔记,希望对您有所帮助。如果您有其他问题,欢迎继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

designonline

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值