Windows環境でのSmartyのインストールと使用
一:Smarty紹介
Smartyを使用するには、プログラムアプリケーションロジックとWebプレゼンテーションロジックを明確に分離するという原則を身につけなければなりません.つまりPHPプログラムにはHTMLコードがあまり入っていないということです.プログラムの中でそれらの変数をテンプレートに要塞することを決定して、テンプレートに自分でこれらの変数をどのように表現するかを決定させます
Smartyでは、すべて変数を主とし、すべてのプレゼンテーションロジックはテンプレートを自分で制御します.Smartyには独自のテンプレート言語があるので、ブロックが表示されるかどうかにかかわらず、繰り返すかどうかにかかわらず、Smartyのテンプレート構文(if,foreach,section)で変数の内容を組み合わせて提示します.そうなるとテンプレートが少し複雑になっているような気がしますが、計画が適切であればPHPプログラムは1行も変更する必要はありません.
二:Smartyテンプレートの実行概略図:
3:Smartyインストール構成:
1.公式サイトのダウンロード:http://www.smarty.net/最新バージョンはSmarty-2.6です.21
2.Smarty-2.6を解凍する.21、中のlibsディレクトリの下のファイルは私たちが必要としています.
3.サイトのルートディレクトリの下にプロジェクトフォルダsmartyDmoe/を作成し、libsフォルダ全体をsmartyDmoeディレクトリの下に試験します.
4.smartyDmoe/の下に6つのフォルダを新規作成:
cache configs templates//テンプレートファイルtemplates_c//smartyコンパイルされたファイル
includes//function、sqlファイルを配置するために使用されます
modules//はプログラムモジュールを配置するために使用されており、プログラムをあちこちに置き去りにすることはなく、全体的なアーキテクチャが一目瞭然です.
5. test.htm、templatesディレクトリの下に保存
6.ファイルテンプレートプロファイルの作成:main.php、プロジェクトホームsmartyDmoe/の下に保存
7.ファイルを作成するphpファイル、プロジェクトホームsmartyDmoe/の下に置く
8.ブラウザで入力http://localhost/smartyDmoe/test.php
表示:smarty構成に成功しました
9.ウェブサイトでSmartyテクノロジーをグローバルに使用できるように、PHP.を修正することができます.incの中の
; Windows: "\path1;\path2"
include_path = ".;d:\php\includes"
変更後:
; Windows: "\path1;\path2"
include_path = ".;d:\php\includes;d:\web\smartyDmoe\libs"
次の文はもう書かなくてもいいです.
include "libs/Smarty.class.php";
最後にSmartyプログラム全体の作成手順を整理します:Step 1.Smartyテンプレートエンジンをロードします. Step 2. Smartyオブジェクトを作成します. Step 3. Smartyオブジェクトのパラメータを設定します. Step 4. プログラムで変数を処理した後、Smartyのassignメソッドで変数をテンプレートに配置します. Step 5. Smartyのdisplayメソッドを利用してWebページを表示します.
四:制御テンプレートの内容、重複領域
1.Smartyテンプレートでは、1つのブロックを繰り返すにはforeachとsectionの2つの方法があります.この2つの関数の出力は同じです.
(1.foreachは巣状に処理する方法で私たちのassignの2層の配列変数を提示する.
(2.sectionは「主配列[ループ名].サブ配列インデックス」で配列全体を表示
ここでforeach関数はPHPのforeach関数と同じである.
sectionはSmartyが配列変数のように発展した記述を処理するためである.
2.配列indexインデックスは0から開始します.そうしないと、section出力時に最後のインデックス値は出力されません.indexインデックスが整数でない場合、sectionは取得されません.
3.例:
(1.test 2.phpコード:
(2.test 2.htmlコード:
(3.運転結果:
Smartyを使用するには、プログラムアプリケーションロジックとWebプレゼンテーションロジックを明確に分離するという原則を身につけなければなりません.つまりPHPプログラムにはHTMLコードがあまり入っていないということです.プログラムの中でそれらの変数をテンプレートに要塞することを決定して、テンプレートに自分でこれらの変数をどのように表現するかを決定させます
Smartyでは、すべて変数を主とし、すべてのプレゼンテーションロジックはテンプレートを自分で制御します.Smartyには独自のテンプレート言語があるので、ブロックが表示されるかどうかにかかわらず、繰り返すかどうかにかかわらず、Smartyのテンプレート構文(if,foreach,section)で変数の内容を組み合わせて提示します.そうなるとテンプレートが少し複雑になっているような気がしますが、計画が適切であればPHPプログラムは1行も変更する必要はありません.
二:Smartyテンプレートの実行概略図:
3:Smartyインストール構成:
1.公式サイトのダウンロード:http://www.smarty.net/最新バージョンはSmarty-2.6です.21
2.Smarty-2.6を解凍する.21、中のlibsディレクトリの下のファイルは私たちが必要としています.
3.サイトのルートディレクトリの下にプロジェクトフォルダsmartyDmoe/を作成し、libsフォルダ全体をsmartyDmoeディレクトリの下に試験します.
4.smartyDmoe/の下に6つのフォルダを新規作成:
cache configs templates//テンプレートファイルtemplates_c//smartyコンパイルされたファイル
includes//function、sqlファイルを配置するために使用されます
modules//はプログラムモジュールを配置するために使用されており、プログラムをあちこちに置き去りにすることはなく、全体的なアーキテクチャが一目瞭然です.
5. test.htm、templatesディレクトリの下に保存
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>{$title}</title>
</head>
<body>
{$content}
</body>
</html>
6.ファイルテンプレートプロファイルの作成:main.php、プロジェクトホームsmartyDmoe/の下に保存
<?php
include "libs/Smarty.class.php";
@define('__SITE_ROOT', str_replace("\\","/",dirname(__FILE__)));
$tpl = new Smarty();
$tpl->template_dir = __SITE_ROOT . "/templates/";
$tpl->compile_dir = __SITE_ROOT . "/templates_c/";
$tpl->config_dir = __SITE_ROOT . "/configs/";
$tpl->cache_dir = __SITE_ROOT . "/cache/";
$tpl->left_delimiter = '{';
$tpl->right_delimiter = '}';
?>
//left_delimiter
right_delimiter
, Smarty.class.php 。
7.ファイルを作成するphpファイル、プロジェクトホームsmartyDmoe/の下に置く
<?php
require "config.php";
$tpl->assign("title", "smarty ");
$tpl->assign("content", "smarty ");
$tpl->display('test.htm');
?>
8.ブラウザで入力http://localhost/smartyDmoe/test.php
表示:smarty構成に成功しました
9.ウェブサイトでSmartyテクノロジーをグローバルに使用できるように、PHP.を修正することができます.incの中の
; Windows: "\path1;\path2"
include_path = ".;d:\php\includes"
変更後:
; Windows: "\path1;\path2"
include_path = ".;d:\php\includes;d:\web\smartyDmoe\libs"
次の文はもう書かなくてもいいです.
include "libs/Smarty.class.php";
最後にSmartyプログラム全体の作成手順を整理します:Step 1.Smartyテンプレートエンジンをロードします. Step 2. Smartyオブジェクトを作成します. Step 3. Smartyオブジェクトのパラメータを設定します. Step 4. プログラムで変数を処理した後、Smartyのassignメソッドで変数をテンプレートに配置します. Step 5. Smartyのdisplayメソッドを利用してWebページを表示します.
四:制御テンプレートの内容、重複領域
1.Smartyテンプレートでは、1つのブロックを繰り返すにはforeachとsectionの2つの方法があります.この2つの関数の出力は同じです.
(1.foreachは巣状に処理する方法で私たちのassignの2層の配列変数を提示する.
(2.sectionは「主配列[ループ名].サブ配列インデックス」で配列全体を表示
ここでforeach関数はPHPのforeach関数と同じである.
sectionはSmartyが配列変数のように発展した記述を処理するためである.
2.配列indexインデックスは0から開始します.そうしないと、section出力時に最後のインデックス値は出力されません.indexインデックスが整数でない場合、sectionは取得されません.
3.例:
(1.test 2.phpコード:
<?php
require "config.php";
//index 0 , section , index ,section
//$array1 = array(1=>" ", 2=>" ", 3=>" ", 4=>" ");
//$array1 = array("a"=>" ", "b"=>" ", "c"=>" ", "d"=>" ");
$array1 = array(=>" ", 2=>" ", 3=>" ", 4=>" ");
$tpl->assign("array1", $array1);
$array2 = array(
array("index1" => "data1-1", "index2" => "data1-2", "index3" => "data1-3"),
array("index1" => "data2-1", "index2" => "data2-2", "index3" => "data2-3"),
array("index1" => "data3-1", "index2" => "data3-2", "index3" => "data3-3"),
array("index1" => "data4-1", "index2" => "data4-2", "index3" => "data4-3"),
);
$tpl->assign("array2", $array2);
$tpl->display("test2.html");
?>
(2.test 2.htmlコード:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> </title>
</head>
<body>
<!-- foreach array1-->
foreach array1--->
{foreach item=arr1 from=$array1}
{$arr1}
{/foreach}
<br>
<!-- section array1-->
section array1--->
{section name=sec1 loop=$array1}
{$array1[sec1]}
{/section}
<br>
<!-- foreach array2-->
foreach array2---><br>
{foreach item=index2 from=$array2}
{foreach key=key2 item=item2 from=$index2}
{$key2}: {$item2}
{/foreach}
<br>
{/foreach}
<br>
<!-- section array2-->
section array2---><br>
{section name=sec2 loop=$array2}
index1: {$array2[sec2].index1}
index2: {$array2[sec2].index2}
index3: {$array2[sec2].index3}
<br>
{/section}
</body>
</html>
(3.運転結果:
foreach array1--->
<!----> section array1--->
<!----> foreach array2--->
index1: data1-1 index2: data1-2 index3: data1-3
index1: data2-1 index2: data2-2 index3: data2-3
index1: data3-1 index2: data3-2 index3: data3-3
index1: data4-1 index2: data4-2 index3: data4-3
<!----> section array2--->
index1: data1-1 index2: data1-2 index3: data1-3
index1: data2-1 index2: data2-2 index3: data2-3
index1: data3-1 index2: data3-2 index3: data3-3
index1: data4-1 index2: data4-2 index3: data4-3