2

Twig syntax highlighting in code blocks is a mess. Many things just don't work (see first bug report and examples below) and make reading code less clear than without highlights.

Please remove the feature until this works better.

.

Problems

Twig:

  • Some Twig tags and operators are highlighted (1) and some are not (2)
  • Filters are not highlighted at all (3)
  • String "var" highlighted as twig tag (4)

Twig Comments:

  • No highlighting of comment text and opening tag (5)
  • Twig code highlighting not disabled within comments (6)

HTML:

  • Different opening/closing tag highlights (7)
  • Opening tag highlighted differently if attributes added (8)
  • Twig code highlighting not disabled within comments (9)
  • HTML code highlighted treated similar to twig code and vars (10)
  • No highlighting of HTML comments (11)
  • Complete twig syntax highlighting fails if the code begins with html (see ex. here)

Other:

  • Strange highlights of words like "Twig" and "Comment" (12)

.

Examples

Syntax language set to lang-twig:

{% set foo = true %} [1]

{% extends "foo.html" %} [1]

{% spaceless %} ... {% endspaceless %} [2]

{% macro foo() %} ... {% endmacro %} [2]

{{ foo|raw ~ foo|length ~ foo|date('Y') ~ '...' }} [3]

{% if var == true and foo == 'string' %} ... {% elseif foo %} ... {% endif %} [2, 4]

{# Twig code within comments: extends, in, ... #} [5, 6, 12]

<p>Twig code within html: with, set, ...</p> [7, 9, 10, 12]

<p attribute="extends test">HTML code with attributes</p> [8, 10]

<!-- HTML Comment --> [11, 12]

.

Comparison

Compare this to syntax highlighting in Sublime Text using PHP-Twig.tmbundle by Andrew Fricke or Craft-Twig.tmbundle by Ben Parizek:

enter image description here

23
  • Nicael reported this as a bug, but it's obviously an issue. – Matt Stein Jul 14 '14 at 18:09
  • Since this was specifically requested and added earlier, I'm going to keep an eye on responses here before immediately reversing the change. I think it makes sense to turn off the default lang-php option for the tags that are using it, and encourage people to use <!-- language: lang-twig --> in the code block if desired. – Matt Stein Jul 14 '14 at 18:14
  • @Matt I was about to add the bugs I found to nicael bug report. But because I found so many of them (and I probably didn't find them all), I decided to open a discussion about removing the highlighting altogether temporarily. – carlcs Jul 14 '14 at 18:36
  • 2
    Official documentation on buildwithcraft.com works well for me without any highlighting, way more readable then much of the code over here. I just changed highlighting from lang-none to lang-twig with one of my answers, I think this works very good as an example of how bad this actually works so far. – carlcs Jul 14 '14 at 18:50
  • Hey @Christian, the other mods and I have chatted briefly about this and didn't come to any formal conclusion. But this request has not been forgotten, and has a lot of merit. Let me touch base with them again, and we'll try to get you a conclusive response soon! – Lindsey D Jul 21 '14 at 19:27
  • It needs to be fixed, but I think it's better then nothing. I vote we keep it running in the interim before it's updated. – Simon Kuran Jul 22 '14 at 23:21
  • 1
    @Simon no I think it is way worse than nothing. Just look at that example I posted above. Not only not helpfull, it is confusing and irritating especially for twig newcomers! – carlcs Jul 25 '14 at 9:36
  • What about getting Brandon's new twig highlighting over here, @Lindsey? In his code I trust ;) In the meantime I'd still vote to disable it for now! – carlcs Jul 25 '14 at 9:42
  • @Lindsey I just found the next bug: "Complete twig syntax highlighting fails if the code begins with html (see ex. here)". Wow, I can't believe that we still need to have this running! Shall we start to bug fix the syntax highlighting by adding unnecessary code to questions or answers... – carlcs Jul 29 '14 at 11:27
  • @Christian Yikes, that's a serious bug! Definitely not acceptable when about half the examples here are Twig (and many start with HTML). Anna and I are still trying to get to the bottom of the best solution here... It's looking like we're going to disable all automatic syntax highlighting, and just provide some sort of FAQ page which will show people how to manually add their own syntax highlighting. I don't think we can use Brandon's code (or otherwise make custom modifications) because SE is relying on Google Code Prettify to work its magic. – Lindsey D Jul 29 '14 at 16:38
  • @Lindsey and what about all the code snippets where <!-- language: lang-twig --> is already added? They'll still look like cr.. (sorry) or I'll have to open them all up and remove the line?! Please just remove it altogether. It's just for now until s.o. makes it work properly. In the meantime we can all continue to use it in the markup and then, some day, they'll all look beautiful :D – carlcs Jul 29 '14 at 16:46
  • @Christian Believe it or not, I don't think Twig syntax is even supported. Which means that every time someone uses that language tag, the parser is doing exactly... nothing. Or more specifically, it's falling back to "default" detection, which is generally picking it up as HTML. – Lindsey D Jul 29 '14 at 16:53
  • @Christian I can't find a single reference anywhere on the interwebs about Google Code Prettify supporting Twig... If you can find it mentioned somewhere that Twig is supported, please paste a link here and I will continue to investigate. Thanks! – Lindsey D Jul 29 '14 at 16:55
  • @Lindsey will do, but isn't that enough of a reason to disable it NOW? – carlcs Jul 29 '14 at 17:15
  • @Christian There isn't anything to disable. If Twig syntax isn't supported, the parser will treat it as "default"... it attempts to just guess the best match. At this point, the best we can do is force none or html as the automatic highlighting for anything with a "twig" tag. I'd like to hear more from the community and other mods before we make a decision like that. – Lindsey D Jul 29 '14 at 17:30
1

Twig syntax highlighting is apparently a myth.

I did a lot of research, and wrote about my findings here...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .