2010年9月25日 #php #php #環境構築 #smarty

ダウンロード

http://wiki.github.com/MrAnchovy/kohana-module-smarty/

インストール

githubでも手順がとても分かりやすいので直接引用させていただきます。

  1. Download the latest version from the links above
  2. Unpack the downloaded file
  3. Move the smarty directory into the Kohana modules directory
  4. Enable the module in your application's bootstrap.php
Kohana::modules(array(
  'auth' => MODPATH.'auth', // Basic authentication
  // 'cache' => MODPATH.'cache', // Caching with multiple backends
  // 'codebench' => MODPATH.'codebench', // Benchmarking tool
  'database' => MODPATH.'database', // Database access
  // 'image' => MODPATH.'image', // Image manipulation
  'orm' => MODPATH.'orm', // Object Relationship Mapping
  'pagination' => MODPATH.'pagination', // Paging of results
  'userguide' => MODPATH.'userguide', // User guide and API documentation
  'smarty' => MODPATH.'smarty', // smarty template module.
));
  1. Visit the page www.yoursite.com/smarty to confirm all is OK

その他リンク

このモジュールのファイル構造: http://github.com/MrAnchovy/kohana-module-smarty/wiki/file-structure

2010年9月25日 #mac #php #環境構築 #smarty

Smartyのインストールはphp.iniにインクルードパスを書く方法と書かない方法があります。ここではphpのインクルードパスに書く方法を紹介します。

1 Smartyをダウンロード

http://www.smarty.net/download.phpからダウンロードします。今回は3.0rc3にしました。

2 ファイルの解凍

できたフォルダ名はSmarty-3.0rc3で、それをsmartyにリネームして、/Applications/XAMPP/xamppfiles/lib/phpに移動します。こうするのはここがXAMPPの場合のinclude_pathになるからです。

3 php.iniを変更して、smartyを読み込ませる

php.ini中のinclude_pathに".:/Applications/XAMPP/xamppfiles/lib/php/smarty/libs"の記述を追加します。
できたイメージはこうなります。

include_path=".:/Applications/XAMPP/xamppfiles/lib/php:/Applications/XAMPP/xamppfiles/lib/php/pear.:/Applications/XAMPP/xamppfiles/lib/php/smarty/libs"

4 smartyが必要なフォルダを作成します。

smartyは四つのフォルダが必要です:

  • templates
  • templates_c
  • cache
  • config

templates_cとcacheフォルダには適切な書き込み権限を付与しなければなりません。
場所は任意のはずです。ここではこんな形にしました。htdocsはXAMPPのウェブルートフォルダです。

  • /Applications/XAMPP/xamppfiles/htdocs/smarty/templates
  • /Applications/XAMPP/xamppfiles/htdocs/smarty/config
  • /Users/zolo/Develop/smarty/templates_c
  • /Users/zolo/Develop/smarty/cache

5 smartyテンプレートを作成

上記templatesフォルダにsmarty.tplというファイルを作成します。

<html>
<body>
Hello, {$name}!
</body>
</html>

6 テンプレートに値をセットするphpファイルを作成

/Applications/XAMPP/xamppfiles/htdocs/にsmarty.phpというファイルを作成します。

<?php

// load Smarty library
require('Smarty.class.php');

$smarty = new Smarty;

$smarty->template_dir = '/Applications/XAMPP/xamppfiles/htdocs/smarty/templates';
$smarty->config_dir = ' /Applications/XAMPP/xamppfiles/htdocs/smarty/config';
$smarty->cache_dir = '/Users/zolo/Develop/smarty/cache';
$smarty->compile_dir = '/Users/zolo/Develop/smarty/templates_c';

$smarty->assign('name','kinopyo!');

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

7 動作確認

php.iniを編集したため、まずはapacheを再起動します。
そしてhttp://127.0.0.1/smarty.phpにアクセスしてエラーがなければOKです。

参考リンク:

http://news.php.net/php.smarty.dev/2703

2010年9月25日 #php #php #smarty

Smarty templateの作成

application/viewsにhello.tplというファイルを作成します。

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>{$title}</title>
</head>
<body>
    hello, {$name}
</body>
</html>

Controllerの作成

hello.phpというControllerを作成します。

<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Hello extends Controller {

    public function action_index()
    {
        $view = View::factory("smarty:hello");
        $view->name = "kinopyo!";
        $view->title = "Smarty & Kohana Sample";
        $this->request->response = $view;
    }
}

View::factoryに"smarty:"というプリフィックスを書くことでSmartyテンプレートとして認識してくれます。

動作確認

http://127.0.0.1/myapp/helloにアクセスし、"hello, kinopyo!"が表示されれば成功です。

Controller_Templateの場合

<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Hello extends Controller_Template {

    public $template = 'smarty:hello';

    public function action_index()
    {
        $this->template->name = 'kinopyo!';
        $this->template->title = "Hello Title";
    }

}