text 将Twig扩展添加到Slim Framework V3

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 将Twig扩展添加到Slim Framework V3相关的知识,希望对你有一定的参考价值。

<?php

namespace App\TwigExtension;

class IsLoggedIn extends \Slim\Views\TwigExtension
{
    public function __construct()
    {
    }

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

    public function getFunctions()
    {
        return [
            new \Twig_SimpleFunction('isLoggedIn', array($this, 'isLoggedIn'))
        ];
    }

    public function isLoggedIn ()
    {
        return \App\Helper\Acl::isLoggedIn();
    }
}



-------------------------------------------------------------------------------------------

Then in dependencies.php add the IsLoggedIn() extension to the Twig setup section: 

$container['view'] = function ($c) {
    $settings = $c->get ('settings');
    $view = new \Slim\Views\Twig ($settings['view']['template_path'], $settings['view']['twig']);

    // Add extensions
    $view->addExtension (new Slim\Views\TwigExtension($c->get ('router'), $c->get ('request')->getUri ()));
    $view->addExtension (new Twig_Extension_Debug());
    $view->addExtension (new App\TwigExtension\IsLoggedIn());
    $view['settings'] = $settings; // Do we need this?

    return $view;
};

以上是关于text 将Twig扩展添加到Slim Framework V3的主要内容,如果未能解决你的问题,请参考以下文章

将文本附加到 Twig 中的变量

将产品添加到购物车 jQuery/Ajax/Symfony/Twig

Symfony twig 如何将类添加到表单行

Twig 检查文件是不是存在

使用 Twig 生成纯 JSON 响应是不是合适?

Slim 文档-First Application 翻译