目前共有4篇帖子。 字體大小:較小 - 100% (默認)▼  內容轉換:马新简体▼
 
點擊 回復
476 3
Smarty最简单的使用方法
一派掌門 二十級
1樓 發表于:2015-10-25 11:32
把smarty压缩包下载下来后解压,把libs文件夹放到自己网站的根目录下(或者也可以把文件夹的名称换掉,比如“根目录/includes/libraries/smarty”)。然后在网站根目录下建立一个templates文件夹,专门用来存放HTML页面模板。
打开templates文件夹,在其中创建以下三个文件夹:
templates_c
config
cache
然后创建一个index.tpl的HTML模板文件,内容如下:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>My First Smarty Page</title>
</head>

<body>
<header>
  <h2>My First Smarty Page </h2>
</header>
<main>
  <article>
    <section>Content for New main Tag Goes Here</section>
    <section>{$hello}</section>
  </article>
</main>
</body>
</html>

一派掌門 二十級
2樓 發表于:2015-10-25 11:34
在根目录下建立index.php文件作为浏览器访问的页面,内容如下:
<?php
date_default_timezone_set('UTC');
require_once 'libs/Autoloader.php';
Smarty_Autoloader::register();
$smarty = new Smarty();
$smarty->template_dir = 'templates';
$smarty->compile_dir ="templates/templates_c";
$smarty->config_dir = "templates/config";
$smarty->cache_dir ="templates/cache";
$hello = 'I\'d like to say Hello. How are your <b>feelings</b>?';
$smarty->assign('hello', $hello);
$smarty->display('index.tpl');
注意文件末尾不要加“?>”

$smarty->compile_dir ="templates/templates_c";这句话最好不要去掉,否则templates_c会出现在网站的根目录下,很难看
 
一派掌門 二十級
3樓 發表于:2015-10-25 11:36
然后浏览器访问http://localhost:81/index.php,输出的内容如下
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>My First Smarty Page</title>
</head>

<body>
<header>
  <h2>My First Smarty Page </h2>
</header>
<main>
  <article>
    <section>Content for New main Tag Goes Here</section>
    <section>I'd like to say Hello. How are your <b>feelings</b>?</section>
  </article>
</main>
</body>
</html>

 
一派掌門 二十級
4樓 發表于:2015-10-27 23:06
smarty调用gettext的方法很简单,直接写{_('字符串')}就行了
 

回復帖子

內容:
用戶名: 您目前是匿名發表
驗證碼:
(快捷鍵:Ctrl+Enter)
 

本帖信息

點擊數:476 回複數:3
評論數: ?
作者:巨大八爪鱼
最後回復:巨大八爪鱼
最後回復時間:2015-10-27 23:06
 
©2010-2025 Purasbar Ver2.0
除非另有聲明,本站採用共享創意姓名標示-相同方式分享 3.0 Unported許可協議進行許可。