Smartyの導入手順を簡潔にまとめてみた

Smarty

Smartyのダウンロード

下記(GitHub)サイトからSmartyをダウンロード。

GitHub - smarty-php/smarty: Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic.
Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic. - GitHub - smarty-php/smarty: Smarty is a te...

右下の「Download ZIP」をクリックしてダウンロード。
ダウンロードしたファイルの「libs」を「smarty」にリネームし、利用したいプロジェクトファイルに移動させる。

Smartyで必要なファイルの準備

プロジェクトファイル直下に以下のファイル、フォルダを作成する。
 ・templatesディレクトリ
 ・templates_cディレクトリ
 ・index.php

commonディレクトリは、.cssや.jsを設置する場所として、作成しておいても◎。
このとき、templates_cディレクトリのパーミッションは「777」にしておく必要があります
FTPソフトやコマンドで忘れずに設定しておきましょう。
コマンドでのパーミッションの設定方法はlinux参照。

作成した各ディレクトリの役割

 ・templates・・・テンプレートファイルを格納するディレクトリ
 ・templates_c・・・コンパイルされたファイルが格納されるディレクトリ
テンプレートファイルの作成
→「index.tpl」をtemplatesディレクトリ内に作成。

結果、各々のフォルダの役割としては、
 smarty・・・Smarty本体が格納されているディレクトリ
 templates・・・テンプレートファイルを格納
 templates_c・・・Smartyでコンパイルされたファイルを保存。コンパイルは自動で行われる(要パーミッション設定:777)
 ~.php・・・行いたいPHPの処理をここに書く。~.phpが処理したデータをテンプレートに渡す
 ~.tpl・・・表示したい表側(主にHTML)を記述する。PHP側の処理結果を出力する
 common・・・「.css」「.js」「img」等のファイルを格納。

こんな感じです。

タイトルとURLをコピーしました