How to use { in a template file

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
User avatar
javiexin
Code Contributor
Posts: 1157
Joined: Wed Oct 12, 2011 11:46 pm
Location: Madrid, Spain
Name: Javier
Contact:

How to use { in a template file

Post by javiexin »

Hi,

I would like to use the following HTML code in a template file (example):

Code: Select all

<input type="text" pattern="#[a-fA-F0-9]{6}" name="mycolor" value="#000000" size="7"/>
Now all works fine, EXCEPT the {6} is processed by the phpbb template engine, and replaced by 6 without the brackets, making the pattern fail.

Any suggestions on how to solve this? How to force the brackets into the output?

And no, putting six times the same pattern is not valid: I have other use cases for something like {5,25} that would be really awful in a pattern without multipliers.

Thanks a lot!
-javiexin

PS: I have tested pattern="#[a-fA-F0-9]\{6\}" but that is not good either: the pattern includes the backslashes, so does not interpret the multiplier, and expects the brackets in the input. Also other variations do not make any difference, like using single quotes, or combinations of single/double quotes.
User avatar
javiexin
Code Contributor
Posts: 1157
Joined: Wed Oct 12, 2011 11:46 pm
Location: Madrid, Spain
Name: Javier
Contact:

Re: How to use { in a template file

Post by javiexin »

Sorry to bother, I found an alternative, ugly, but it works (using Twig escaping):
pattern="#[a-fA-F0-9]{{ '{' }}6{{ '}' }}"

Any other options?
Thanks!
User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 4577
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: How to use { in a template file

Post by kasimi »

Have you tried using double braces? pattern="#[a-fA-F0-9]{{6}}"

Don't forget there's also a short version to specify a color: #ABC
User avatar
javiexin
Code Contributor
Posts: 1157
Joined: Wed Oct 12, 2011 11:46 pm
Location: Madrid, Spain
Name: Javier
Contact:

Re: How to use { in a template file

Post by javiexin »

Thanks a lot Kasimi, but:

Code: Select all

Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'A hash key must be followed by a colon (:). Unexpected token "punctuation" of value "}" ("punctuation" expected with value ":") in "file.html" at line 36.' in C:\wamp64\www\phpbb\quickinstall\boards\tb31\vendor\twig\twig\lib\Twig\TokenStream.php on line 87
If I put spaces around, then it would be interpreted as a Twig expression, and evaluated as 6.
And if I try pattern="#[a-fA-F0-9]{{ '{6}' }}" it is translated as {{ 6 }} and sent that way to the template.

And for the other syntax, yes, I know that CSS allows that, and also 'aliceblue', 'palevioletred' and quite a lot more (that sometimes I have a hard time to interpret, I don't see those many colors :lol:). I am not planning to complicate this input so much, only give the simple basic option.

Again, many thanks for your suggestion.
-javiexin
User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 4577
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: How to use { in a template file

Post by kasimi »

I didn't expect Twig to pick up single curly braces like this. :shock: There must be a prettier solution, but how about this? pattern="#[a-fA-F0-9]{{ '{{6}}' }}"
User avatar
javiexin
Code Contributor
Posts: 1157
Joined: Wed Oct 12, 2011 11:46 pm
Location: Madrid, Spain
Name: Javier
Contact:

Re: How to use { in a template file

Post by javiexin »

Thanks again, Kasimi, but no: it gets translated as {{{ 6 }}}
kasimi wrote:
Tue Aug 08, 2017 1:12 pm
I didn't expect Twig to pick up single curly braces like this. :shock:
In fact, I am suspecting that this is not produced by Twig, but by the phpbb preprocessor...
User avatar
javiexin
Code Contributor
Posts: 1157
Joined: Wed Oct 12, 2011 11:46 pm
Location: Madrid, Spain
Name: Javier
Contact:

Re: How to use { in a template file

Post by javiexin »

Looking at it further, I think it is this line that is causing the issue:
https://github.com/phpbb/phpbb/blob/3.2 ... r.php#L134
$code = preg_replace('#{([a-zA-Z0-9_\.]+)(\|[^}]+?)?}#', '{{ $1$2 }}', $code);

Now, should this be fixed at the core?
-javiexin
User avatar
canonknipser
Registered User
Posts: 2096
Joined: Thu Sep 08, 2011 4:16 am
Location: Germany
Name: Frank Jakobs
Contact:

Re: How to use { in a template file

Post by canonknipser »

If you use pure twig syntax in your template, then the lexer functions should be skipped, as they are not needed. But afiak there is currently no switch to tell phpBB that a template file is written in pure twig syntax
Greetings, Frank
phpbb.de support team member
English is not my native language - no support via PM or mail
New arrival - Extensions and scripts for phpBB
User avatar
javiexin
Code Contributor
Posts: 1157
Joined: Wed Oct 12, 2011 11:46 pm
Location: Madrid, Spain
Name: Javier
Contact:

Re: How to use { in a template file

Post by javiexin »

canonknipser wrote:
Tue Aug 08, 2017 6:19 pm
If you use pure twig syntax in your template, then the lexer functions should be skipped, as they are not needed. But afiak there is currently no switch to tell phpBB that a template file is written in pure twig syntax
Yes, you are right. They run always, as there is no way to tell if a template is phpbb or Twig.
-javiexin

PS: I am using pure Twig in the template anyway.
User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 4577
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: How to use { in a template file

Post by kasimi »

Two alternatives that avoid phpBB's syntax parser. Still not the prettiest but they also work with ranges.
  • pattern="#[a-fA-F0-9]{{ '{ 5,25 }' | replace({' ': ''}) }}"
  • pattern="#[a-fA-F0-9]{{ '{%d,%d}' | format(5, 25) }}"
User avatar
javiexin
Code Contributor
Posts: 1157
Joined: Wed Oct 12, 2011 11:46 pm
Location: Madrid, Spain
Name: Javier
Contact:

Re: How to use { in a template file

Post by javiexin »

The best solution I have found is the following: pattern="#[a-fA-F0-9]{6,6}"

It works perfectly, and is translated as it should, basically because the phpbb lexer is NOT understanding 6,6 as a variable name, while 6 is interpreted as such, and hence, replaced for Twig to process it.

Thanks a lot for your help. Finally, I will implement this one.
-javiexin
User avatar
javiexin
Code Contributor
Posts: 1157
Joined: Wed Oct 12, 2011 11:46 pm
Location: Madrid, Spain
Name: Javier
Contact:

Re: How to use { in a template file

Post by javiexin »

javiexin wrote:
Tue Aug 08, 2017 5:09 pm
Looking at it further, I think it is this line that is causing the issue:
https://github.com/phpbb/phpbb/blob/3.2 ... r.php#L134
$code = preg_replace('#{([a-zA-Z0-9_\.]+)(\|[^}]+?)?}#', '{{ $1$2 }}', $code);
Looking at this, and given the situation, I will propose a change in the core to replace the above line with the following:
$code = preg_replace('#{([a-zA-Z][a-zA-Z0-9_\.]*)(\|[^}]+?)?}#', '{{ $1$2 }}', $code);

Basically, the change is to ask for a token that starts with a letter (not number or _ or .) followed by any number (0 or more) of letters, digits, _ or .; now, it is a non empty number of that set (1 or more).

Let's see if it works and is approved.
-javiexin
User avatar
MattF
Extensions Development Coordinator
Extensions Development Coordinator
Posts: 5263
Joined: Sat Jan 17, 2009 9:37 am
Location: Los Angeles, CA
Name: Matt Friedman
Contact:

Re: How to use { in a template file

Post by MattF »

javiexin wrote:
Tue Aug 08, 2017 11:38 am
It's really quite simple: {{ '{' }}

Code: Select all

<input type="text" pattern="#[a-fA-F0-9]{{ '{' }}6{{ '}' }}" name="mycolor" value="#000000" size="7"/>
Formerly known as VSEMy ExtensionsPlease do not PM me for support.
User avatar
javiexin
Code Contributor
Posts: 1157
Joined: Wed Oct 12, 2011 11:46 pm
Location: Madrid, Spain
Name: Javier
Contact:

Re: How to use { in a template file

Post by javiexin »

VSE wrote:
Thu Aug 10, 2017 3:55 pm
It's really quite simple: {{ '{' }}
I know:
javiexin wrote:
Tue Aug 08, 2017 11:52 am
Sorry to bother, I found an alternative, ugly, but it works (using Twig escaping):
pattern="#[a-fA-F0-9]{{ '{' }}6{{ '}' }}"

Any other options?
Thanks!
But honestly, it is not quite simple, and anyhow I like the other option better (pattern="#[a-fA-F0-9]{6,6}"): more readable and easier to maintain.

-javiexin
Post Reply

Return to “Extension Writers Discussion”