[Tutorial] Convert to Twig Syntax

Discussion forum for Extension Writers regarding Extension Development.
User avatar
AlfredoRamos
Recognised Extension Developer
Posts: 1302
Joined: Wed Dec 25, 2013 9:06 pm
Location: /dev/null
Name: Alfredo
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by AlfredoRamos »

MarkDHamill wrote: Wed Nov 29, 2017 1:19 am

Code: Select all

var collapseImageIdAlt = '{LA_DIGESTS_COLLAPSE}';
var collapseImageIdTitle = '{LA_DIGESTS_COLLAPSE}';
Try:

Code: Select all

var collapseImageIdAlt = '{{ lang("DIGESTS_COLLAPSE") | escape("js") }}';
var collapseImageIdTitle = '{{ lang("DIGESTS_COLLAPSE") | escape("js") }}';
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
MarkDHamill
Registered User
Posts: 4885
Joined: Fri Aug 02, 2002 12:36 am
Location: Florence, MA USA
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by MarkDHamill »

Thank you. This works. I suggest changing the first post, removing the addslashes option.
Need phpBB services or a phpBB consultant? I offer most phpBB services. Getting lost managing phpBB? Buy my book, Mastering phpBB Administration. Covers through phpBB 3.3.7. eBook and paper versions available.
User avatar
mrgoldy
Former Team Member
Posts: 1394
Joined: Tue Oct 06, 2009 7:34 pm
Location: The Netherlands
Name: Gijs
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by mrgoldy »

I am not sure if there is an addslashes filter for Twig at the momoment, you can add one yourself.
Perhaps have a look at the Twig escape docs.

And for the language key, you need to add ' for language inputs,
eg: {L_DIGEST_COLLAPSE} becomes {{ lang('DIGEST_COLLAPSE') }}
and {SOME_VARIABLE} becomes {{ SOME_VARIABLE }}
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
MarkDHamill
Registered User
Posts: 4885
Joined: Fri Aug 02, 2002 12:36 am
Location: Florence, MA USA
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by MarkDHamill »

Since my extension uses the ACP, I am trying to convert a common snippet of template code:

Code: Select all

<!-- BEGIN options -->
	<!-- IF options.S_LEGEND -->
		<!-- IF not options.S_FIRST_ROW -->
			</fieldset>
		<!-- ENDIF -->
		<fieldset>
			<legend>{options.LEGEND}</legend>
	<!-- ELSE -->

		<dl>
			<dt><label for="{options.KEY}">{options.TITLE}{L_COLON}</label><!-- IF options.S_EXPLAIN --><br /><span>{options.TITLE_EXPLAIN}</span><!-- ENDIF --></dt>
			<dd>{options.CONTENT}</dd>
		</dl>

	<!-- ENDIF -->
<!-- END options -->
This looks like it should work:

Code: Select all

{% for option in options %}
	{% if option.S_LEGEND %}
		{% if not loop.first %}
	</fieldset>
		{% endif %}
	<fieldset>
		<legend>{{ option.LEGEND }}</legend>
	{% else %}

	<dl>
		<dt><label for="{{ option.KEY }}">{{ option.TITLE }}{{ lang('COLON') }}</label>{% if option.S_EXPLAIN %}<br><span>{{ option.TITLE_EXPLAIN }}</span>{% endif %}</dt>
		<dd>{{ option.CONTENT }}</dd>
	</dl>

	{% endif %}
{% endfor %}
but this leaves out a </fieldset> before the </form> tag, so it is not valid. Something I am missing?
Need phpBB services or a phpBB consultant? I offer most phpBB services. Getting lost managing phpBB? Buy my book, Mastering phpBB Administration. Covers through phpBB 3.3.7. eBook and paper versions available.
User avatar
david63
Registered User
Posts: 20646
Joined: Thu Dec 19, 2002 8:08 am

Re: [Tutorial] Convert to Twig Syntax

Post by david63 »

</fieldset> is an HTML tag not a Twig tag so whatever problem you have now you will have had in the past.
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
MarkDHamill
Registered User
Posts: 4885
Joined: Fri Aug 02, 2002 12:36 am
Location: Florence, MA USA
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by MarkDHamill »

thank you. Next question. How do I convert something like this?

Code: Select all

	<!-- INCLUDE {auth_tpl.TEMPLATE_FILE} -->
Need phpBB services or a phpBB consultant? I offer most phpBB services. Getting lost managing phpBB? Buy my book, Mastering phpBB Administration. Covers through phpBB 3.3.7. eBook and paper versions available.
User avatar
kasimi
Former Team Member
Posts: 4900
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by kasimi »

See first post, Includes section, first example. Also have a look at the Twig Converter extension.
User avatar
MarkDHamill
Registered User
Posts: 4885
Joined: Fri Aug 02, 2002 12:36 am
Location: Florence, MA USA
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by MarkDHamill »

kasimi, you created an awesome extension that saves developers tons of time! I don't particularly want to learn more about the TWIG syntax until I have to dig into it.

I have some language files with the old syntax still embedded in it. Any chance you will update the extension to process these as well?
Need phpBB services or a phpBB consultant? I offer most phpBB services. Getting lost managing phpBB? Buy my book, Mastering phpBB Administration. Covers through phpBB 3.3.7. eBook and paper versions available.
User avatar
kasimi
Former Team Member
Posts: 4900
Joined: Sat Sep 10, 2011 7:12 pm
Location: Germany
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by kasimi »

MarkDHamill wrote: Fri Dec 15, 2017 12:52 am I have some language files with the old syntax still embedded in it.
There's template syntax embedded in language files? Do you have an example?
User avatar
MarkDHamill
Registered User
Posts: 4885
Joined: Fri Aug 02, 2002 12:36 am
Location: Florence, MA USA
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by MarkDHamill »

The messenger class uses template tags that go in the /language/en/email folder, so extensions that do emailing like mine must create them in our extensions. In 3.0 it was not as sophisticated as the full templating system. You could not use loops, for example. Perhaps this is no longer the case.

It wasn't a big deal to convert my language files so I did it manually, but you might want to add it to the files to convert.

Here's part of my digest tree as an example:

https://github.com/MarkDHamill/digests/ ... e/en/email

Your extension certainly saved me hours of tedious work, so thanks again!
Need phpBB services or a phpBB consultant? I offer most phpBB services. Getting lost managing phpBB? Buy my book, Mastering phpBB Administration. Covers through phpBB 3.3.7. eBook and paper versions available.
User avatar
Naguissa
Registered User
Posts: 498
Joined: Thu Mar 02, 2017 6:55 am
Location: Barcelona
Name: Naguissa
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by Naguissa »

Hello,


I received this message trying to validate an extension:
{% include 'partials/star_rating.html' %}

Line 27: you should use the @vendor_extname/ prefix here.


I've tried:

Code: Select all

{% include '@vendor_extname/partials/star_rating.html' %}
{% include @vendor_extname'/partials/star_rating.html' %}
Giving me both a blank page (syntax error).


I've asked reporter but didn't receive any feedback.

So, anyone knows how to use it in twig templates? I know hot to use in old templates, but not on twig....



Thanks!
User avatar
david63
Registered User
Posts: 20646
Joined: Thu Dec 19, 2002 8:08 am

Re: [Tutorial] Convert to Twig Syntax

Post by david63 »

Can you not use just {% include 'star_rating.html' %}
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
Naguissa
Registered User
Posts: 498
Joined: Thu Mar 02, 2017 6:55 am
Location: Barcelona
Name: Naguissa
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by Naguissa »

david63 wrote: Sun Jun 17, 2018 2:55 pm Can you not use just {% include 'star_rating.html' %}
It works (well, adding the "partials" folder), but seems to be against validation rules.
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by 3Di »

The problem is your "partial" folder is outside of the "event" one, you should indicate the right route for it.

As per the @vendor_extname/ prefix, who is the vendor and what's the extension name :roll:

That should be
{% include '@gfksx_ThanksForPosts/partials/star_rating.html' %}

On a side note, the extension's name can not be CamelCase'd if I am not wrong.
🆓 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
Naguissa
Registered User
Posts: 498
Joined: Thu Mar 02, 2017 6:55 am
Location: Barcelona
Name: Naguissa
Contact:

Re: [Tutorial] Convert to Twig Syntax

Post by Naguissa »

3Di wrote: Sun Jun 17, 2018 3:15 pm The problem is your "partial" folder is outside of the "event" one, you should indicate the right route for it.

As per the @vendor_extname/ prefix, who is the vendor and what's the extension name :roll:

That should be
{% include '@gfksx_ThanksForPosts/partials/star_rating.html' %}
Oh, my!!!

In old "html-comments-like" version it was a variable!

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....




Thanks!!!
Post Reply

Return to “Extension Writers Discussion”