LoginSignup
23
28

More than 5 years have passed since last update.

Smarty再入門

Last updated at Posted at 2014-05-09

Smartyを数年ぶりにさわってみたので要点をメモ。

yotsuba_s.jpg はじめに

SmartyはPHPテンプレートエンジンでは一番有名なやつです。(ただ、別記事にも書いてますが、Smartyもどきは10行ほどで簡単に自作できます)
http://qiita.com/yasumodev/items/049c41a2f90db935503c

yotsuba_s.jpg 導入手順

まずはインストール…というか、導入方法は色々あるようですが、php.ini 設定不要のアップロードするだけでOKなやり方でいきます。

ダウンロードはここから。
http://www.smarty.net/download

1)Latest Stable Release にある Smarty-3.1.18.zip をダウンロード。

2)解凍すると、libsフォルダがあるのでそれをそのままアップ。

3)フォルダ構成は下記の通り。

|-libs
|    |-Smarty.class.php
|    |-SmartyBC.class.php
|    |-plugins
|    |-sysplugins
|    |-debug.tpl
|
|(以下は、自力で作成)
|
|-templates_c
|-templates
|    |-hello.tpl
|
|-hello.php

4)libsフォルダと同じ階層に次の2フォルダ作成。
 templates_c、templates
 (ひとまずパーミッションは777で…。)

5)hello.tpl を作成し、構成図の場所にアップ。

hello.tpl(UTF-8)
<html>
<meta charset="utf-8">
こんにちは、{$Name}さん。
<ul>
{foreach from=$Fruits key=key item=item}
    <li>{$key}: <b>{$item}</b></li>
{/foreach}
</ul>
</html>

6)hello.phpを作成し、構成図の場所にアップ。

hello.php(UTF-8)
<?php
require './libs/Smarty.class.php';
$smarty = new Smarty();
//// 下記2フォルダの相対位置が変わるなら明示的に指定
//$smarty->setTemplateDir('./templates/');
//$smarty->setCompileDir('./templates_c/');
$smarty->assign("Name", "果物屋");
$smarty->assign("Fruits", array("みかん", "りんご", "バナナ"));
$smarty->display('hello.tpl');
?>

7)あとはブラウザから hello.php にアクセスして確認。

(・o・ゞ いじょー。

23
28
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
28