逆に分かり辛い!?

"multi byte を扱う場合は template に記述しましょう。"を徹底すると、逆に分かり辛い!?


下記が csv.php

<?php

error_reporting(E_ALL);

require_once('class/MySmarty.class.php');

$o_smarty = new MySmarty();

$o_smarty->assign(
	'csv' ,array(
		'a' => array(1 ,2 ,3)
		,'b' => array(10 ,20 ,30)
		,'c' => array(100 ,200 ,300)
	)
);

$output = $o_smarty->fetch('csv.tpl');

$file_name = date('Y_m_d_H_i_s') . '.csv';

$file = fopen($file_name ,'w') or die('file open error.');
flock($file ,LOCK_EX);
fwrite($file ,$output);
flock($file ,LOCK_UN);
fclose($file);

header('Cache-Control: public');
header('Pragma: public');
header('Content-Disposition: inline;filename="' . $file_name . '"');
header('Content-Type: text/octet-stream;name="' . $file_name . '"');
header('Content-Length: ' . filesize($file_name));

$code = mb_internal_encoding();	//現在の内部文字 code を keep
mb_http_output('CP932');	//HTTP 文字コードを CP932 に明示的に設定
readfile($file_name); 		//flie を読み込んで HTTP 出力
mb_internal_encoding($code);	//内部文字 code を元に戻す

unlink($file_name);

下記が MySmarty.class.php

<?php

error_reporting(E_ALL);

require_once('smarty/libs/Smarty.class.php');

class MySmarty extends Smarty{
	public function __construct(){
		$this->Smarty();
		$this->tmplate_dir = 'templates';
		$this->compile_dir = 'templates_c';
		$this->left_delimiter = '{{';
		$this->right_delimiter = '}}';
	}

	public function __destruct(){
		
	}
}

下記が csv.tpl

dataい	dataろ	dataは
{{foreach from=$csv key='key' item='ary'}}
{{strip}}
	{{foreach from=$ary key='ary_key' item='value' name='ary'}}
		{{* 最後は区切り文字は要らない *}}
		{{if $smarty.foreach.ary.last}}
			{{$value}}
		{{else}}
			{{$value|cat:"\t"}}
			{{*	CSV にする場合
			{{$value|cat:","}}
			*}}
		{{/if}}
	{{/foreach}}
{{/strip}}
{{/foreach}}

strip は下記を参照して下さい。
http://sunset.freespace.jp/smarty/SmartyManual_2-6-7J_html/language.function.strip.html


csv.php の出力結果

dataい	dataろ	dataは
1	2	3
10	20	30
100	200	300


参考
ttp://sunset.freespace.jp/smarty/


mimic28号でした。