kazpgmの日記

『プログラム自動作成@自動生成』作成の日記

TOOL更新_viewをsmartyで。の続き。logininfoDetailView.html

TOOL更新_viewをsmartyで。の続き。logininfoDetailView.htmlに以下のようなところがある。($ELEMENTSはコード一覧。このコード一覧からコード値を取得している。)

<th width="18%" nowrap  >ログイン権限</th>
<td>
<?=esc($ELEMENTS['LOGIN_TYPE'][$o['login_type']][$o['c_elements_idx']]) ?>
</td>

これをsmartyにおきかえる。

■common.php 修正
自作「テンプレート関数プラグイン」をインクルードした。

/* Smartyを設定 */
require_once(DIR_SMARTY . 'Smarty.class.php');
$smarty = new Smarty();
$smarty->template_dir = DIR_TEMPLATE; // テンプレートファイルが存在するディレクトリ
$smarty->compile_dir = DIR_COMPILE; // テンプレートのキャッシュを作成するディレクトリ
$smarty->left_delimiter = '{{';
$smarty->right_delimiter = '}}';
$smarty->default_modifiers = 'escape:"html"';

<<追加 start>>
 //Smarty Plugin
 require_once('AppSmartyPlugin.php');
 $smarty->register_function("get_element", "smarty_function_get_element");
 $smarty->register_modifier("td_nl2br", "smarty_modifier_td_nl2br");
<<追加 end>>
>||

Zend_Registry::set('smarty',$smarty);

■AppSmartyPlugin.php 新規
自作「テンプレート関数プラグイン」を作った。

<?php
/**
 * AppSmartyPlugin.php
 */

/**
 * -------------------------------------------------------------
 * Smarty {get_element} function plugin
 *
 * Type:     function<br>
 * Name:     get_element<br>
 * Input:<br>
 *           - elename    コード一覧(AppElements)のコード名
 *           - value      コード値
 *           - idx        elements_idx ('0':日本語、'1':英語) デフォルト='0'
 *           - tdKbn      テーブルTD区分 ('TD':テーブルTD、 以外(指定なしを含む))
 * Purpose:  AppElementsからコード値を取得しエスケープして戻します。
 * @param array
 * @param Smarty
 * @return string
 * @uses smarty_function_escape_special_chars()
 * -------------------------------------------------------------
 **/
function smarty_function_get_element($params, &$smarty)
{
    require_once $smarty->_get_plugin_filepath('shared','escape_special_chars'); // エスケープ処理

    $elements = AppElements::getInstance();	// コード一覧
    if( isset($params['elename']) == false){
        $smarty->trigger_error("get_element: missing 'elename' parameter");
        return;
    }
    if( isset($params['value']) == false){
        $smarty->trigger_error("get_element: missing 'value' parameter");
        return;
    }
    if( isset($params['idx']) == false){
        $params['idx']='0'; // 日本用
    }
    if( isset($params['tdKbn']) == false){
        $params['tdKbn']=''; // テーブルTD以外
    }
    if(mb_strwidth($params['elename']) == 0 || 
       mb_strwidth($params['value'])   == 0){ // パラメータに値がない場合
        $rtn = "";
    } else {
        $rtn = smarty_function_escape_special_chars(
               $elements->elements[$params['elename']][$params['value']][$params['idx']]);
    }
    if (strtoupper($params['tdKbn']) == 'TD' && 
        (is_null($rtn) || $rtn == "")){ // テーブルTDの場合かつ、値が空白のとき
        return "&nbsp;";
    }
    return nl2br($rtn);
}

/**
 * Smarty td_nl2br modifier plugin
 *
 * Type:     modifier<br>
 * Name:     td_nl2br<br>
 * Purpose:  TD用nl2br。null、空文字のとき”&nbsp;”を戻す。それ以外はnl2brして戻す。
 * @param string
 * @return string
 */
function smarty_modifier_td_nl2br($string)
{
    return is_null($string) || $string == '' ? "&nbsp;":nl2br($string);
}
?>

■logininfoDetailView.html 修正
 phpsmartyへ置き換えた。自作「テンプレート関数プラグイン」を使用した。

<<変更前>>
<?=esc($ELEMENTS['LOGIN_TYPE'][$o['login_type']][$o['c_elements_idx']]) ?>
<<変更後>>
{{get_element elename='LOGIN_TYPE' 
value=$o.login_type|smarty:nodefaults idx=$o.c_elements_idx tdKbn='TD'}}

5/15 6:00-8:00 20:00-20-30 22:00-3:30
1.「テンプレート関数プラグイン」は自分でエスケープする必要がある。これに「テンプレート関数プラグイン」の「smarty_function_escape_special_chars」を使用した。

2.テーブルのTDのところに空文字を入れる罫線が壊れることがある。ので”TD”指定のときは空白は"&nbsp;"に置き換える。以下の2function

 ・smarty_function_get_element
 ・smarty_modifier_td_nl2br(テーブルのTD用に作った。)

3.$smarty->default_modifiers = 'escape:"html"';は各変数ひとつひとつにかかっている。なので、解除するには「{{get_element elename='LOGIN_TYPE' value=$o.login_type|smarty:nodefaults ・・・}}」の「$o.login_type|smarty:nodefaults」のように解除する項目ひとつひとつに対しておこなう。smartyコンパイル結果内容はこんな感じ。解除したので、$this->_tpl_vars['o']['login_type']が'escape:"html"'されていないのがわかる。 <=smartyコンパイル結果ってphpだったんだ。そういえば、昔、そう教えてもらったような気がする。

■%%56^56C^56C5B96A%%logininfoDetailView.html.phpから抜粋
<?php echo smarty_function_get_element(array('elename' => 'LOGIN_TYPE',
'value' => $this->_tpl_vars['o']['login_type'],
'idx' => ((is_array($_tmp=$this->_tpl_vars['o']['c_elements_idx'])) ? 
$this->_run_mod_handler('escape', true, $_tmp, 'html') : 
smarty_modifier_escape($_tmp, 'html')),'tdKbn' => 'TD'), $this);?>