PHP code example of fgh151 / yii2-littletwig

1. Go to this page and download the library: Download fgh151/yii2-littletwig library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fgh151 / yii2-littletwig example snippets


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

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

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';
    }
}

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

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';
    }
}

$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);

php composer.phar