Search code examples
symfonyservicedependency-injectiontwig-extension

Error when calling function in twig, Call to a member function render() on null, twig extension


I have a problem using the function as a servis in a twig swatch. I would like to inject some of the template into the page, in part it goes wrong because the page loads but in the place where it should be injected the template pops up error

Error: Call to a member function render() on null

This is a function to be called in the page

public function printCategoriesList() {

    $categoryRepo = $this->doctrine->getRepository('AirblogBundle:Category');
    $categoriestList = $categoryRepo->findAll();

    return $this->environment->render(
                    'AirblogBundle:Template:categoriesList.html.twig', ['categoriesList' => $categoriestList]
    );
}

Below is a link to the rest of the file

Plunker Files

I work on symfony version 2.8


Solution

  • Remove the initRuntime function from TwigExtension and try this:

    public function printCategoriesList(\Twig_Environment $environment) {
    
        $categoryRepo = $this->doctrine->getRepository('AirblogBundle:Category');
        $categoriestList = $categoryRepo->findAll();
    
        return $environment->render(
                        'AirblogBundle:Template:categoriesList.html.twig', ['categoriesList' => $categoriestList]
        );
    }