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?

  • 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. – Mats Mikkel Rummelhoff Jun 12 '15 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). – Douglas McDonald Jun 12 '15 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 Jun 15 '15 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 – Douglas McDonald Jun 16 '15 at 15:47
14

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

|improve this answer|||||
  • 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 Jun 15 '15 at 15:31
  • 1
    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 Jul 19 '15 at 19:21
  • 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 Apr 2 '19 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'),
);
|improve this answer|||||
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.

|improve this answer|||||

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.