PHP code example of ajiho / think-smarty

1. Go to this page and download the library: Download ajiho/think-smarty library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ajiho / think-smarty example snippets




return [
    // 模板引擎左边标记
    'left_delimiter' => '<{',
    // 模板引擎右边标记
    'right_delimiter' => '}>',
    // 空格策略
    'auto_literal' => false,
    // 开启缓存
    'caching' => false,
    // 缓存周期(开启缓存生效) 单位:秒
    'cache_lifetime' => 120,
    // Smarty工作空间目录名称(该目录用于存放模板目录、插件目录、配置目录)
    'workspace_dir_name' => 'view',
    // 模板目录名
    'template_dir_name' => 'templates',
    // 插件目录名
    'plugins_dir_name' => 'plugins',
    // 配置目录名
    'config_dir_name' => 'configs',
    // 模板编译目录名
    'compile_dir_name' => 'templates_compile',
    // 模板缓存目录名
    'cache_dir_name' => 'templates_cache',
    // 全局输出替换
    'tpl_replace_string'  =>  []
];


namespace app\index\controller;

use app\BaseController;
use ajiho\smarty\facade\Smarty;

class Index extends BaseController
{
    public function index()
    {   
        // 通过应用实例获取smarty给模板赋值
        $this->app->smarty->assign('name','think-smarty');
        $this->app->smarty->assign('email','[email protected]');
        
        // 使用应用助手函数app('smarty')
        app('smarty')->assign('name','think-smarty');
        app('smarty')->assign('name','think-smarty');
            
         // 上面的方式还是太长,通过助手函数smarty()
         smarty()->assign('name','think-smarty');
         smarty()->assign('email','[email protected]');
        
         // 上面的方式还是太长,直接通过助手函数smarty_assign()一步到位
         smarty_assign('name','think-smarty');
         smarty_assign('email','[email protected]');
        
         // 一个一个赋值太麻烦,直接批量赋值
         smarty_assign([
            'name'  => 'think-smarty',
            'email' => '[email protected]'
         ]);
        
         // v2.0.2新增一个门脸类操作,该门脸类可以调用Smarty提供的所有方法
         Smarty::assign('name','smarty')
        
        // 模板输出
        return smarty_fetch('index.tpl');
        return Smarty::fetch('index.tpl') //v2.0.2+新增
    }
}

<{ $smarty.const.PHP_VERSION }>
<{ $smarty.server.SERVER_NAME }>
<{ $smarty.get.page}>
<{ $smarty.post.page}>
<{ $smarty.server.SCRIPT_NAME }>
<{ $smarty.env.PATH}>
<{ $smarty.session.user_id}>
<{ $smarty.cookies.name}>
<{ $smarty.request.username}>

<{ $think->request->param('name') }>
<{ $think->request->root() }>
<{ $think->request->root(true) }>
<{ $think->request->patch('name') }>
<{ $think->request->controller() }>
<{ $think->request->action() }>
<{ $think->request->ext() }>
<{ $think->request->host() }>
<{ $think->request->ip() }>
<{ $think->request->header('accept-encoding') }>
<{ $think->config->get('app.default_app') }>
<{ $think->config->get('app.default_timezone') }>
<{ $think->lang->get('


declare (strict_types=1);

namespace app\admin\controller;

use app\BaseController;
use think\App;

class Index extends BaseController
{
    public function index(App $app)
    {

        smarty_assign('think', $app);
        smarty_display('index.tpl');
    }
}


$smarty = new Smarty();//实例化smarty

//五配置 两方法
$smarty->setLeftDelimiter("{");  //左定界符
$smarty->setRightDelimiter("}"); //右定界符
$smarty->setTemplateDir("/path/templates");  //.tpl模板目录
$smarty->setCompileDir("/path/templates_c"); //模板编译生成的文件
$smarty->setCacheDir("/path/cache"); //缓存目录
$smarty->setConfigDir("/path/configs"); //配置目录
$smarty->setPluginsDir("/path/plugins"); //插件目录
$smarty->caching = true; //开始缓存
$smarty->cache_lifetime = 120; // 缓存时间

// 程序中使用
$smarty->assign('name','this is smarty');//传参到模板
$smarty->display('index.tpl');//渲染(展示模板)

// 渲染设置的模板路径下的tpl文件 path/templates/index.tpl
<!doctype html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

hello ! { $name }

</body>
</html>


namespace app\index\controller;

use app\BaseController;

class Index extends BaseController
{
    public function index()
    {   
        //方式一
        $this->app->smarty->assign('name','think-smarty')
        return $this->app->smarty->fetch('index.tpl')
        
        //方式二
        app('smarty')->assign('name','think-smarty')
        return app('smarty')->fetch('index.tpl')
        
        //方式三
        smarty_assign('name','think-smarty');
        return smarty_fetch('index.tpl');
        
        //方式四
        $name = 'think-smarty';
        return smarty_fetch('index.tpl',compact('name'));
        
        
        //方式五
        $name = 'think-smarty';
        return smarty_display('index.tpl',compact('name'));
    }
}


namespace app\index\controller;

use app\BaseController;

class Index extends BaseController
{
    public function index()
    {   
        $name = 'think-smarty';
        smarty_display('index.tpl',compact('name'));
    }
}

smarty_display('index@user/index.tpl');

smarty_display('/index.tpl');
smarty_display('/template/public/menu.tpl');

//明确指定资源类型,等价于smarty_display('index.tpl');
smarty_display('file:index.tpl');

smarty_display('file:C:/Users/Administrator/Desktop/tp6/index.tpl');
// 包括可以指定非项目路径,可以是磁盘上任何的绝对路径
smarty_display('file:G:/templates/index.tpl');

$content = '<{$name}>-<{$email}>';
//下次使用时编译
smarty_display('string:'.$content,['name'=>'ajiho','email'=>'[email protected]']);
//每次都编译
smarty_display('eval:'.$content,['name'=>'ajiho','email'=>'[email protected]']);

'tpl_replace_string'  =>  [
    '__STATIC__'=>'/static',
	'__JS__' => '/static/javascript',
]

// 加载配置
<{ config_load file="static_path.conf" }>

//使用配置,方式一
<{#__STATIC__#}>
//使用配置,方式二
<{ $smarty.config.__STATIC__ }>

__STATIC__ = '/static'
__JS__  = '/static/javascript'