2

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)) 
    {
        ...
    }
}

1 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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