设置 | 登录 | 注册

目前共有4篇帖子。

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的方法很簡單,直接寫{_('字符串')}就行了

内容转换:

回复帖子
内容:
用户名: 您目前是匿名发表。
验证码:
看不清?换一张
©2010-2025 Purasbar Ver3.0 [手机版] [桌面版]
除非另有声明,本站采用知识共享署名-相同方式共享 3.0 Unported许可协议进行许可。