2017 © Pedro Peláez
 

yii2-extension yii2-littletwig

This extension allow render twig pages into php layout

image

fgh151/yii2-littletwig

This extension allow render twig pages into php layout

  • Wednesday, May 25, 2016
  • by fgh151
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Little Twig

This extension allow render twig pages into php layout, (*1)

Installation

The preferred way to install this extension is through composer., (*2)

Either run, (*3)

php composer.phar require --prefer-dist fgh151/yii2-littletwig "*"

or add, (*4)

"fgh151/yii2-littletwig": "*"

to the require section of your composer.json file., (*5)

Usage

First you need add trait to controller:, (*6)

class TestController extends Controller
{
    use fgh151\littletwig\TwigTrait
    ...
}

now you can render twig page:, (*7)

public function actionIndex()
{
    return $this->renderTwig('/web/test.twig', ['users' => User::find()->limit(10)->all()], ['Project_Twig_Extension']);
}

Available params: twig page, array of variables array of twig extensions classes (optional), (*8)

Twig extensions

To add new extension oyu mast create class, for example:, (*9)

class Project_Twig_Extension extends \Twig_Extension
{
    public  function  getFunctions()
    {
        return [
            new \Twig_SimpleFunction('TestFunction', function ($p, $p1){ 
                return $p + $p1;
            })
        ];
    }

    public function getName()
    {
        return 'project';
    }
}

in render function add class:, (*10)

return $this->renderTwig('/web/test.twig', [], ['Project_Twig_Extension']);

instead callback you may write class function:, (*11)

class Project_Twig_Extension extends \Twig_Extension
{
    public  function  getFunctions()
    {
        return [
            new \Twig_SimpleFunction('TestFunction', $this->f())
        ];
    }

    public function f($p, $p1)
    {
        return $p + $p1;
    }

    public function getName()
    {
        return 'project';
    }
}

Now in twig template available function TestFunction :, (*12)

<p>{{ TestFunction(6, 4) }}</p>

it will render:, (*13)

<p>10</p>

Twig options

You may path parameters to twig constructor:, (*14)

$params = [
    'debug' => false,
    'charset' => 'UTF-8',
    'base_template_class' => 'Twig_Template',
    'strict_variables' => false,
    'autoescape' => 'html',
    'cache' => false,
    'auto_reload' => null,
    'optimizations' => -1,
];

return $this->renderTwig('/web/test.twig', [], ['Project_Twig_Extension'], $params);

All params are optional. For params detail see twig documentation, (*15)

The Versions

25/05 2016

dev-master

9999999-dev http://openitstudio.ru

This extension allow render twig pages into php layout

  Sources   Download

MIT

The Requires

 

extension yii2

25/05 2016

v1.0.0

1.0.0.0 http://openitstudio.ru

This extension allow render twig pages into php layout

  Sources   Download

MIT

The Requires

 

extension yii2