yiiext/twig-renderer Twig renderer for the Yii Framework.

extensionrenderertwig

Twig view renderer

This extension allows you to use Twig templates in Yii.

Resources

Requirements

  • Yii 1.0 or above

Installation

Installing manually

  • Extract the release file under vendor/Twig.
  • Download and extract all Twig files from fabpot-Twig-______.zip\fabpot-Twig-______\lib\Twig\ under protected/vendor/Twig.
  • Add the following to your config file 'components' section:
<?php

  'viewRenderer' => array(
      'class' => 'ext.ETwigViewRenderer',

      // All parameters below are optional, change them to your needs
      'fileExtension' => '.twig',
      'options' => array(
          'autoescape' => true,
      ),
      'extensions' => array(
          'My_Twig_Extension',
      ),
      'globals' => array(
          'html' => 'CHtml'
      ),
      'functions' => array(
          'rot13' => 'str_rot13',
      ),
      'filters' => array(
          'jencode' => 'CJSON::encode',
      ),
      // Change template syntax to Smarty-like (not recommended)
      'lexerOptions' => array(
          'tag_comment'  => array('{*', '*}'),
          'tag_block'    => array('{', '}'),
          'tag_variable' => array('{$', '}')
      ),
  ),

Installing via Composer

  • Get Composer
  • Create file protected/composer.json if absent:
{
  "repositories":[
      {
          "type":"package",
          "package":{
              "name":"yiiext/twig-renderer",
              "version":"1.1.15",
              "source":{
                  "type":"git",
                  "url":"https://github.com/yiiext/twig-renderer",
                  "reference":"v1.1.15"
              }
          }
      }
  ],
  "require":{
      "php":">=5.3.0",
      "yiisoft/yii": "dev-master",
      "twig/twig": "1.*",
      "yiiext/twig-renderer":"1.1.*"
  }
}
  • Run composer update in the protected folder of your app.
  • Add the following to your config file 'components' section:
<?php

  'viewRenderer' => array(
      'class' => 'application.vendor.yiiext.twig-renderer.ETwigViewRenderer',
      'twigPathAlias' => 'application.vendor.twig.twig.lib.Twig',

      // All parameters below are optional, change them to your needs
      'fileExtension' => '.twig',
      'options' => array(
          'autoescape' => true,
      ),
      'extensions' => array(
          'My_Twig_Extension',
      ),
      'globals' => array(
          'html' => 'CHtml'
      ),
      'functions' => array(
          'rot13' => 'str_rot13',
      ),
      'filters' => array(
          'jencode' => 'CJSON::encode',
      ),
      // Change template syntax to Smarty-like (not recommended)
      'lexerOptions' => array(
          'tag_comment'  => array('{*', '*}'),
          'tag_block'    => array('{', '}'),
          'tag_variable' => array('{$', '}')
      ),
  ),

Usage

  • See Twig syntax.
  • Current controller properties are accessible via {{this.pageTitle}}.
  • Yii::app() object is accessible via {{App}}.
  • Yii's core static classes (for example, CHtml) are accessible via {{C.ClassNameWithoutFirstC.Method}} (example: {{C.Html.textField(name,'value')}})
  • To call functions or methods which return non-string result wrap these calls with 'void' function: {{void(App.clientScript.registerScriptFile(...))}}

Widgets usage example

<div id="mainmenu">
    {{ this.widget('zii.widgets.CMenu',{
        'items':[
            {'label':'Home', 'url':['/site/index']},
            {'label':'About', 'url':{0:'/site/page', 'view':'about'} },
            {'label':'Contact', 'url':['/site/contact']},
            {'label':'Login', 'url':['/site/login'], 'visible':App.user.isGuest},
            {'label':'Logout ('~App.user.name~')', 'url':['/site/logout'], 'visible':not App.user.isGuest}
        ]
    }, true) }}
</div><!-- mainmenu -->

Changelog

1.1.15

  • Changed default twigPathAlias value to application.vendor.Twig to match Yii application template (samdark)

1.1.14

  • 4: Fixed a little bug with themes (ssidelnikov)

  • Added instructions on installation of the extension via Composer to readme (ssidelnikov)

1.1.3

  • 10: PHP 5.2 compatibility (resurtm)

1.1.2

  • Added global 'void' function to make possible to call functions and methods which return non-string result (Leonid Svyatov)

1.1.1

  • Added global 'C' variable from v0.9.5 for accessing Yii core static classes (Leonid Svyatov)
  • Added new class 'ETwigViewRendererYiiCoreStaticClassesProxy' to provide global 'C' variable feature (Leonid Svyatov)
  • Removed hard-coded global 'Yii' variable from v0.9.5 (it could be done by using 'globals' option) (Leonid Svyatov)
  • Removed unnecessary now classes: 'YiiStatic', 'twigC' and 'twigCObj' (Leonid Svyatov)

1.1.0

  • Added an ability to add Twig globals (objects and static classes) (Leonid Svyatov)
  • Added an ability to add Twig functions and filters (Leonid Svyatov)
  • Added an ability to specify Twig location through path alias (Leonid Svyatov)
  • Added an ability to change templates syntax (Leonid Svyatov)
  • Default template extension changed to '.twig' (Leonid Svyatov)
  • Yii::app() object now can be accessed in templates by name 'App' ({{ App.cache.get('id') }}) (Leonid Svyatov)
  • changelog.txt renamed to changelog.md for better look in GitHub (Leonid Svyatov)
  • README.md link to readme_en.txt removed (Leonid Svyatov)
  • readme_en.txt renamed to README.md (Leonid Svyatov)
  • readme_ru.txt renamed to README_RU.md for better look in GitHub (Leonid Svyatov)
  • Changed all links in READMEs to point at GitHub (Leonid Svyatov)
  • Fixed all markdown in all files for better look in GitHub (Leonid Svyatov)
  • Actualized all links to Twig (Leonid Svyatov)

0.9.5

  • Added C, Yii, app vars (Sapphiriq)
  • Fixed cache = false in twig options (Spartakus)

0.9.4

  • Added an ability to set Twig environment options (Sam Dark)
  • Added an ability to load extensions (Roman, Sam Dark)

0.9.3

  • Fixed renderFile method (AlexandrZ, Sam Dark)
  • Extension is now theme-aware (zadoev, Sam Dark)

0.9.2

  • Changed translation category to 'yiiext'.
  • New naming conventions.

0.9.1

0.9

  • Initial public release (Sam Dark)

Statistics

Downloads
GitHub Stars
GitHub Forks

Releases

No stable releases.

Comments



No stable releases.



BSD-3-Clause license
Stats
70 github stars & 30 github forks
12 downloads in the last day
216 downloads in the last 30 days
78222 total downloads