[Tutorial] Convert to Twig Syntax

Discussion forum for Extension Writers regarding Extension Development.
User avatar
MattF
Extensions Development Coordinator
Extensions Development Coordinator
Posts: 5942
Joined: Sat Jan 17, 2009 9:37 am
Location: Los Angeles, CA
Name: Matt Friedman

Re: [Tutorial] Convert to Twig Syntax

Post by MattF »

Naguissa wrote: Sun Jun 17, 2018 3:22 pm
3Di wrote: Sun Jun 17, 2018 3:15 pm On a side note, the extension's name can not be CamelCase'd if I am not wrong.
Interesting... I've not received any report about this, if so it will be a serious trouble....
Capitals are allowed.
https://www.phpbb.com/extensions/rules- ... extensions

However, keeping everything lowercase makes everything easier!

That's because if you use mixed case for the vendor and package naming, that has to be carried over exactly to the name-spacing of PHP files and in all references to the extension name such as in composer.json. Any mistakes and it will break. It's also worth noting that if developing on a Mac with a drive formatted to be case-insensitive (which is the default), then you won't realize your extension may be broken due to mistakes in getting the cases right until you deploy it to a UNIX based server.
Formerly known as VSEMy ExtensionsPlease do not PM me for support.
User avatar
bubbathegimp
Registered User
Posts: 192
Joined: Tue Sep 25, 2018 8:02 pm
Location: Off Plumb
Name: Bubba

Re: [Tutorial] Convert to Twig Syntax

Post by bubbathegimp »

A little confusion.....
I am all for anything that will improve performance on a shared server since my board is hosted on one.
Just installed the TWIG converter....
before you upload the modified files back to the proper directory, do you need to delete anything from the old directory.
I also noted that when some of the modified files are uploaded, they can be a little larger than the original,
so we might be trading a tad of server space for improved performance?
If at first you don't succeed, Sky diving is not for you....
Ymmv
User avatar
david63
Registered User
Posts: 20646
Joined: Thu Dec 19, 2002 8:08 am

Re: [Tutorial] Convert to Twig Syntax

Post by david63 »

This is an extension writer's/style author's tool - not an extension that you would install on a live board.
David
Remember: You only know what you know and - you don't know what you don't know!

I now no longer support any of my extensions but they will start to become available here
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3803
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay

Re: [Tutorial] Convert to Twig Syntax

Post by Kailey »

Are both of these code examples right?

Code: Select all

$this->template->assign_block_vars('trackers', [
	'TRACKER_NAME'	=> $row['tracker_name'],
	'U_TRACKER'		=> $this->helper->route('kinerity_trackers_controller', ['page' => 'viewtracker', 't' => (int) $row['tracker_id']]),
]);

Code: Select all

{% for tracker in loops.trackers %}
<li>
	<a href="{{ tracker.U_TRACKER }}" role="menuitem">
		<i class="icon fa-file-o fa-fw icon-gray" aria-hidden="true"></i><span>{{ tracker.TRACKER_NAME }}</span>
	</a>
</li>
{% endfor %}
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules
If you have any questions about the rules/customs of this website, feel free to send me a PM.

My little corner of the world | Administrator @ phpBB Modders
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco

Re: [Tutorial] Convert to Twig Syntax

Post by 3Di »

{% for tracker in loops.trackers %}
should be
{% for tracker in trackers %}

Also if it works is deprecated.
Also note that the loops. prefix is only needed in phpBB 3.1.x, and is not needed if you are writing for 3.2.x or later.
🆓 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. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3803
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay

Re: [Tutorial] Convert to Twig Syntax

Post by Kailey »

Cool, thanks!
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules
If you have any questions about the rules/customs of this website, feel free to send me a PM.

My little corner of the world | Administrator @ phpBB Modders
User avatar
Kailey
Community Team Leader
Community Team Leader
Posts: 3803
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Snay

Re: [Tutorial] Convert to Twig Syntax

Post by Kailey »

One more question - how would I convert this to Twig?

Code: Select all

<div class="<!-- IF articles.S_ROW_COUNT is even -->bg1<!-- ELSE -->bg2<!-- ENDIF -->">
Kailey Snay - Community Team Leader
Knowledge Base | Documentation | Community rules
If you have any questions about the rules/customs of this website, feel free to send me a PM.

My little corner of the world | Administrator @ phpBB Modders
User avatar
AlfredoRamos
Recognised Extension Developer
Posts: 1304
Joined: Wed Dec 25, 2013 9:06 pm
Location: /dev/null
Name: Alfredo

Re: [Tutorial] Convert to Twig Syntax

Post by AlfredoRamos »

kinerity wrote: Sun Mar 08, 2020 8:48 pm One more question - how would I convert this to Twig?

Code: Select all

<div class="<!-- IF articles.S_ROW_COUNT is even -->bg1<!-- ELSE -->bg2<!-- ENDIF -->">
Something like:

Code: Select all

<div class="{{ articles.S_ROW_COUNT is even ? 'bg1' : 'bg2' }}">
Or

Code: Select all

<div class="bg{{ articles.S_ROW_COUNT is even ? '1' : '2' }}">
Not tested.
Some of my phpBB extensions:
:chart_with_upwards_trend: SEO Metadata | Image Markdown | :shield: hCaptcha
:trophy: Check out all my validated extensions :trophy:

:penguin: Arch Linux user | Linux Boards :penguin:
User avatar
david63
Registered User
Posts: 20646
Joined: Thu Dec 19, 2002 8:08 am

Re: [Tutorial] Convert to Twig Syntax

Post by david63 »

kinerity wrote: Sun Mar 08, 2020 8:48 pm One more question - how would I convert this to Twig?

Code: Select all

<div class="<!-- IF articles.S_ROW_COUNT is even -->bg1<!-- ELSE -->bg2<!-- ENDIF -->">
You could always use the twig convertor extension to do it for you.
David
Remember: You only know what you know and - you don't know what you don't know!

I now no longer support any of my extensions but they will start to become available here
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco

Re: [Tutorial] Convert to Twig Syntax

Post by 3Di »

david63 wrote: Mon Mar 09, 2020 2:18 am You could always use the twig convertor extension to do it for you.
Dunno if it is capable to handle ternary operators shorthands of if-then-else as above explained.

https://twig.symfony.com/doc/3.x/templa ... -operators
https://www.branchcms.com/learn/docs/de ... /operators
🆓 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. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
david63
Registered User
Posts: 20646
Joined: Thu Dec 19, 2002 8:08 am

Re: [Tutorial] Convert to Twig Syntax

Post by david63 »

3Di wrote: Mon Mar 09, 2020 4:09 am Dunno if it is capable to handle ternary operators shorthands of if-then-else as above explained.
Seems to have worked when I have tried it
David
Remember: You only know what you know and - you don't know what you don't know!

I now no longer support any of my extensions but they will start to become available here
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: [Tutorial] Convert to Twig Syntax

Post by mrgoldy »

I would suggest using loop.index instead, so you rely on twig rather than assigned loop variables.
And then use AbaddonOrmuz' suggestion: {{ loop.index is even ? 'bg1' : 'bg2' }}
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco

Re: [Tutorial] Convert to Twig Syntax

Post by 3Di »

3Di wrote: Thu Mar 05, 2020 11:42 pm {% for tracker in loops.trackers %}
should be
{% for tracker in trackers %}

Also if it works is deprecated.
Also note that the loops. prefix is only needed in phpBB 3.1.x, and is not needed if you are writing for 3.2.x or later.
.loops prefix definitely does not work in phpBB 3.3 as a side note. At least here. :P
🆓 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. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs

Re: [Tutorial] Convert to Twig Syntax

Post by mrgoldy »

There is a distinct difference between {% for x in loops.y %} and {{ loop.index }}.
loop. is a twig reference, while loops. is a prefix added to template block variables by phpBB.
For full documentation: https://twig.symfony.com/doc/3.x/tags/for.html
phpBB Studio / Member of the Studio

Contributing: You can do it too! Including testing Pull Requests (PR).
phpBB Development and Testing made easy.
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco

Re: [Tutorial] Convert to Twig Syntax

Post by 3Di »

mrgoldy wrote: Tue Mar 10, 2020 12:29 am ... while loops. is a prefix ...
3Di wrote: Tue Mar 10, 2020 12:19 am.loops prefix
I know, it was not a reply to yours m8 - just a general clarification for the readers. :)
🆓 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. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades

Return to “Extension Writers Discussion”