LoginSignup
8
10

More than 5 years have passed since last update.

MacでSmartyを試す

Last updated at Posted at 2015-10-26

MacのローカルにPHPのテンプレートエンジン「Smarty」を入れて動かしてみます。
Smartyのインストールにはcomposerを用います1

composerでSmartyをインストール

今回は ~/test/ ディレクトリで作業します。

$ mkdir test
$ cd test

composer install の場合

# composer.jsonファイル準備
$ vi composer.json

Smarty 3.1系の最新安定版をインストールする設定2を書きます。

composer.json
{
  "require": {
    "smarty/smarty": "~3.1"
  }
}
$ composer install
Loading composer repositories with package information
Installing dependencies (including require-dev)
  - Installing smarty/smarty (v3.1.27)
    Downloading: 100%

Writing lock file
Generating autoload files

composer require の場合

$ composer require smarty/smarty
Using version ^3.1 for smarty/smarty
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing smarty/smarty (v3.1.27) Downloading: 100%
Writing lock file
Generating autoload files

require の場合、composer.json は自動的に生成されます。

生成されたcomposer.jsonの内容
$ cat composer.json
{
    "require": {
        "smarty/smarty": "^3.1"
    }
}

テスト用コンテンツ準備

header.tpl
<!DOCTYPE html>
<meta charset="utf-8">
<title> {$page_title} </title>
index.tpl
{*
  これはコメントです。
  別ファイルで定義したヘッダを読み込み。
  このページのタイトルを定数で指定
*}
{include file='header.tpl' page_title={$smarty.const.MY_TITLE}}

{* 予約変数(一部) *}
<dl>
  <dt>現在の タイムスタンプ
  <dd> {$smarty.now}

  <dt>現在処理中のテンプレートファイル名
  <dd> {$smarty.template}

  <dt>このテンプレートをコンパイルした Smarty のバージョン
  <dd> {$smarty.version}
</dl>

{* 変数 *}
<p> {$hello}

{* メソッド *}
<p> {$today->format('Y-m-d (D)')}

<ul>
{* 配列 $animal のすべての値を逆順に表示 *}
{section name=i loop=$animal step=-1}
    <li> {$animal[i]}
{/section}
</ul>

{* 連想配列 $address の内容をループ *}
{section name=i loop=$address}
<ul>
  <li> name: {$address[i].name}
  <li> home: {$address[i].home}
  <li> cell: {$address[i].cell}
  <li> e-mail: {$address[i].email}
</ul>
{/section}
index.php
<?php

require_once 'vendor/autoload.php';

ini_set('date.timezone', 'Asia/Tokyo');
define('MY_TITLE', 'このページのタイトル');

$smarty = new Smarty();

$smarty->assign('hello', 'Hello, world!');
$smarty->assign('today', new DateTime());
$smarty->assign('animal', array('いぬ', 'さる', 'きじ'));
$smarty->assign('address', array(
          array('name' => 'John Smith', 'home' => '555-555-5555',
                'cell' => '666-555-5555', 'email' => 'john@myexample.com', ),
          array('name' => 'Jack Jones', 'home' => '777-555-5555',
                'cell' => '888-555-5555', 'email' => 'jack@myexample.com', ),
          array('name' => 'Jane Munson', 'home' => '000-555-5555',
                'cell' => '123456', 'email' => 'jane@myexample.com', ),
              ));

$smarty->display('index.tpl');

ビルトインウェブサーバ起動

# ビルトインウェブサーバ起動
$ php -S localhost:8001

ブラウザアクセス

ブラウザでhttp://localhost:8001にアクセス

↓こんな感じの物が出力

出力
<!DOCTYPE html>
<meta charset="utf-8">
<title> このページのタイトル </title>



<dl>
  <dt>現在の タイムスタンプ
  <dd> 1445867579

  <dt>現在処理中のテンプレートファイル名
  <dd> index.tpl

  <dt>このテンプレートをコンパイルした Smarty のバージョン
  <dd> 3.1.27
</dl>


<p> Hello, world!


<p> 2015-10-26 (Mon)

<ul>

    <li> きじ
    <li> さる
    <li> いぬ
</ul>


<ul>
  <li> name: John Smith
  <li> home: 555-555-5555
  <li> cell: 666-555-5555
  <li> e-mail: john@myexample.com
</ul>
<ul>
  <li> name: Jack Jones
  <li> home: 777-555-5555
  <li> cell: 888-555-5555
  <li> e-mail: jack@myexample.com
</ul>
<ul>
  <li> name: Jane Munson
  <li> home: 000-555-5555
  <li> cell: 123456
  <li> e-mail: jane@myexample.com
</ul>

おわりです。

参考や注釈


  1. 今回、composerはhomebrewで既に導入済みでした。 

  2. smarty/smarty - Packagist The PHP Package Repository 

8
10
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
8
10