# 安装
其实,安装Twig有多种方法。
### 安装Twig PHP package
通过Composer安装(推荐)
安装 [Composer](https://getcomposer.org/download/),然后运行以下命令获取最新版Twig:
~~~
composer require twig/twig:~1.0
~~~
### 安装压缩包版
1. 从[下载页面](https://github.com/twigphp/Twig/tags)下载最新的压缩包。
1. 验证压缩包的完整性 [http://fabien.potencier.org/article/73/signing-project-releases](http://fabien.potencier.org/article/73/signing-project-releases)
1. 解压压缩包
1. 将文件移入你的项目内容合适的目录中。
### 安装开发版
~~~
git clone git://github.com/twigphp/Twig.git
~~~
### 安装 PEAR 包
注意:
> 通过 PEAR 安装Twig已被弃用,Twig 1.15.1是发布在 PEAR频道的最新版;使用Composer替代它吧。
~~~
pear channel-discover pear.twig-project.org
pear install twig/Twig
~~~
### 安装C扩展
> Twig 1.4版本加入的新东西。
注意:
> C扩展是**可选的**,它带来了一些很不错的性能提升。记住,扩展程序不是PHP代码的替代品。它只是实现了部分PHP代码,使其在运行时刻(runtime)提升性能。你仍然必须安装原生PHP代码。
Twig的C扩展增强了Twig运行引擎的性能。像安装其他PHP扩展那样安装它:
~~~
cd ext/twig
phpize
./configure
make
make install
~~~
Windows 平台:
1. 参照[PHP文档](https://wiki.php.net/internals/windows/stepbystepbuild)设置构建环境。
1. 将Twig的C扩展源代码放入`C:\php-sdk\phpdev\vcXX\x86\php-source-directory\ext\twig`
1. 使用`configure --disable-all --enable-cli --enable-twig=shared` 命令,替代第十四步。
1. `nmake`
1. 复制`C:\php-sdk\phpdev\vcXX\x86\php-source-directory\Release_TS\php_twig.dll`文件到PHP设置中。
最后,在`php.ini`配置文件中启用扩展。
~~~
extension=twig.so #For Unix systems
extension=php_twig.dll #For Windows systems
~~~
从现在起,Twig将利用C扩展自动编译你的模板。记住,该扩展并不替代PHP代码,但提供了`Twig_Template::getAttribute()`方法的优化版。