[Tutorial] Convert to Twig Syntax

Discussion forum for Extension Writers regarding Extension Development.
User avatar
MattF
Extensions Development Coordinator
Extensions Development Coordinator
Posts: 5164
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 »

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: 159
Joined: Tue Sep 25, 2018 8:02 pm
Location: Off Plumb
Name: Bubba
Contact:

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?
Beans: Because just saying we need to socially distance doesn't always work....
User avatar
david63
Registered User
Posts: 17965
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Contact:

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!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored
User avatar
kinerity
Community Team Member
Community Team Member
Posts: 2730
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by kinerity »

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 Truscott - Community Team
GitHub repositories (including un-released extensions)
User avatar
3Di
Former Team Member
Posts: 15692
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

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.
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
:studio_microphone: Looking for a specific feature or alternative option?
User avatar
kinerity
Community Team Member
Community Team Member
Posts: 2730
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by kinerity »

Cool, thanks!
Kailey Truscott - Community Team
GitHub repositories (including un-released extensions)
User avatar
kinerity
Community Team Member
Community Team Member
Posts: 2730
Joined: Mon Sep 01, 2014 1:00 am
Location: sudo rm -rf /
Name: Kailey Truscott
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by kinerity »

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 Truscott - Community Team
GitHub repositories (including un-released extensions)
User avatar
AbaddonOrmuz
Recognised Extension Developer
Posts: 912
Joined: Wed Dec 25, 2013 9:06 pm
Location: /dev/null
Name: Alfredo
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by AbaddonOrmuz »

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:
Image Imgur | :chart_with_upwards_trend: SEO Metadata | Image Markdown | :lock: Auto-lock Topics
:trophy: Check out all my validated extensions :trophy:

:penguin: Arch Linux user :penguin:
User avatar
david63
Registered User
Posts: 17965
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Contact:

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!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored
User avatar
3Di
Former Team Member
Posts: 15692
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

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
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
:studio_microphone: Looking for a specific feature or alternative option?
User avatar
david63
Registered User
Posts: 17965
Joined: Thu Dec 19, 2002 8:08 am
Location: Lancashire, UK
Contact:

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!
My CDB Contributions | How to install an extension
I will not be accepting translations for any of my extensions in Github - please post any translations in the appropriate topic.
No support requests via PM or email as they will be ignored
User avatar
mrgoldy
Development Team Member
Development Team Member
Posts: 1392
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

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
Former Team Member
Posts: 15692
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

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
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
:studio_microphone: Looking for a specific feature or alternative option?
User avatar
mrgoldy
Development Team Member
Development Team Member
Posts: 1392
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

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
Former Team Member
Posts: 15692
Joined: Mon Apr 04, 2005 11:09 pm
Location: Milan (IT) Frankfurt (DE)
Name: Marco
Contact:

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. :)
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
:studio_microphone: Looking for a specific feature or alternative option?
Post Reply

Return to “Extension Writers Discussion”