Problem/Motivation

When including a svg file inline in your template like this
{% include active_theme_path() ~ '/icons/svg-is-the-awesomesauce.svg' %}

if you happen to delete or missspell (se what i did there) the file name, drupal & twig gets very unhappy and refuses to load the file, that is less than ideal & causes confusion + a broken site.

Proposed resolution

A test to see if the file actually exist:
something along the way of:

{% if active_theme_path() ~ '/icons/svg-is-the-awesomesauce.svg exist % }
  {% include active_theme_path() ~ '/icons/svg-is-the-awesomesauce.svg' %}
{% endif %}

Remaining tasks

... write that code etc ;)

User interface changes

none

API changes

Data model changes

Comments

mortendk created an issue. See original summary.

mortendk’s picture

Title: est if included file exist to prevent template not loading » test if included file exist to prevent template not loading
mortendk’s picture

Rene Bakx’s picture

It's not that hard, first you need to define an new test in Twig

new Twig_SimpleTest('is_readable', function ($file) {
    return file_exists($file);
});
Then you can test for the file in your template

{% set file =  active_theme_path() ~ '/icons/svg-is-the-awesomesauce.svg %}

{% if file is_readable %}
 	{% include file %}
 {% endif %}

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.