Tapatalk

Syntax highlighting file for Twig

Syntax highlighting file for Twig

251
Basic UserBasic User
251

PostFeb 14, 2025#1

Hello,

I did not find a syntax highlighting file for the Twig template engine. Is there any on a hidden place?

I try to understand the smarty template engine wordfile, maybe to adopt it. But it is not really the same.

In Twig, a distinction is often made between an opening and closing tag, e.g. {% if ... %} {% endif %} {% block ... %} {% endblock %}
In Smarty, it looks like this: [{ if ... }] [{ /if }] [{ block ... }] [{ /block }]

I don't know how to represent this in the wordfile.

6,826625
Grand MasterGrand Master
6,826625

PostFeb 15, 2025#2

There is the page User-submitted wordfiles for UltraEdit/UEStudio and wordfiles repository on GitHub and no hidden place.

It would be necessary that UltraEdit would detect the beginning/end of a Twig block for true multi-language syntax highlighting of HTML files with Twig embedded to highlight the HTML code according to the definitions in the syntax highlighting wordfile html.uew and the Twig code according to the definitions in a syntax highlighting wordfile like twig.uew. But such a built-in support for Twig block identification in an HTML file does not yet exist as also not existing for Smarty in UltraEdit for Windows and UEStudio v2024.2.0.44. Such a built-in support exists, for example, for CSS, JS and PHP, but not for Twig or Smarty. It is necessary for that reason to replace html.uew in the wordfiles directory as configured at Advanced - Settings or Configuration - Editor display - Syntax highlighting (default: %APPDATA%\IDMComp\UltraEdit\wordfiles) by an extended wordfile with HTML and Twig definitions in one wordfile.

The user-submitted wordfile for Smarty is not good and is also very old. You should delete this wordfile from the wordfiles directory respectively the wordfile added by you on which you made modifications.

I decided using the wordfile html.uew as installed with UltraEdit for Windows and UEStudio v2024.2.0.44 as template and modified it by adding the color groups /C5 to /C15. Download the attached ZIP file, extract the wordfile html.uew from the ZIP file and replace with that file the syntax highlighting wordfile html.uew in your wordfiles directory. It is important for the HTML multi-language syntax highlighting of UltraEdit that there is just one *.uew file in the wordfiles directory containing the the special keyword HTML_LANG. Then start UltraEdit or restart it if UE is currently running.

I do not know which file extension is used by default for HTML files with embedded Twig blocks. The File Extensions = list at the end of the first line is not modified for that reason. Open in UltraEdit the configuration dialog Advanced - Settings or Configuration - Editor display - Syntax highlighting, select the installed wordfile HTML, click on button Open and close the configuration. Open an HTML file with Twig blocks in same instance of UltraEdit and add its file extension (in upper case as being not case sensitive) after File Extensions = if not being already in the file extensions list, save the modified wordfile and the syntax highlighting should be immediately applied to the HTML file with Twig blocks.

I have made following changes in the original HTML wordfile sections:
  1. The keyword Noquote in the first line for disabling default syntax highlighting of strings (= sequence of characters between " or ') is replaced by String Chars = "' Escape Char = \ EnableMLS for enabling multi-line string syntax highlighting.
    It is up to you if that is good or not good for the HTML files with lots of strings (attributes) edited by you with Twig blocks with strings enclosed in " or '.
    You can replace the string syntax highlighting definition back to Noquote if you prefer attribute values in HTML blocks not syntax highlighted as strings which of course means that also the strings in Twig blocks are not syntax highlighted.
    There could be used also String Chars = ' Escape Char = \ DisableMLS in the wordfile if your HTML code is always with " for strings like attribute values and in Twig code is used by you only ' for strings which begin and end always on same line. It is sometimes better making not use of all variants a language supports for a better syntax highlighting in UltraEdit.
  2. There is added to the first line Block Comment On Alt = {# Block Comment Off Alt = #} to get comments in Twig blocks syntax highlighted with the text and background color and the font style defined in the Manage Themes dialog window on tab Syntax for the language HTML for the color group Alternate block comments. You should configure that color group suitable for the used theme and of course also the color and font style settings for the additional color groups Operators, Twig Tags, Twig Filters, Twig Functions, Twig Tests, Symfony Tags, Symfony Filters, Symfony Functions, Symfony Tests, Twig Global Variables, Twig Keywords.
  3. There are added with the lines 33 to 38 function string definitions to find Twig block and macro definitions in the file and list them (with the parameters for macros) in the Function List view. It would be of course possible to add more such function string definitions. It would be further possible to change the function string definition for Twig macro definitions to a hierarchical function string definition listing in the Function List view just the macro name on first level (second level with Twig Macros interpreting as first level) and each parameter of the macro in a sublevel.
  4. The first /Open Brace Strings = "{" in line 45 is modified to "{%" and added "{{".
  5. The first /Close Brace Strings = "}" in line 46 is modified to "%}" and added "}}".
  6. The HTML attributes max and min are deleted from line 97 as these two words are added to the color group Twig Functions as I suppose the two words are more often used in Twig blocks than in HTML blocks.
  7. Duplicate HTML attribute onerror= is removed from the line 99.
  8. The character = is moved from last line 101 (in original html.uew) of the color group Attributes to line 314 of the color group Operators because of being better most likely for the Twig syntax highlighting.
The Twig syntax is very special according to the documentations (for engine 3.x) as it defines strings of which meaning depends on the context. There is the tag with and the operators starts with and ends with. It is impossible for the general syntax highlighting engine of UltraEdit not written specific for Twig syntax highlighting according to its syntax rules to differ between usage of the word with as tag and as part of the name of operators.

The space character is the word delimiter in syntax highlighting wordfiles of UltraEdit. It is therefore not possible to define a phrase like divisible by as a keyword of a syntax highlighting language. There is for that reason just divisible in color group Twig Tests. You could add by also on a separate line to this color group, but that results in by would be everywhere syntax highlighted including the HTML sections.

You will for sure notice that also other named operators like and, is, not … in color group Operators are syntax highlighted also in text in HTML sections. That cannot be avoided without built-in support for Twig section identification by UltraEdit. It is your decision to live with the false positive syntax highlighting in HTML sections or remove those operators from the syntax highlighting wordfile on being rarely used in Twig sections. Most languages use operators like && for and for making the parsing easier and less context sensitive.

Code folding for Twig blocks seems not to be possible because of built-in code folding for HTML is disabled on adding open/close fold strings. I do not even get a customized code folding for the HTML sections working with UE/UES v2024.2.0.44. That needs further investigation by me because that was possible with older versions of UE/UES, or I do something wrong.

Note: I have not read the entire docs for Twig for 3.x engine and have not tested everything in the attached syntax highlighting wordfile.

It is perhaps necessary to make corrections or add something to the wordfile. Feel free to ask here if you need help by me or other UltraEdit forum members on further improvement of the wordfile.
html_twig_wordfile_v1.zip (10.18 KiB)   0
UltraEdit wordfile for HTML + Twig syntax highlighting version 1

251
Basic UserBasic User
251

PostFeb 18, 2025#3

Hallo @Mofi : It looks very nice. I did only one small tweak. I´ve add "TWIG" as extension in my file.

And to your question about code folding: The HTML code is folded correctly. There is no folding in the Twig tags yet. This is probably due to the tag structure with the "end...". (see Screenshot)

I am currently using 2024.1.0.36 64-bit, I rolled back my version 2024.2 because there were some errors in the GUI. I am now waiting for the next release 2024.3...
2025-02-18 14_27_23.png (107.04KiB)
Screenshot CodeFolding