noldor's diary

調べたことのメモとよしなしごと

viewのエスケープ方法を調べてみた(OutputEscaperとSmarty3)

追いかけてなかったので知らなかったけど、symfony2ではOutputEscaperは廃止していた。twigでやるということらしい。

Summary
-------
The output escaping component for PHP templates does not work very well
and I think it cannot be "fixed". So, I want to remove its support in
Symfony2 entirely. This means that we won't have automatic output
escaping if you use the PHP templating engine in Symfony2.
I think that makes sense because we have decided to use Twig as the
default templating system (and Twig supports a much more robust
implementation of automatic output escaping -- still not finished yet
though.)

http://groups.google.com/group/symfony-devs/browse_thread/thread/925d40f1cff7fe11?pli=1

捨てるには惜しかったのか、切り出して使えるようにしたものがgithubに残っている。

https://github.com/fabpot/output-escaper


Smartyは3からホワイトリスト式のエスケープが可能になっていた。variableフィルタというのがあった。

type にはフィルタの型を定義します。使える値は "pre"、"post"、"output" および "variable" です。

http://www.smarty.net/docs/ja/api.register.filter.tpl

ところがマニュアルにはプリフィルタ、ポストフィルタ、アウトプットフィルタのことしか書いてなくてvariableフィルタのことが見つからなかった。フォーラムを読んで何とかわかる程度。

http://www.smarty.net/forums/viewtopic.php?p=62862

普通に使うとvariableフィルタによって変換され、変換したくない場合はnofilterを使えばよい。

{$record.CONTACT_NAME nofilter}

現段階の最新版である3.1.7試してみたところ、フィルタの登録には$thisが使えなかった。不具合のようで、Smartyクラスを継承したmy_smartyクラスのメソッドescape_filterをコンストラクタで登録すると

$this->registerFilter("variable", array($this, 'escape_filter')); 

なぜかmy_smarty_escape_filter()を呼び出そうとしてfatalになった。

staticメソッドとして登録することで意図通りに動いた。

$this->registerFilter("variable", array(__CLASS__, 'escape_filter'));