1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

twigで、改行コードを`<br />`に変換するnl2brで、htmlエンティティさせない

Posted at

概要

FuelPHPでtwigを活用するケースが多いです。

そして、改行コードを含むテキストをtwigに表示させる際には、twigのnl2brというフィルターを使う方も多いとおもいます。

ところが、nl2brをそのまま使おうとすると、htmlエンティティが自動で適用されてしまうので、それを外してしまおう、というお話です。

完全に、備忘録レベルです・・・。

手順

FuelPHPを利用されている場合は、fuel/app/classes/vendor/ディレクトリにtwigをインストールされている方も多いとおもいますので、その前提で。

fuel/app/classes/vendor/twig/extension.phpに、以下を追記しましょう。

```
public function getFilters()
{
	return array(
		new Twig_SimpleFilter('nl2br', 'nl2br'),  // この1行を追記
	);
}

これで、快適なnl2brライフが送れます。
もちろん、必要に応じてescapeなどのフィルターも通すようにしましょう。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?