[phpStorm] phpBB 3.1 Twig Syntax

Hello,

I was wondering how I can get phpStorm to recognise TWIG syntax in a file with just the .HTML extension.  I ask this, because phpBB 3.1 has silently integrated Symfony and Twig into their wonderful Bulletin Board System.  This eliminates the need to use their proprietary html comment style template engine ( https://area51.phpbb.com/phpBB/viewtopic.php?f=108&t=46445&hilit=twig ).

So this:
<!-- IF S_ENABLE_FEEDS -->
   <!-- IF S_ENABLE_FEEDS_OVERALL --><link rel="alternate" type="application/atom+xml" title="{L_FEED} - {SITENAME}" href="{U_FEED}" /><!-- ENDIF -->
   <!-- IF S_ENABLE_FEEDS_NEWS --><link rel="alternate" type="application/atom+xml" title="{L_FEED} - {L_FEED_NEWS}" href="{U_FEED}?mode=news" /><!-- ENDIF -->
   <!-- IF S_ENABLE_FEEDS_FORUMS --><link rel="alternate" type="application/atom+xml" title="{L_FEED} - {L_ALL_FORUMS}" href="{U_FEED}?mode=forums" /><!-- ENDIF -->
   <!-- IF S_ENABLE_FEEDS_TOPICS --><link rel="alternate" type="application/atom+xml" title="{L_FEED} - {L_FEED_TOPICS_NEW}" href="{U_FEED}?mode=topics" /><!-- ENDIF -->
   <!-- IF S_ENABLE_FEEDS_TOPICS_ACTIVE --><link rel="alternate" type="application/atom+xml" title="{L_FEED} - {L_FEED_TOPICS_ACTIVE}" href="{U_FEED}?mode=topics_active" /><!-- ENDIF -->
   <!-- IF S_ENABLE_FEEDS_FORUM and S_FORUM_ID --><link rel="alternate" type="application/atom+xml" title="{L_FEED} - {L_FORUM} - {FORUM_NAME}" href="{U_FEED}?f={S_FORUM_ID}" /><!-- ENDIF -->
   <!-- IF S_ENABLE_FEEDS_TOPIC and S_TOPIC_ID --><link rel="alternate" type="application/atom+xml" title="{L_FEED} - {L_TOPIC} - {TOPIC_TITLE}" href="{U_FEED}?f={S_FORUM_ID}&amp;t={S_TOPIC_ID}" /><!-- ENDIF -->
<!-- ENDIF -->


<!-- IF U_CANONICAL -->
   <link rel="canonical" href="{U_CANONICAL}" />
<!-- ENDIF -->


Can now be written like this:
{% if S_ENABLE_FEEDS %}
   {% if S_ENABLE_FEEDS_OVERALL %}<link rel="alternate" type="application/atom+xml" title="{L_FEED} - {SITENAME}" href="{U_FEED}" />{% endif %}
   {% if S_ENABLE_FEEDS_NEWS %}<link rel="alternate" type="application/atom+xml" title="{L_FEED} - {L_FEED_NEWS}" href="{U_FEED}?mode=news" />{% endif %}>
   {% if S_ENABLE_FEEDS_FORUMS %}<link rel="alternate" type="application/atom+xml" title="{L_FEED} - {L_ALL_FORUMS}" href="{U_FEED}?mode=forums" />{% endif %}
   {% if S_ENABLE_FEEDS_TOPICS %}<link rel="alternate" type="application/atom+xml" title="{L_FEED} - {L_FEED_TOPICS_NEW}" href="{U_FEED}?mode=topics" />{% ENDIF -->
   {% if S_ENABLE_FEEDS_TOPICS_ACTIVE %}<link rel="alternate" type="application/atom+xml" title="{L_FEED} - {L_FEED_TOPICS_ACTIVE}" href="{U_FEED}?mode=topics_active" />{% endif %}
   {% if S_ENABLE_FEEDS_FORUM and S_FORUM_ID %}<link rel="alternate" type="application/atom+xml" title="{L_FEED} - {L_FORUM} - {FORUM_NAME}" href="{U_FEED}?f={S_FORUM_ID}" />{% endif %}
   {% if S_ENABLE_FEEDS_TOPIC and S_TOPIC_ID %}<link rel="alternate" type="application/atom+xml" title="{L_FEED} - {L_TOPIC} - {TOPIC_TITLE}" href="{U_FEED}?f={S_FORUM_ID}&amp;t={S_TOPIC_ID}" />{% endif %}
{% endif %}


{% if U_CANONICAL %}
   <link rel="canonical" href="{U_CANONICAL}" />
{% endif %}


However, I can't seem to get my IDE to recognise the TWIG syntax in a normal .HTML file.  I tried going to Settings >> Template Data Languages to assign TWIG to the folder, but it is not an option.  Is there a work around I can use, or a way to add TWIG to the dropdown list for Template data language?

Thanks!

Sven2157

5 comments
Comment actions Permalink

Hi there,

Is it possible to give some unique file extension to such files (e.g. *.html.twig .. or *.twig.html .. or whatever)?

If "yes" -- "Settings (Preferences on Mac) | Editor | File Types | Twig" and add such pattern there.

If "no" (extension has to be "*.html") then only by associating "*.html" (or any other pattern that would cover such files) to the aforementioned Twig file type (at the same time it will be removed from "HTML files" entry).

I tried going to Settings >> Template Data Languages to assign TWIG to the folder, but it is not an option.

That is correct -- only "proper" languages can be assigned there and not templating systems/engines (like Twig/Smarty /etc). Here you can override default "HTML" for Twig files to, lets say, XML or CSS (so that you can use twig to generate .css or .xml files).

0
Comment actions Permalink

Unfortunately, if I change the extension to .html.twig ( or any ccombination, other than plain .html ) it breaks the site.  While the Full Twig system is still usable, the files must remain as .HTML.  On the 'if no': Are you saying I could overide the syntax for .HTML altogether, with the Twig file type, but entirely loose the default HTML file type association?

That seems a bit drastic.  Though, I really only use Blade and Twig, if I use any template engine. So that might be the solution.

On your last point, you are saying there is no way to force the language option, correct?

Thanks,

Sven2157

0
Comment actions Permalink
On the 'if no': Are you saying I could overide the syntax for .HTML altogether, with the Twig file type, but entirely loose the default HTML file type association?

Correct. And it's an IDE-wide setting that affects all projects.

On your last point, you are saying there is no way to force the language option, correct?

Yes -- you cannot apply "Twig" using "Template Data Languages" settings.

0
Comment actions Permalink
Unfortunately, if I change the extension to .html.twig ( or any ccombination, other than plain .html ) it breaks the site.

Well .. technically you should be able to do that .. but for this you will have to patch the template loader (or provide your own loader -- which is a correct way using Twig) -- to add ".twig" part dynamically.

Unfortunately I'm not familiar with phpBB and cannot say if it's possible to make such change permanent so that it will survive actual phpBB upgrades (so that you do not need to re-add this after each new version update).

0
Comment actions Permalink

Yeah I was just looking into that idea of making the .html.twig be viable.

This change is pretty new to phpBB.  So new in fact, that they only mention it in the Discussion section of Area51 - their development forum and wiki area.  They made a bunch of changes to the core framework, and didn't want to overwhelm the community with all the changes.  So they left TWIG off the official additions, and provided for backwards compatibility for their own template engine.  That will envetually be phased out, in favor of Twig, however that is still down the road.

Thanks again for your time Andriy!

P.S. Really glad I made the switch to phpStorm! :x

0

Please sign in to leave a comment.