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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:10:55+00:00 2026-06-12T10:10:55+00:00

In Symfony2, when trying to render index.mobile.twig , How would I load index.html.twig when

  • 0

In Symfony2, when trying to render index.mobile.twig, How would I load index.html.twig when index.mobile.twig doesn’t exist. instead go getting a “this template does not exist error”.

I believe the answer lies somewhere with creating a custom loader for Twig but I couldn’t find any documentation on it, just occasional references to it.

  • 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-12T10:10:56+00:00Added an answer on June 12, 2026 at 10:10 am

    The answer involves extending the twig.loader.class and creating an additional step once it’s discovered that the specific twig template doesn’t exist and before the Exception is thrown.

    PART 1 – Configuration

    The answer involves extending the twig.loader.class.

    1. Create a customloader.php which extends the Twig FilesystemLoader file.

      namespace Acme\MyBundle\Twig\Loader;       
      use Symfony\Bundle\TwigBundle\Loader\FilesystemLoader;
      class CustomLoader extends FilesystemLoader{}
      
    2. Modify your config.yml to include:

     parameters: 
         twig.loader.class: Acme\YourBundle\Twig\Loader\name_of_your_class
    

    PART 2 – Extending the class

    Extending the class is fairly straight forward.. The method I needed to modify was findTemplate($template). It’s very small method and you could probably use parent:: to extend the original code more properly.. However, I chose to completely over-write it and include the following code right before the Unable to find template error:

        if (false === $file || null === $file) {
    
            switch( $template->get('format')){
                case 'mobile':
                        $template->set('format','html');
                        return self::findTemplate($template->getLogicalName());
                    break;
            }
    
            throw new \Twig_Error_Loader(sprintf('Unable to find template "%s".', $logicalName), -1, null, $previous);
    

    After the code realizes that the file exists, it checks to see if mobile was the requested format. IF it is, it sets the format of the template object to html and then tries the name.html.twig. There’s a lot more you could do in this area but this solved my problem.

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

Sidebar

Related Questions

In Symfony2 TWIG, when trying to generate a path like this: <a href={{ path('folder_new',
I am trying to alter the date_widget using symfony2 and twig. I would like
I'm learning Symfony2. Currently, I'm trying to render a form label in a twig
Trying to customize Symfony2 form to produce html code looking like the following example:
I am trying to alter the date_widget using symfony2 and twig. By default a
I'm trying to build a form using symfony2, but I keep getting the error
I'm trying to use Twig (inside Symfony2) to debug a variable that contains a
I am trying to install symfony2 JMS DI Extra bundle. But i keep getting
I'm trying to implement Symfony2 form builder component as a standalone. The documentation doesn't
I'm trying to install JMSSerializerBundle on Symfony2. I followed the Composer instructions here .

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.