Laravel4でもSmartyを使用したい!
2014.11.20
この記事は最終更新日から1年以上が経過しています。
どもです。
Laravel4でもSmartyを使用したい使用したい使用….。。。
そう。Laravelはデフォルトで bladeテンプレートが備わっているのですが、別のテンプレートエンジンが使いたい。
といったわがままを言って見たい時期だったりもする訳ですよ。
使い慣れたテンプレートエンジンが良かったりしますよねー。
できるのかなー。と思っていたら、
.
..
..
できますねー!! ><
こちらとか
http://packalyst.com/packages/package/comnect/smarty
http://blog.comnect.jp.net/blog/76
こちらを参考です。
インストール
for Laravel4.2
"require": {
"comnect/smarty": "2.*"
},
for Laravel4.1
"require": {
"comnect/smarty": "1.*"
},
とありますので、御使いのバージョンに合わせcomposer.jsonに追加します。
こんな感じで
"require": {
"laravel/framework": "4.1.*",
"comnect/smarty": "1.0"
},
そして、Laravelのapp/config/app.phpのprovidersに以下を追加して
'Comnect\Smarty\SmartyServiceProvider'
コマンドラインでcomposer updateを入力、インストールを行います。
composer update
インストールが完了したら、artisanコマンドでコンフィグをパブリッシュ。
php artisan config:publish comnect/smarty
実行すると app/conig/packagesの下にcomnect/smarty/config.php といった、smartyのコンフィグと同様のコンフィグファイルが作成されます。
<?php
// smarty configure
return [
// smarty file extension
'extension' => 'tpl',
//
'debugging' => false,
// use cache
'caching' => false,
//
'cache_lifetime' => 120,
//
'compile_check' => false,
// delimiters
// default "{$smarty}"
'left_delimiter' => '{',
'right_delimiter' => '}',
// path info
'template_path' => app_path() . '/views',
'cache_path' => app_path() . '/storage/smarty/cache',
'compile_path' => app_path() . '/storage/smarty/compile',
'plugins_paths' => [
app_path() . '/views/smarty/plugins',
]
];
デフォルトではapp/storageの下にcacheとcompileが出力されますので、app/storageに必ず実行権限を与えてください。
ひとまずこれで使用可能になりました。
使用方法
特に変更しなくても使用できます。
return View::make('home');
views配下にsmartyファイルがあればそれをテンプレートと使用し、なければ通常通りbladeテンプレートかphpファイルを使用するようになっております。
なので、bladeテンプレートもそのまま併用も可能となっております。優秀!
smartyのメソッドも利用可能です。
use smarty method
View::assign('word', 'hello');
View::clearAllAssign();
値も一緒に渡したい場合
return View::make('template', ['values' => $values]);
withでも同じです
return View::make('template')->with('values', $values);
Artisan
artisanコマンドでキャッシュをクリアしたい場合は
cache clear
php artisan comnect:smarty-cacheclear
Options
--file (-f) specify file
--time (-t) clear all of the files that are specified duration time
–file (-f) specify file::削除したいファイルを指定
–time (-t) clear all of the files that are specified duration time::cacheの経過時間を指定して削除
remove compile class
また、コンパイルファイルの削除も行えます。
php artisan comnect:smarty-clear-compiled
Options
--file (-f) specify file
–file (-f) specify file:削除したいファイルを指定
その他、smarty modifier, sumarty functionなどすべて使用可能となっております。ただし、配列で記述する場合はショートシンタックスのみ対応のようです。
いやー助かりますねー。よろしければー。ではでは。
















