12

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.

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

Then on my config.yml on /app/config

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

But error occurs :

"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 ?

2
  • Did you add the MyApp namespace to autoload.php? Commented May 13, 2011 at 13:11
  • @igorw i have 'MyApp' => DIR.'/../src', it's enough ? Commented May 13, 2011 at 13:17

1 Answer 1

6

Replace this

namespace Bundle\WebsiteBundle\Extension;

By this

namespace MyApp\Bundle\WebsiteBundle\Extension;

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

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

Sign up to request clarification or add additional context in comments.

4 Comments

I will try this. But i can't change the structure of directory, it's not a personal project :'(
Ha thx it's working now ! I have lake of knowledge in php namespace I will learn more about it
Hi @azat, where do you put this file -- in Acme/DefaultBundle/Extension ?
Here MyApp/Bundle/WebsiteBundle/Extension

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.