Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
901 views
in Technique[技术] by (71.8m points)

symfony - define custom filesystem path for twig templates

I've already read How does Symfony2 detect where to load templates from? and another ton of articles on the web

The question was quite similar to mine, but the answer wasn't comprehensive.

I need to override a bundle-defined template inside another custom bundle. In other words I want specify another path where symfony have to look before looking in app/Resources when loading templates.

Workflow should be something like this:

first: /src/My/Bundle/Resources/views/example.html.twig (or /src/My/Bundle/OriginalBundle/views/example.html.twig)

then: /app/Resources/OriginalBundle/views/example.html.twig

finally: /src/Original/Bundle/Resources/views/example.html.twig

The standard app/Resources -> src/Original/Bundle isn't enough.

sorry for my poor english and thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's a native feature to do exactly what you want in a nice way. Escentially you can add a twig namespace to the twig configuration in app/config.yml like this:

twig:
    # ...
    paths:
        "%kernel.root_dir%/../vendor/acme/foo-bar/templates": foo_bar

This creates an alias to the folder vendor/acme/foo-bar/templates and then you can use it to render your templates either from the controllers:

return $this->render(
    '@foo_bar/template.html.twig',
    $data
);

or from other twig templates

{% include '@foo_bar/template.html.twig' %}

Source: official cookbook http://symfony.com/doc/current/cookbook/templating/namespaced_paths.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...