Introduction

Welcome to the documentation for Twig, the flexible, fast, and secure template engine for PHP.

Twig のドキュメントへようこそ。これは、PHP 用の柔軟で高速かつ安全なテンプレート エンジンです。

Twig is both designer and developer friendly by sticking to PHP's principles and adding functionality useful for templating environments.

Twig は、PHP の原則に固執し、環境のテンプレート化に役立つ機能を追加することで、デザイナーと開発者の両方にとって使いやすくなっています。

The key-features are...

主な機能は...
  • Fast: Twig compiles templates down to plain optimized PHP code. The overhead compared to regular PHP code was reduced to the very minimum.
    高速: Twig はテンプレートを最適化された単純な PHP コードにコンパイルします。通常の PHP コードと比較したオーバーヘッドは最小限に抑えられました。
  • Secure: Twig has a sandbox mode to evaluate untrusted template code. This allows Twig to be used as a template language for applications where users may modify the template design.
    セキュア: Twig には、信頼されていないテンプレート コードを評価するためのサンドボックス モードがあります。これにより、ユーザーがテンプレートのデザインを変更できるアプリケーションのテンプレート言語として Twig を使用できます。
  • Flexible: Twig is powered by a flexible lexer and parser. This allows the developer to define their own custom tags and filters, and to create their own DSL.
    柔軟: Twig は、柔軟なレクサーとパーサーによって強化されています。これにより、開発者は独自のカスタム タグとフィルターを定義し、独自の DSL を作成できます。

Twig is used by many Open-Source projects like Symfony, Drupal8, eZPublish, phpBB, Matomo, OroCRM; and many frameworks have support for it as well like Slim, Yii, Laravel, and Codeigniter — just to name a few.

Twig は、Symfony、Drupal8、eZPublish、phpBB、Matomo、OroCRM などの多くのオープンソース プロジェクトで使用されています。また、Slim、Yii、Laravel、Codeigniter など、多くのフレームワークがそれをサポートしています。

Screencast

スクリーンキャスト

Like to learn from video tutorials? Check out the SymfonyCasts Twig Tutorial!

ビデオチュートリアルから学びたいですか? SymfonyCasts Twig チュートリアルをチェックしてください!

Prerequisites

Twig 3.x needs at least PHP 7.2.5 to run.

Twig 3.x を実行するには、少なくとも PHP 7.2.5 が必要です。

Installation

The recommended way to install Twig is via Composer:

Twig をインストールする推奨される方法は、Composer を使用することです。
1
composer require "twig/twig:^3.0"

Basic API Usage

This section gives you a brief introduction to the PHP API for Twig:

このセクションでは、Twig 用の PHP API を簡単に紹介します。
1
2
3
4
5
6
7
8
require_once '/path/to/vendor/autoload.php';

$loader = new \Twig\Loader\ArrayLoader([
    'index' => 'Hello {{ name }}!',
]);
$twig = new \Twig\Environment($loader);

echo $twig->render('index', ['name' => 'Fabien']);

Twig uses a loader (\Twig\Loader\ArrayLoader) to locate templates, and an environment (\Twig\Environment) to store its configuration.

Twig はローダー (\Twig\Loader\ArrayLoader) を使用してテンプレートを検索し、環境 (\Twig\Environment) を使用して構成を保存します。

The render() method loads the template passed as a first argument and renders it with the variables passed as a second argument.

render() メソッドは、最初の引数として渡されたテンプレートをロードし、2 番目の引数として渡された変数でそれをレンダリングします。

As templates are generally stored on the filesystem, Twig also comes with a filesystem loader:

テンプレートは通常ファイルシステムに保存されるため、Twig にはファイルシステムローダーも付属しています。
1
2
3
4
5
6
$loader = new \Twig\Loader\FilesystemLoader('/path/to/templates');
$twig = new \Twig\Environment($loader, [
    'cache' => '/path/to/compilation_cache',
]);

echo $twig->render('index.html', ['name' => 'Fabien']);