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

  • Home
  • SEARCH
  • 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 9106085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:15:19+00:00 2026-06-17T02:15:19+00:00

I have a loop in Twig template, which returns multiple values. Most important –

  • 0

I have a loop in Twig template, which returns multiple values. Most important – an ID of my entry. When I didn’t use any framework nor template engine, I used simply file_exists() within the loop. Now, I can’t seem to find a way to do it in Twig.

When I display user’s avatar in header, I use file_exists() in controller, but I do it because I don’t have a loop.

I tried defined in Twig, but it doesn’t help me. Any ideas?

  • 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-17T02:15:20+00:00Added an answer on June 17, 2026 at 2:15 am

    If you want want to check the existence of a file which is not a twig template (so defined can’t work), create a TwigExtension service and add file_exists() function to twig:

    src/AppBundle/Twig/Extension/TwigExtension.php

    <?php
    
    namespace AppBundle\Twig\Extension;
    
    class FileExtension extends \Twig_Extension
    {
    
        /**
         * Return the functions registered as twig extensions
         * 
         * @return array
         */
        public function getFunctions()
        {
            return array(
                new Twig_SimpleFunction('file_exists', 'file_exists'),
            );
        }
    
        public function getName()
        {
            return 'app_file';
        }
    }
    ?>
    

    Register your service:

    src/AppBundle/Resources/config/services.yml

    # ...
    
    parameters:
    
        app.file.twig.extension.class: AppBundle\Twig\Extension\FileExtension
    
    services:
    
        app.file.twig.extension:
            class: %app.file.twig.extension.class%
            tags:
                - { name: twig.extension }
    

    That’s it, now you are able to use file_exists() inside a twig template 😉

    Some template.twig:

    {% if file_exists('/home/sybio/www/website/picture.jpg') %}
        The picture exists !
    {% else %}
        Nope, Chuck testa !
    {% endif %}
    

    EDIT to answer your comment:

    To use file_exists(), you need to specify the absolute path of the file, so you need the web directory absolute path, to do this give access to the webpath in your twig templates
    app/config/config.yml:

    # ...
    
    twig:
        globals:
            web_path: %web_path%
    
    parameters:
        web_path: %kernel.root_dir%/../web
    

    Now you can get the full physical path to the file inside a twig template:

    {# Display: /home/sybio/www/website/web/img/games/3.jpg #}
    {{ web_path~asset('img/games/'~item.getGame.id~'.jpg') }}
    

    So you’ll be able to check if the file exists:

    {% if file_exists(web_path~asset('img/games/'~item.getGame.id~'.jpg')) %}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a loop in razor which generates a template a number of times.
I have a loop, where I need to use the values from 2 IEnumerable's,
I have a loop which decreases the margin-top by 182px every iteration. I want
I have Symfony framework with TWIG template engine. I am making 1000 page pdf
I have a loop spitting out values and are put into a string: $all_values
I have this loop: for (Map.Entry<Integer, String> entry: collection.entrySet()) { } I'd like to
I have a loop which goes through each item a list and adds it
I have a loop which is generating some data and in certain cases I
I have loop in template: {% for item in items %} <div class=item> {{
I have a loop which im trying to make onclicks with their own ID

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.