FuelPHPでSmarty_Fuel_Extensionを使う
フレームワークとしてFuelPHPを使い、View側にSmartyを使いたい。
Config::get('base_url') 的な感じで設定値を呼び出したい etc...
前提
FuelPHP 1.7.3
parserとしてSmartyが使える状況
作業
app/config/parser.php
<?php
return array(
'extensions' => array(
'tpl' => 'View_Smarty',
),
'View_Smarty' => array(
'extensions' => array('Smarty_Fuel_Extension')
),
);
以上です。
拡張子 .tpl の時にView_Smartyを使うようにします。
更に、View_Smarty => extensions に Smarty_Fuel_Extension を指定することで、
packages/parser/classes/smarty/fuel/extension.php
が読み込まれるようになります。
tplファイルでの記述
sample/index.tpl
{fuel_version} = FuelPHPのバージョン
{config name=base_url default=''} = Config::get('base_url', '') と同等の処理
といったことができます。
コントローラー側はこんな感じ
sample.php
<?php
class Controller_Sample extends Controller
{
public function action_index()
{
return Response::forge(View::forge('sample/index.tpl'));
}
}
他にどんなのが使える?
packages/parser/classes/smarty/fuel/extension.php ファイルを見るとどう処理されているかを含め確認できます。
packages/parser/classes/smarty/fuel/extension.php
$smarty->registerPlugin('function', 'fuel_version', array($this, 'fuel_version'));
$smarty->registerPlugin('function', 'url', array($this, 'url'));
$smarty->registerPlugin('function', 'base_url', array('Uri', 'base'));
$smarty->registerPlugin('function', 'current_url', array('Uri', 'current'));
$smarty->registerPlugin('function', 'uri_segment', array($this, 'uri_segment'));
$smarty->registerPlugin('function', 'uri_segments', array('Uri', 'segments'));
$smarty->registerPlugin('function', 'config', array($this, 'config_get'));
$smarty->registerPlugin('function', 'lang', array($this, 'lang_get'));
$smarty->registerPlugin('block', 'form', array($this, 'form'));
$smarty->registerPlugin('function', 'form_input', array($this, 'form_input'));
$smarty->registerPlugin('function', 'form_password', array($this, 'form_password'));
$smarty->registerPlugin('function', 'form_hidden', array($this, 'form_hidden'));
$smarty->registerPlugin('function', 'form_button', array($this, 'form_button'));
$smarty->registerPlugin('function', 'form_reset', array($this, 'form_reset'));
$smarty->registerPlugin('function', 'form_submit', array($this, 'form_submit'));
$smarty->registerPlugin('function', 'form_textarea', array($this, 'form_textarea'));
$smarty->registerPlugin('block', 'form_fieldset', array($this, 'form_fieldset'));
$smarty->registerPlugin('function', 'form_label', array($this, 'form_label'));
$smarty->registerPlugin('function', 'form_checkbox', array($this, 'form_checkbox'));
$smarty->registerPlugin('function', 'form_radio', array($this, 'form_radio'));
$smarty->registerPlugin('function', 'form_file', array($this, 'form_file'));
$smarty->registerPlugin('function', 'form_select', array($this, 'form_select'));
$smarty->registerPlugin('function', 'form_val', array($this, 'form_val'));
$smarty->registerPlugin('function', 'input_get', array($this, 'input_get'));
$smarty->registerPlugin('function', 'input_post', array($this, 'input_post'));
$smarty->registerPlugin('function', 'asset_add_path', array($this, 'asset_add_path'));
$smarty->registerPlugin('function', 'asset_css', array($this, 'asset_css'));
$smarty->registerPlugin('function', 'asset_js', array($this, 'asset_js'));
$smarty->registerPlugin('function', 'asset_img', array($this, 'asset_img'));
$smarty->registerPlugin('function', 'asset_render', array($this, 'asset_render'));
$smarty->registerPlugin('function', 'asset_find_file', array($this, 'asset_find_file'));
$smarty->registerPlugin('function', 'html_anchor', array($this, 'html_anchor'));
$smarty->registerPlugin('function', 'session_get_flash', array($this, 'session_get_flash'));
$smarty->registerPlugin('block', 'markdown', array($this, 'markdown_parse'));
$smarty->registerPlugin('function', 'auth_has_access', array($this, 'auth_has_access'));
$smarty->registerPlugin('function', 'auth_check', array($this, 'auth_check'));
このくらいのfunctionやblockが使える様になります。