11

Does anyone know how to make Craft by default use .twig extensions. It helps IDEs better understand Twig blocks. I have tried .twig and .html.twig but it doesn't appear to work by default.

I don't want to set up special configs for every template.

1
  • It works now fine with renaming all files to .twig extension. No ideea why it did not work. Can be closed
    – Cristian
    Commented May 19, 2016 at 10:39

2 Answers 2

12

Craft does already support .twig as a template file extension option by default (see the defaultTemplateExtensions config setting docs). If you want to allow .html.twig as well, just open craft/config/general.php and add this to the config array:

'defaultTemplateExtensions' => array('html', 'twig', 'html.twig'),

(Note that this will only affect front-end templates. The Control Panel is locked to .html and .twig for plugins.)

6

Why do you want to use a .twig extension? The official Twig documentation uses .html, P&T use .html in the Craft codebase and in the Craft documentation. I would say .html is the standard for Craft template files.

Most editors can be configured to load the Twig language / syntax parser for files with other extensions then .twig

Configuration instructions for some popular editors and IDEs:

  • Sublime Text (“Open all with current extension as...” setting)
  • Atom Editor (add extension to text.html.twig language)
  • Visual Studio Code (add extension to files.associations setting)
  • PHPStorm (Preferences > Editor > File Types > Twig; also enable all Twig live templates for HTML in Preferences > Editor > Live Templates)
10
  • 7
    I concur that .html is the default, but I personally prefer .twig. It's more clear; the template files aren't actually HTML files, they are Twig files. Also, using .twig means most IDEs will use Twig syntax highlighting by default (as long as it is installed), without any additional effort. Also, since Craft doesn't require the file extension in {% include %} tags etc., the two are totally interchangeable; there are no drawbacks to using .twig, in my opinion. Commented May 19, 2016 at 12:05
  • 1
    I second Mats opinion. You can also leave off file extensions when specifying templates to use for singles/entries in the control panel - and they will work whether they are HTML or twig filetypes. Commented May 19, 2016 at 13:15
  • 2
    Hey guys, I didn't say it's bad practice to use .twig nor that there are disadvantages. I just wanted to make the point that it's easy to set up in your editors and because SensioLabs and P&T use .html it could be regarded as the standard that I personally like to conform to.
    – carlcs
    Commented May 19, 2016 at 13:33
  • 3
    @carlcs We use .twig in Commerce though, just to mess with you :) Commented May 19, 2016 at 14:10
  • 1
    Ok, so the work around I've found for this is in WebStorm/PHPStorm is to go into Preferences > Editor > Live Templates and then set each individual Twig live template to be applicable in HTML. Then In Preferences > Languages & Frameworks > Template Data Languages set your directories containing the template files to HTML in the dropdown on the right( It doesn't show up for me until I clicked in the column). This has solved the issue of no HTML syntax highlighting inside of Twig tags, allows me to keep .html as the files extension, and use the live templates.
    – Bonk
    Commented Apr 3, 2017 at 15:11

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.