« 独習PHP - 山田 祥寛 | メイン | 初めてのPerl - Randal L. Schwartz »

2008年4月 7日

PHPのテンプレートエンジン「Smarty」

?u?b?N?}?[?N???B???

独習PHPで「Smarty」の使い方を軽く勉強したので軽くメモ。とりあえずインストールの仕方と最小限のページ。

「Smarty」のインストール

debian (sid)でのインストールはとても簡単でした。aptで一発。
# apt-get install smarty
これでインストールされます。

基本的な使い方

基本的な使い方です。まず最初に必要なフォルダを用意する必要があります。Smartyを動かすアプリケーションの入っているフォルダに、3つのフォルダを作成します。
# mkdir cache
# mkdir templates
# mkdir templates_c
その後、実際にコードを動かすコードを含むファイルとテンプレートを書いたファイルを作成します。 まず、実際にコードを動かすファイルはこんな感じ。
# vi testSmarty.php
===
<?php
require_once('smarty/libs/Smarty.class.php');
$o_smarty=new Smarty();
$o_smarty->template_dir='./templates/';
$o_smarty->compile_dir='./templates_c/';
$o_smarty->assign('name','Smarty');
$o_smarty->display('testSmarty.tpl');
?>
===
続いて、テンプレートを書いたファイルはこんな感じ。さっき作ったtemplatesフォルダいかに作ります。
# vi templates/testSmarty.tpl
===
<html>
<head>
<title>Test Smarty</title>
</head>
<body>
Test {name}
</body>
</html>
===

これでブラウザから「http://localhost/testSmarty.php」にアクセスすれば無事ページが表示されるはずです。


  1. プログラミングPHP - Rasmus Lerdorf
  2. 独習PHP - 山田 祥寛
  3. 第3回CakePHP勉強会



トラックバック

このエントリーのトラックバックURL:
http://webcrawl.s288.xrea.com/mt/mt-tb.cgi/501

コメントを投稿

サイト内検索

Google
 

2008年7月

    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
アーカイブ

Author Info.

mypic mkataigi
Webエンジニア
mail:makoto@wcrawl.com