Symfony Twig extension render view

shobekhan

My twig extension code is as follows

/**
 * @return array
 */
public function getFunctions()
{
    return [
        new \Twig_SimpleFunction('bsPanelTitle', array($this, 'bsPanelTitle')),
    ];
}


/**
 * @param $headline
 * @return string
 */
public function bsPanelTitle($headline)
{
    $loader = new \Twig_Loader_Filesystem(__DIR__ . '/../Resources/views/Common/Placeholder');
    $twig = new \Twig_Environment($loader);
    return $twig->render('xtitle.html.twig', ['headline' => $headline]);
}

My question is:

Is there a better way to reach AppBundle/Resources/views/Common/Placeholder folder from a Twig extension function?

Alexandru Cosoi

Since this question is tagged with symfony2, I'm assuming you are indeed talking about symfony2 project, and if that is the case you can registering your own namespaces with twig, using the twig config, something like this:

twig:
    # ...
    paths:
        "%kernel.root_dir%/../pathFromProjectRootToPlaceholder/Placeholder": placeholder

This creates an alias to the folder pathFromProjectRootToPlaceholder/Placeholder and then you can use it to render your templates like this:

//from controller
return $this->render(
    '@placeholder/index.html.twig',
    $data
);

OR

{# from twig template #}
{% include '@placeholder/index.html.twig' %}

You can add multiple paths to the same alias, but for more information check the official cookbook on symfony 2 site: http://symfony.com/doc/current/cookbook/templating/namespaced_paths.html

Hope this helps,

Alexandru Cosoi

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Register Twig Extension in Symfony

From Dev

symfony twig render dynamic twig code

From Dev

symfony twig render dynamic twig code

From Dev

Twig string render and Symfony extensions

From Dev

Symfony security and twig Render funcion

From Dev

Symfony security and twig Render funcion

From Dev

symfony 3 render controller in twig

From Dev

Symfony dependency injection in twig extension

From Dev

Activate StringLoader Twig Extension in Symfony

From Dev

How to render a view inside of other twig + Modal Dialog Symfony2

From Dev

Symfony 2 render controller in twig and assign it to variable

From Dev

Symfony 2.7 Render image in twig with VichUploaderBundle

From Dev

Symfony2 Twig extension: Creating form

From Dev

Include assets in Twig Extension with Symfony2

From Dev

Multiple Twig_Extension in Symfony2

From Dev

getting image extension in Twig Symfony2

From Dev

Twig Extension doesn't work with Symfony 2

From Dev

Symfony2 creating twig extension

From Dev

Symfony2 creating twig extension

From Dev

symfony twig extension get config variable

From Dev

Twig Extension doesn't work with Symfony 2

From Dev

Symfony2 - Twig extension does not exist in Twig file

From Dev

Symfony2 - Twig extension does not exist in Twig file

From Dev

Drupal 8: render view in twig template

From Dev

Detect Symfony's internal subrequests in a twig view

From Dev

Symfony and Twig, prerender, parse view, replace content

From Dev

How can I render a controller action within a twig extension?

From Dev

How can I render a controller action within a twig extension?

From Dev

php twig get variables passed to render in extension function

Related Related

HotTag

Archive