# TwigView ドキュメント

pieceofcake2/twig-view は、[Twig テンプレート言語](https://twig.symfony.com/) (Twig 3.x) を CakePHP 2 に統合し、
Twig テンプレートからヘルパー、エレメント、テーマを使えるようにします。

> [!NOTE]
> CakePHP 5.x では、代わりに公式の [cakephp/twig-view](https://github.com/cakephp/twig-view) パッケージを使ってください。

- [インストール](#installation)
- [クイックスタート](#quick-start)
- [機能](#features)
- [その他のドキュメント](#further-documentation)
- [例](#examples)
- [由来](#origin)

<a id="installation"></a>

## インストール

[Composer](https://getcomposer.org/) でプラグインをインストールします。

```bash
composer require pieceofcake2/twig-view
```

このプラグインは `cakephp-plugin` タイプなので、`composer/installers` によってプラグインディレクトリにインストールされます。
正しい場所にインストールされるよう、アプリケーションの `composer.json` で `installer-paths` を設定してください。

| レイアウト | `installer-paths` |
| --- | --- |
| モダン (`config/`、`src/`、`templates/`、`plugins/`) | `"plugins/{$name}/": ["type:cakephp-plugin"]` |
| レガシー (`app/Config/`、`app/Plugin/`) | `"app/Plugin/{$name}/": ["type:cakephp-plugin"]` |

`config/bootstrap.php` (レガシーレイアウトでは `app/Config/bootstrap.php`) でプラグインを読み込みます。

```php
CakePlugin::load('TwigView');
// or
CakePlugin::loadAll();
```

<a id="quick-start"></a>

## クイックスタート

`src/Controller/AppController.php` (レガシーレイアウトでは `app/Controller/AppController.php`) で
ビュークラスを設定します。

```php
class AppController extends Controller
{
    public $viewClass = 'TwigView.Twig';
}
```

`.twig` 拡張子でテンプレートを作成します。たとえば `templates/Posts/index.twig`
(レガシーレイアウトでは `app/View/Posts/index.twig`) です。

```twig
{% for post in posts %}
    <h2>{{ post.Post.title }}</h2>
    <p>{{ post.Post.body }}</p>
{% endfor %}
```

<a id="features"></a>

## 機能

- **CakePHP との統合**: ヘルパー、エレメント、テーマをすべて使えます
- **カスタムエクステンション**: カスタムのフィルターや関数で Twig を拡張できます
- **テーマのサポート**: CakePHP のテーマ付きビューと互換性があります
- **プラグインのサポート**: `View/` ディレクトリと `templates/` ディレクトリの両方で動作します
- **プリコンパイル**: 本番環境のパフォーマンスのためにテンプレートをコンパイルできます
- **PSR-4 名前空間**: モダンな PHP のアーキテクチャです

<a id="further-documentation"></a>

## その他のドキュメント

- **[関数とフィルターのリファレンス](https://pieceofcake2.github.io/twig-view/ja/reference.md)** - 使用できる Twig の関数とフィルターの完全な一覧
- **[ヘルパーの使い方](https://pieceofcake2.github.io/twig-view/ja/helpers.md)** - Twig テンプレートで CakePHP のヘルパーを使う方法
- **[設定](https://pieceofcake2.github.io/twig-view/ja/configuration.md)** - 高度な設定オプション
- **[エクステンション](https://pieceofcake2.github.io/twig-view/ja/extensions.md)** - よく使われる Twig エクステンション

<a id="examples"></a>

## 例

Twig で書いたレイアウトの例:

- [`Layouts/default.twig`](https://github.com/pieceofcake2/twig-view/blob/2.x/docs/examples/Layouts/default.twig)
- [`Layouts/ajax.twig`](https://github.com/pieceofcake2/twig-view/blob/2.x/docs/examples/Layouts/ajax.twig)
- [`Layouts/flash.twig`](https://github.com/pieceofcake2/twig-view/blob/2.x/docs/examples/Layouts/flash.twig)
- [`Layouts/js/default.twig`](https://github.com/pieceofcake2/twig-view/blob/2.x/docs/examples/Layouts/js/default.twig)
- [`Layouts/rss/default.twig`](https://github.com/pieceofcake2/twig-view/blob/2.x/docs/examples/Layouts/rss/default.twig)
- [`Layouts/xml/default.twig`](https://github.com/pieceofcake2/twig-view/blob/2.x/docs/examples/Layouts/xml/default.twig)
- [`Layouts/email/html/default.twig`](https://github.com/pieceofcake2/twig-view/blob/2.x/docs/examples/Layouts/email/html/default.twig)
- [`Layouts/email/text/default.twig`](https://github.com/pieceofcake2/twig-view/blob/2.x/docs/examples/Layouts/email/text/default.twig)

<a id="origin"></a>

## 由来

このパッケージは [predominant/TwigView](https://github.com/predominant/TwigView)
(Packagist では公開されておらず、アップストリームの最終バージョンは v0.6) のメンテナンスされたフォークで、
元の作者は Kjell Bublitz と Graham Weldon です。[pieceofcake2](https://github.com/pieceofcake2) が、
モダンな PHP で動く CakePHP 2 向けにメンテナンスしています。オリジナルからの変更点は
[CHANGELOG.md](https://github.com/pieceofcake2/twig-view/blob/2.x/CHANGELOG.md) で確認できます。

predominant/TwigView 自体は、Kjell Bublitz によるオリジナルの実装である
[m3nt0r-legacy/cakephp-twig-view](https://github.com/m3nt0r-legacy/cakephp-twig-view) をもとにしています。
