承接 fgh151/yii2-littletwig 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fgh151/yii2-littletwig

Composer 安装命令:

composer require fgh151/yii2-littletwig

包简介

This extension allow render twig pages into php layout

README 文档

README

This extension allow render twig pages into php layout

Installation

The preferred way to install this extension is through composer.

Either run

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

or add

"fgh151/yii2-littletwig": "*"

to the require section of your composer.json file.

Usage

First you need add trait to controller:

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

now you can render twig page:

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)

Twig extensions

To add new extension oyu mast create class, for example:

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:

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

instead callback you may write class function:

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 :

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

it will render:

<p>10</p>

Twig options

You may path parameters to twig constructor:

$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

统计信息

  • 总下载量: 6
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-05-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固