多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# Introduction This is the documentation for Twig, the flexible, fast, and secure templateengine for PHP. If you have any exposure to other text-based template languages, such asSmarty, Django, or Jinja, you should feel right at home with Twig. It's bothdesigner and developer friendly by sticking to PHP's principles and addingfunctionality useful for templating environments. The key-features are... - *Fast*: Twig compiles templates down to plain optimized PHP code. Theoverhead compared to regular PHP code was reduced to the very minimum. - *Secure*: Twig has a sandbox mode to evaluate untrusted template code. Thisallows Twig to be used as a template language for applications where usersmay modify the template design. - *Flexible*: Twig is powered by a flexible lexer and parser. This allows thedeveloper to define its own custom tags and filters, and create its own DSL. Twig is used by many Open-Source projects like Symfony, Drupal8, eZPublish,phpBB, Piwik, OroCRM, and many frameworks have support for it as well likeSlim, Yii, Laravel, Codeigniter, and Kohana, just to name a few. ### Prerequisites Twig needs at least **PHP 5.2.7** to run. ### Installation The recommended way to install Twig is via Composer: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1</pre></div></td><td class="code"><div class="highlight"><pre>composer require "twig/twig:~1.0" </pre></div></td></tr></table> Note To learn more about the other installation methods, read the[*installation*](#) chapter; it also explains how to installthe Twig C extension. ### Basic API Usage This section gives you a brief introduction to the PHP API for Twig. <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2 3 4 5 6 7 8</pre></div></td><td class="code"><div class="highlight"><pre>require_once '/path/to/vendor/autoload.php'; $loader = new Twig_Loader_Array(array( 'index' =&gt; 'Hello {{ name }}!', )); $twig = new Twig_Environment($loader); echo $twig-&gt;render('index', array('name' =&gt; 'Fabien')); </pre></div></td></tr></table> Twig uses a loader (`Twig_Loader_Array`) to locate templates, and anenvironment (`Twig_Environment`) to store the configuration. The `render()` method loads the template passed as a first argument andrenders it with the variables passed as a second argument. As templates are generally stored on the filesystem, Twig also comes with afilesystem loader: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2 3 4 5 6</pre></div></td><td class="code"><div class="highlight"><pre>$loader = new Twig_Loader_Filesystem('/path/to/templates'); $twig = new Twig_Environment($loader, array( 'cache' =&gt; '/path/to/compilation_cache', )); echo $twig-&gt;render('index.html', array('name' =&gt; 'Fabien')); </pre></div></td></tr></table> Tip If you are not using Composer, use the Twig built-in autoloader: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2</pre></div></td><td class="code"><div class="highlight"><pre>require_once '/path/to/lib/Twig/Autoloader.php'; Twig_Autoloader::register(); </pre></div></td></tr></table>