zoukankan      html  css  js  c++  java
  • Php模板引擎Smarty安装和配置

    Smarty 是PHP的一个模板引擎,是由Monte Ohrt 和 Andrei Zmievski 使用PHP语言开发的,发展至今已成为一个非常流行的模板引擎,Smarty 提供了一种易于管理和使用的方法,将PHP代码从HMTL代码页面中分享出来,使用程序员与页面美工之间的分工更加明确,大大提高团队的开发效率。

    1、要使用Smarty 可到官方网站http://www.smarty.net 去下载,smary2.x 和3.x区别:

    【转】Smarty 3 API 的语法结构已经重构,使之更一致性和模块化,虽然为了向下兼容,仍然支持Smarty 2的语法,但会抛出一个被弃用的notice,虽然你可以屏蔽该notice,但强烈建议,在使用Smarty 3 时使用3的语法,一方面,Smarty 2的语法很可能在后面的版本中逐渐被取消,另一方面, Smarty2的语法,是对Smarty3的API的封装,所以性能方面也会有损失。 以下是 Smarty3与Smarty2的差别之处

    1) 基本上,Smarty3的方法采用驼峰式的命名方式,如 fooBarBaz ;

    2) 所有Smarty的属性都有get 和 set 的方法 如$smarty->cache_dir = ‘foo/’ 现在可以这样赋值 $smarty->setCacheDir('foo/'),同样可以通过 $smarty->getCacheDir() 来得到该属性值 ;

    3) Smarty 3废除了一些如 ”is*”的方法,因为他们和现在的”get*”方法重复了 ;

    4) Smarty 3 只能在PHP5下运行,不支持PHP4.;

    5) {php} 标签默认是关闭的. 使用$smarty->allow_php_tag=true.开启 ;

    6) 被空白包围的分隔符将不被解析,如{ foo }将不再作为smarty标签被解析,你必须使用{foo} 。

    转自:http://www.5dcode.com/?p=339

    下载完成按以下步骤安装:
    (1) 将下载完的 Smarty 压缩包解压到一个指定位置,比如C:/Smarty
    (2) 因为在程序中要用到 Smarty 的类库,而Smarty 类文件是位于 libs 文件夹下的Smarty.class.php文件,所以需要修改 PHP的配置文件 Php.ini 文件:查找 include_path 项,在下面添加 include_path = " .; c:/smarty/libs ",注意,多个路径可用分号隔开,因为新添加的路径会覆盖上面的路径设置。

    ; UNIX: "/path1:/path2"
    ;include_path = ".:/php/includes"
    ;
    ; Windows: "path1;path2"
    include_path=".;F:xamppphpPEAR"

    改为:

    ; Windows: "path1;path2"
    include_path=".;F:xamppphpPEAR;F:programFilesSmarty-3.1.16libs"


        页面引用时: require ( ' Smarty.class.php' );
    (3) 另一种做法是手动设置SMARTY_DIR常量,例如:
    define ( ' SMARTY_DIR' ,' /usr/local/lib/php/Smarty/ libs/' );
    require ( SMARTY_DIR.' Smarty.class.php' );
    (4) 还有一种做法是引用库文件的绝对路径,不推荐使用,代码如下:
    require ( '/usr/local/lib/php/Smarty/libs/Smarty.class.php' );
    [ 注意:其中路径以实际安装路径为准,文中只做参考。]

    在PHP中实例化Smarty对象的方法:

    <?php
    // NOTE: Smarty has a capital 'S'
    require_once('Smarty.class.php');
    $smarty = new Smarty();
    ?>
    

    现在,库文件已经放好了,可以开始为你的程序配置Smarty了:

    • Smarty可配置四个目录,默认名称分别是 templates/templates_c/configs/ 和cache/

    • 这些都分别对应Smarty类的属性定义 $template_dir$compile_dir$config_dir, 和 $cache_dir

    • 强烈建议分别在每个使用Smarty的程序中都单独定义这些目录。

    • 你可以通过testInstall() 来测试Smarty是否有权限读写这些目录。

    在下面的安装例子中,我们将为一个留言本程序建立Smarty环境。 我们提供了一个目录命名约定的例子。 你可以为任何的程序建立同样的环境,仅需要修改guestbook/名称。

    /usr/local/lib/Smarty-v.e.r/libs/
            Smarty.class.php
            debug.tpl
            sysplugins/*
            plugins/*
    
    /web/www.example.com/
            guestbook/
            templates/
                index.tpl
            templates_c/
            configs/
            cache/
            htdocs/
                index.php

    明确你的web服务器文档根目录。在下面的例子中, 文档根目录是/web/www.example.com/guestbook/htdocs/。 Smarty目录仅可以通过Smarty库文件访问,而不能直接被浏览器访问。 这样可以避免一些安全问题,强烈建议(但不强制)把这些目录 放到WEB服务器文档根目录之外

    将会有至少一个文件是放到文档根目录的,这个文件也会被浏览器访问到。 我们将这文件命名为index.php, 放置到文档根目录/htdocs/中。

    Smarty需要一些对目录的 读写权限 (windows用户请忽略),包括 $compile_dir 和$cache_dir 目录 (templates_c/ 和 cache/), 所以,要确保web服务器用户有权限读写它们。

    
    

    我们需要创建文件index.tpl,然后供Smarty显示。 文件需要放到 $template_dir目录内。

    Example 2.8. /web/www.example.com/guestbook/templates/index.tpl

    {* Smarty *}
    
    Hello {$name}, welcome to Smarty!
    

     {* Smarty *}是模板的 注释. 虽然不是必须的,但在模板内添加注释这是个很好的习惯。 它可以帮助识别出文件类型,而不需要看后缀。 比如说,代码编辑器可以识别该文件并自动语法高亮。

    现在,我们来修改index.php. 我们将创建Smarty的实例,给模板assign() 赋值变量, 并且display()显示该模板文件 index.tpl

    Example 2.9. 修改 /web/www.example.com/docs/guestbook/index.php

    <?php
    
    require_once(SMARTY_DIR . 'Smarty.class.php');
    
    $smarty = new Smarty();
    
    $smarty->setTemplateDir('/web/www.example.com/guestbook/templates/')
    $smarty->setCompileDir('/web/www.example.com/guestbook/templates_c/');
    $smarty->setConfigDir('/web/www.example.com/guestbook/configs/');
    $smarty->setCacheDir('/web/www.example.com/guestbook/cache/'); 
    
    $smarty->assign('name','Ned');
    
    //** un-comment the following line to show the debug console
    //$smarty->debugging = true;
    
    $smarty->display('index.tpl');
    
    ?>
    
        

    (上面的setTemplateDir等不写都默认是这几个文件夹,所以不写也可以)

    说明

    在我们的例子中,我们为Smarty的目录使用了绝对路径。 如果/web/www.example.com/guestbook/在你PHP的include_path内, 那么,这些设置不是必须的。 然而,设置成绝对路径,是更高效和更不容易出错(来自经验)。 这可以保证Smarty的目录路径被设置成正确的。

    现在,用浏览器访问index.php文件。 你可以看到"Hello Ned, welcome to Smarty!"

    你已经完成了Smarty的基础安装!

    参考了:http://www.smarty.net/docs/zh_CN/installing.smarty.basic.tpl

    稍微更灵活的方式是使用扩展类来安装Smarty和初始化。 代替反复地定义路径,赋同样的值等等,我们可以把这些操作放在一个地方进行。

    我们新建一个目录/php/includes/guestbook/,并新建一个 setup.php文件。 在下面的例子中,我们假设/php/includes 目录已经在include_path中。 确定你已经进行这个配置,或者使用绝对路径。

    <?php
    
    // load Smarty library
    require('Smarty.class.php');
    
    // The setup.php file is a good place to load
    // required application library files, and you
    // can do that right here. An example:
    // require('guestbook/guestbook.lib.php');
    
    class Smarty_GuestBook extends Smarty {
    
       function __construct()
       {
    
            // Class Constructor.
            // These automatically get set with each new instance.
    
            parent::__construct();
    
            $this->setTemplateDir('/web/www.example.com/guestbook/templates/');
            $this->setCompileDir('/web/www.example.com/guestbook/templates_c/');
            $this->setConfigDir('/web/www.example.com/guestbook/configs/');
            $this->setCacheDir('/web/www.example.com/guestbook/cache/');
    
            $this->caching = Smarty::CACHING_LIFETIME_CURRENT;
            $this->assign('app_name', 'Guest Book');
       }
    
    }
    ?>

    index.php文件中使用setup.php:

    Example 2.11. /web/www.example.com/guestbook/htdocs/index.php

     

    <?php
    
    require('guestbook/setup.php');
    
    $smarty = new Smarty_GuestBook();
    
    $smarty->assign('name','Ned');
    
    $smarty->display('index.tpl');
    ?>
    
       
    现在你可以看到这是非常简单就可以实例化一个Smarty的对象, 仅调用Smarty_GuestBook()就可以自动初始化程序。
  • 相关阅读:
    线程_Process实例
    线程_multiprocessing异步
    线程_multiprocessing实现文件夹copy器
    线程_GIL最简单的例子
    线程_FIFO队列实现生产者消费者
    线程_apply堵塞式
    正则表达式_合集下(后续还会有补充)
    正则表达式_合集上
    二分法查找
    数据结构_二叉树
  • 原文地址:https://www.cnblogs.com/youxin/p/3531268.html
Copyright © 2011-2022 走看看