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