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 9165935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:01:42+00:00 2026-06-17T15:01:42+00:00

I have a Twig extension in my Symfony project which renders a twitter widget.

  • 0

I have a Twig extension in my Symfony project which renders a twitter widget. Currently, in my extension, I have this:

public function twitter($handle, $number = 5)
{
    return "<div data-tweet-row=\"&lt;div&gt;&lt;a  href='http://twitter.com/{screen_name}/status/{id}'&gt;{tweet}&lt;/a&gt;&lt;span class='datetime'&gt;{datetime}&lt;/span&gt;&lt;/div&gt;\" data-twitter-header=\"&lt;div class='header'&gt;&lt;img src='https://api.twitter.com/1/users/profile_image?screen_name={screen_name}&amp;size=normal' /&gt;&lt;h4&gt;&lt;a href='http://twitter.com/{screen_name}'&gt;@{screen_name}&lt;/a&gt;&lt;/h4&gt;&lt;/div&gt;\" id=\"twitter\">&nbsp;</div><script>$('#twitter').biff_twitter({screen_name:'$handle',count:$number});</script>";
}

However I really don’t like this solution, I’d rather have the HTML saved under views and then load the file from within my extension.

I have access to the container from within the extension using:

private $container;

public function __construct(ContainerInterface $container){
    $this->container = $container;
}

So I need something like

$view_file = $this->container->get('...')->view('InternalSocialBundle:twitter_placeholder.html.twig')
  • 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-17T15:01:43+00:00Added an answer on June 17, 2026 at 3:01 pm

    Ok, so I resolved the issue that I had but it was sort of my fault for not testing this fully. @Cyprian’s solution does indeed work absolutely fine when you’re not doing anything complicated and just using the extension as intended.

    My problem was that I was using this evaluate extension which was loading a template from the database, therefore it was twig that was doing all of the loading (and therefore was only interpreting the first param of $this->render() as a string – it made no attempt to load it using the symfony naming pattern).

    This means my extension would work fine when using the above method in an actual twig file, but not when I used the {{body|evaluate}} extension.

    The solution to this problem was as follows:

    In my services.yml file, not only did I need service_container, but also twig.loader so it looked something liked:

    class: Test\SocialBundle\Twig\SocialExtension
    arguments: [@service_container,@twig.loader]
    tags:
        -  { name: 'twig.extension' }
    

    I then have this as the twig extensions constructor:

    public function __construct(ContainerInterface $container, \Symfony\Bundle\TwigBundle\Loader\FilesystemLoader $loader)
    {
        $this->container = $container;
        $this->loader = $loader;
    }
    

    In my getFilters() definition:

    public function getFilters()
    {
      return array(
        'twitter' => new \Twig_Filter_Method($this, 'twitter',array(
            'is_safe' => array(
                'evaluate' => true
                ),
            'needs_environment' => true,
            )
        )
      )
    }
    

    and finally, before I returned the rendered output:

    public function twitter(\Twig_Environment $environment, $id)
    {
        $social_item = array(); // get my data here from my service;
        $environment->setLoader($this->loader);
        return $environment->render("TestSocialBundle:Front:twitter_placeholder.html.twig",array());
    }
    

    This ensured that I was always using the \Symfony\Bundle\TwigBundle\Loader\FilesystemLoader loader, and not \Twig_Loader_String which was being used in my case.

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

Sidebar

Related Questions

I have a slugify method in an Twig Extension which i would like to
have written this little class, which generates a UUID every time an object of
I have a twig file and a yml, in which I define the variables
I have a loop in Twig template, which returns multiple values. Most important -
I have been looking at this twig code for a while, and tried a
I have a controller that passes an array to a twig template, which I
As an example, I have a Perl XML::Twig object $xmlDef , which contains the
I have a html.twig file with this {% set num= 1%} {% for idea
I have Symfony framework with TWIG template engine. I am making 1000 page pdf
I have a view template that is currently FooBundle:Bar:baz.html.twig . However, since I have

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.