15

Does anyone know of a way to instruct PHPStorm to treat Craft templates as Twig files, without globally assigning all .html files to be interpreted as Twig? Since Craft templates don't use the .twig extension, PHPStorm thinks they're HTML.

On that note, why doesn't Craft use .twig extensions? I assume P&T doesn't want to scare away users who are comfortable with HTML but not Twig?

4
  • 1
    You can use the .twig extension with Craft if you want to. I do, and syntax highlighting (I use Sublime though) is the main reason. Commented Jun 12, 2015 at 17:30
  • I would also assume that any worthwhile twig filter is also going to highlight html properly as well, so I don't see any reason not to interpret all .html files as HTMLTwig files. Besides, I believe you can set it on a project basis (although don't ask me how — but might be worth looking into). Commented Jun 12, 2015 at 17:49
  • I wish you could set it on a per-project basis, but I can't find any way to do that.
    – Garrett
    Commented Jun 15, 2015 at 15:32
  • Haven't tested it, but the docs seem to suggest that you can specify filetypes on a per-project basis. See Project and IDE settings and Creating and registering Filetypes Commented Jun 16, 2015 at 15:47

3 Answers 3

15

Under the PhpStorm preferences, go Editor->File Types->Twig and add *.html as a Registered Pattern.enter image description here

3
  • Yea, I was hoping there was some way to do it w/o globally interpreting .html as Twig. I guess there's no downside, other than it just feels wrong.
    – Garrett
    Commented Jun 15, 2015 at 15:31
  • 2
    The only problem with this is that it re-assigns it from HTML files. So when you use html files again in another project you will have to switch it back. FYI.
    – mattl
    Commented Jul 19, 2015 at 19:21
  • 1
    You can solve this by adding *?html as a filetype. That way, everything that ends with .html will also get syntax highlighting without breaking regular html support.
    – Bert H
    Commented Apr 2, 2019 at 7:04
4

Even easier way… Just add .html.twig to defaultTemplateExtensions and update your template file extension to .html.twig. PHPStorm will pick up both html and twig syntax highlighting.

<?php

/**
 * General Configuration
 *
 * All of your system's general configuration settings go in here.
 * You can see a list of the default settings in craft/app/etc/config/defaults/general.php
 */

return array(
    'defaultTemplateExtensions' => array('html.twig', 'html'),
);
2

Instead of adding *.html as a registered Twig patter, I use *?html. Seems to work just fine and I don't have to switch back and forth.

On the flip side, Twig support in PhpStorm seems to contain all the HTML support as well, so other than feeling wrong (which it does), there may be no downside to just switching it over to Twig.

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.