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:


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)) 

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:


And your existing code would cover both cases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.