Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9256151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:52:27+00:00 2026-06-18T11:52:27+00:00

Is there a way with Symfony2 and Twig to always make files available which

  • 0

Is there a way with Symfony2 and Twig to always make files available which just contain blocks.

For example, if I wanted to make a block named ‘cookie’ always available in any template in my system, without having to always include or extend it in the template I’m using.

Any ideas?


Clarification

Say I have a generic block which can do something, like:

{% block myBlock %}
    ABC Examples
{% endblock %}

I have a class which knows it wants to be rendered with this block. My template itself doesn’t necessarily know this though.

 {{ block(myObj.blockName) }}

I would then like to have it so my controller/services/etc. could register the file which contains that block, without my template actually needing to know about it directly (so I could have multiple files like that, each working with some common interface).

Similar to registering custom Twig functions with a TwigExtension. My template doesn’t need to explicitly know it’s there, it just has to be available at run-time.

Does that make sense?


To clarify a bit further, I’m essentially looking to do something just like how there are default Twig blocks for rendering Forms in Symfony2. I don’t have to include the default form file every time, just when I want to change something.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:52:28+00:00Added an answer on June 18, 2026 at 11:52 am

    I went digging around in the Symfony source code to try and find my answer. It looks like there isn’t some fancy, neat way to embed it from a configuration file or controller directly, which is a bit disappointing, but not a huge deal.

    So, to solve my situation, I’ll be using the “use” keyword to include my block file in my base template, so it will then be available to everything else.

    {# widget_blocks.html.twig #}
    {# Widgets #}
    {% block my_widget %}
        ABC Cookies
    {% endblock %}
    
    {# base.html.twig #}
    {% use widget_blocks.html.twig %}
    {{ block(my_widget.block) }}
    

    Not exactly what I wanted, but sufficiently close.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way of demanding through annotations in Symfony2 or doctrine2: if you
Is there a way in Symfony2 to customise field names on Type classes ?
Is there any way to resize pictures while uploading them in Symfony2 ? The
I am using Symfony2.0 and I wonder if there is a way to record
I'm working on a Symfony2 project which uses Twig, and the filetypes are myfile.html.twig
Is there any way to implement a MongoDB/Document/User based authentication on Symfony2 ? Currently
Is there a way to manually specify transactions in symfony2 with the entity manager
Is there a simple way in Symfony 1.4 to know whether a submitted form
I'm using symfony 1.4.3 Is there some way to render a sfWidgetFormChoice as an
Is there way to get file from windows xp command prompt? I tried to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.