[Tutorial] Convert to Twig Syntax

Discussion forum for Extension Writers regarding Extension Development.
Kuzia
Registered User
Posts: 31
Joined: Sat Dec 31, 2016 6:59 pm
Location: Moscow
Name: Alex

Re: [Tutorial] Convert to Twig Syntax

Post by Kuzia »

What about form tokens? {S_FORM_TOKEN} is still in use?
User avatar
MattF
Extensions Development Coordinator
Extensions Development Coordinator
Posts: 5322
Joined: Sat Jan 17, 2009 9:37 am
Location: Los Angeles, CA
Name: Matt Friedman
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by MattF »

Kuzia wrote:
Fri May 26, 2017 11:49 pm
What about form tokens? {S_FORM_TOKEN} is still in use?
See the very first example in the first post.
Formerly known as VSEMy ExtensionsPlease do not PM me for support.
User avatar
3Di
Former Team Member
Posts: 16091
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milano 🇮🇹 Frankfurt 🇩🇪
Name: Marco
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by 3Di »

Kuzia wrote:
Fri May 26, 2017 11:49 pm
{S_FORM_TOKEN} is still in use?
Just in reply of this question, specifically. Also if it is something that doesn't really IMO belongs to this topic.

YES, it is. And the related PHP side of the medal, that's considered a security issue if missing, beware. :geek:

Twig: {{ S_FORM_TOKEN }}
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Want to compensate me for my interest? Donate
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
javiexin
Code Contributor
Posts: 1157
Joined: Wed Oct 12, 2011 11:46 pm
Location: Madrid, Spain
Name: Javier
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by javiexin »

Hello again,

I am trying to do something like this, but I cannot make it to work in Twig.
javiexin wrote:
Fri May 26, 2017 11:46 pm
An interesting effect of this, is that now, in the Twig template syntax, you CAN use language keys with parameters! So you could write something like {{ lang('KEY', 3) }} or even nesting vars, if that is allowed {{ lang('KEY', {{ VALUE }}) }}
This works {{ lang('KEY', 3) }}, but what doesn't work is the nesting of variables as in {{ lang('KEY', {{ VALUE }}) }}, neither using them in set statements {% set newvar = {{ VALUE }} %}.

Any clues?

Thanks a lot.
-javiexin
User avatar
javiexin
Code Contributor
Posts: 1157
Joined: Wed Oct 12, 2011 11:46 pm
Location: Madrid, Spain
Name: Javier
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by javiexin »

Another question: how to perform simple arithmetics with template variables, like {{ TOTAL }} - {{ USED }} and use the resulting value?
-javiexin
User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 4725
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by kasimi »

No need for nesting:
  • {{ lang('KEY', VALUE) }}
  • {% set newvar = VALUE %}
  • {{ TOTAL - USED }}
  • {% set newval = TOTAL - USED %}
User avatar
javiexin
Code Contributor
Posts: 1157
Joined: Wed Oct 12, 2011 11:46 pm
Location: Madrid, Spain
Name: Javier
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by javiexin »

Thanks a lot kasimi!!!
User avatar
Toxyy
Registered User
Posts: 812
Joined: Mon Oct 24, 2016 3:22 pm
Location: Namek
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by Toxyy »

How much faster does this make your site/take the load off your server?
I am a web developer/administrator, specializing in forums. If you have work you need done or are too lazy to do, pm me!

My extensions:
[3.2][BETA] Anonymous Posts || [3.2][BETA] Sticky Ad || [3.2][RC] Show User Activity ||
[3.2][DEV] User Delete Topics || [3.3][RC] Short Number Twig Extension
User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 4725
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by kasimi »

I haven't done any performance tests but I'd assume it's negligible for two reasons:
  1. As phpBB doesn't differentiate between templates that use phpBB syntax and templates that use Twig syntax, the code that checks for phpBB syntax has to run on all template files, regardless of them using phpBB or Twig syntax.
  2. Once converted, templates are cached. They are not converted for every request.
User avatar
</Solidjeuh>
Registered User
Posts: 1788
Joined: Tue Mar 29, 2016 3:45 am
Location: Aalst (Belgium)
Name: Andy Dm
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by </Solidjeuh> »

I'm updating my style @ the moment for phpBB 3.2.1 RC1
It's best to convert the whole style to Twig Syntrax then?
User avatar
kasimi
Extension Customisations
Extension Customisations
Posts: 4725
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by kasimi »

It is recommended to not mix them both. Also have a look at the Twig Converter.
User avatar
</Solidjeuh>
Registered User
Posts: 1788
Joined: Tue Mar 29, 2016 3:45 am
Location: Aalst (Belgium)
Name: Andy Dm
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by </Solidjeuh> »

kasimi wrote:
Sat Jul 15, 2017 8:27 am
It is recommended to not mix them both. Also have a look at the Twig Converter.
Converted all the extensions, styles & admin panel.
No problems so far.. Great ext! 8-)
Blitze
Registered User
Posts: 418
Joined: Tue Nov 29, 2005 5:06 pm

Re: [Tutorial] Convert to Twig Syntax

Post by Blitze »

Does phpBB recommend any particular file extension when converting to twig: .html, .twig, .html.twig?

BTW, it appears that template events are currently set to only work with .html.
My Extensions: phpBB SiteMaker
User avatar
3Di
Former Team Member
Posts: 16091
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milano 🇮🇹 Frankfurt 🇩🇪
Name: Marco
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by 3Di »

.html as usual
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Want to compensate me for my interest? Donate
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
MarkDHamill
Registered User
Posts: 4347
Joined: Fri Aug 02, 2002 12:36 am
Location: Florence, MA USA
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by MarkDHamill »

Maybe I'm missing something?

Old code:

Code: Select all

var collapseImageIdAlt = '{LA_DIGESTS_COLLAPSE}';
var collapseImageIdTitle = '{LA_DIGESTS_COLLAPSE}';
Resulting HTML source code generated:

Code: Select all

var collapseImageIdAlt = 'Collapse';
var collapseImageIdTitle = 'Collapse';
New code:

Code: Select all

var collapseImageIdAlt = '{{ lang(DIGESTS_COLLAPSE)|addslashes }}';
var collapseImageIdTitle = '{{ lang(DIGESTS_COLLAPSE)|addslashes }}';
Resulting HTML source code generated:

Code: Select all

var collapseImageIdAlt = '';
var collapseImageIdTitle = '';
Need phpBB services or a phpBB consultant? I offer most phpBB services. Getting lost managing phpBB? Buy my book, Mastering phpBB Administration. eBook and paper versions available.
Post Reply

Return to “Extension Writers Discussion”