Smartyを使う

メモ程度に.逐次追加予定.

インストール

sudo apt-get install smarty

使い方

sample.php

<?php
require_once('smarty/Smarty.class.php');

$smarty = new Smarty();
# テンプレートファイルが格納されているディレクトリを指定
$smarty->template_dir = './template/';
# コンパイル済みテンプレートファイルを格納するディレクトリを指定
# (Smartyが勝手にファイルを置くので気にする必要はない
$smarty->compile_dir = './compile/';
# テンプレートファイル内で使う変数を割り当てる.
# 第1引数は変数名,第2引数は変数の値
$smarty->assign('name', 'miettal');
# 表示
$smarty->display('sample.tpl');
?>

tplSmartyでよく使われる拡張子
sample.tpl

<html>
<head>
<title>Sample</title>
</head>
<body>
ようこそ{$name}さん
</body>
</html>

テンプレートの文法

include
{include file="file"}
if
{if $val eq 1}
{/if}
if-else
{if $val eq 1}
{else}
{/if}
if-elseif-else
{if $val eq 1}
{elseif $val eq 2}
{else}
{/if}