Answers.Body: <p>That's exactly how I'd do it.</p>
  
  <p>It doesn't make much sense, but there's nothing stopping a user from having both extensions for a template like:</p>
  
  <pre><code>/index.html
  /index.twig
  </code></pre>
  
  <p>And your existing code would cover both cases.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-08-28T16:51:24.503Z
Answers.LastActivityDate: 2014-08-28T16:51:24.503Z
Answers.UserDisplayName: Brad Bell
Body: <p>Craft allows users to use whatever template extension they prefer: <code>.html</code> or <code>.twig</code> or something custom.</p>
  
  <p>We can check if a file exists like so: </p>
  
  <pre><code>IOHelper::fileExists($filePath)
  </code></pre>
  
  <p>Is there any way to know which extension we want to be testing for?  By default Craft supports both <code>html</code> and <code>twig</code> in the supported extensions config setting.</p>
  
  <p>Right now, I have the following which feels a bit clunky:</p>
  
  <pre><code>foreach (craft()-&gt;config-&gt;get('defaultTemplateExtensions') as $extension) 
  {
      if (IOHelper::fileExists($template . "." . $extension)) 
      {
          ...
      }
  }
  </code></pre>
  
Tags: <plugin-development><filesystem>
Title: What is the best way to test if a file exists without knowing which file extension a Craft install is using for templates?
ThreadText: # What is the best way to test if a file exists without knowing which file extension a Craft install is using for templates?
  
  (Asked by: username_0 on 2014-08-28)
  
  Craft allows users to use whatever template extension they prefer: `.html` or `.twig` or something custom.
  
  We can check if a file exists like so:
  
  ```
  IOHelper::fileExists($filePath)
  
  ```
  
  Is there any way to know which extension we want to be testing for? By default Craft supports both `html` and `twig` in the supported extensions config setting.
  
  Right now, I have the following which feels a bit clunky:
  
  ```
  foreach (craft()->config->get('defaultTemplateExtensions') as $extension) 
  {
      if (IOHelper::fileExists($template . "." . $extension)) 
      {
          ...
      }
  }
  
  ```
  
  **Tags:** plugin-development, filesystem
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-08-28. Score: 2 | Accepted answer
  
  That's exactly how I'd do it.
  
  It doesn't make much sense, but there's nothing stopping a user from having both extensions for a template like:
  
  ```
  /index.html
  /index.twig
  
  ```
  
  And your existing code would cover both cases.
  
  
  ---
  
  