Symfony2 Twig 扩展 [英] Symfony2 Twig extension

查看:36
本文介绍了Symfony2 Twig 扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在实现树枝扩展时遇到了一些问题.我需要创建自己的过滤器(一个 substr 过滤器).所以我想使用twig扩展.

I have some problem to implement twig extension. I need to create my own filter (a substr filter). So I have thought to use twig extension.

我在 MyApp\Bundle\WebsiteBundle\ 和文件中创建了一个名为Extension"的文件夹:网站扩展.php

I created a folder named "Extension" on MyApp\Bundle\WebsiteBundle\ and file : WebsiteExtension.php

namespace Bundle\WebsiteBundle\Extension;

class WebsiteExtension extends \Twig_Extension {

    public function getFilters() {
        return array(
            'substr'  => new \Twig_Filter_Method($this, 'substrFilter'),
        );
    }

    public function substrFilter($sentence, $start, $end) {
        return substr($sentence, $start, $end);
    }

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

然后在我的 config.yml 上/app/config

Then on my config.yml on /app/config

services:
    Website.twig.extension:
        class: MyApp\Bundle\WebsiteBundle\Extension\WebsiteExtension
        tags:
            - { name: twig.extension }

但是出现错误:

"Fatal error: Class 'MyApp\Bundle\WebsiteBundle\Extension\WebsiteExtension' not found in .../appDevDebugProjectContainer.php on line 1391"

这很奇怪,因为文件和类存在......我错过了一些东西但是什么?

It's strange because file and class exists ... I have missed something but what ?

推荐答案

替换这个

namespace Bundle\WebsiteBundle\Extension;

这样

namespace MyApp\Bundle\WebsiteBundle\Extension;

你需要什么目录Bundle/WebsiteBundle?
Bundle 还不够吗?

And for what your need dir Bundle/WebsiteBundle?
Is Bundle is not enough?

你可以在这里看到我是如何实现的https://github.com/azat/blog/blob/master/src/Blog/WebBundle/Twig/Extension/WebExtension.php

Your can see here how I implement this https://github.com/azat/blog/blob/master/src/Blog/WebBundle/Twig/Extension/WebExtension.php

这篇关于Symfony2 Twig 扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆