Twig as our template engine

These requests for comments/change have lead to an implemented feature that has been successfully merged into the 3.1/Ascraeus branch. Everything listed in this forum will be available in phpBB 3.1.
Post Reply
User avatar
Marc
Development Team Leader
Development Team Leader
Posts: 185
Joined: Thu Sep 09, 2010 11:36 am
Location: Munich, Germany

Re: [RFC] Twig as our template engine

Post by Marc »

I'd say go for twig.

Oleg
Posts: 1150
Joined: Tue Feb 23, 2010 2:38 am
Contact:

Re: [RFC] Twig as our template engine

Post by Oleg »

Remember that the target audience for phpbb templates is non-technical users as much as developers.

User avatar
Pony99CA
Registered User
Posts: 986
Joined: Sun Feb 08, 2009 2:35 am
Location: Hollister, CA
Contact:

Re: [RFC] Twig as our template engine

Post by Pony99CA »

DavidIQ wrote:I'd go one way or the other, not support both. If the phpBB template syntax is as horrible as it's being made out to be here then go with Twig.
Sure, but provide a conversion tool that converts phpBB syntax to Twig so that MOD authors don't have to learn a new syntax if they don't want to. As Oleg said, we need to support non-technical users.

Steve
Silicon Valley Pocket PC (http://www.svpocketpc.com)
Creator of manage_bots and spoof_user (ask me)
Need hosting for a small forum with full cPanel & MySQL access? Contact me or PM me.

User avatar
DavidIQ
Customisations Team Leader
Customisations Team Leader
Posts: 1904
Joined: Thu Mar 02, 2006 4:29 pm
Location: Earth
Contact:

Re: [RFC] Twig as our template engine

Post by DavidIQ »

Pony99CA wrote:
DavidIQ wrote:I'd go one way or the other, not support both. If the phpBB template syntax is as horrible as it's being made out to be here then go with Twig.
Sure, but provide a conversion tool that converts phpBB syntax to Twig so that MOD authors don't have to learn a new syntax if they don't want to. As Oleg said, we need to support non-technical users.

Steve
The way this RFC was started seems to make non-technical users an afterthought. I suppose that as part of this RFC such a tool would need to be part of it.
Image

User avatar
callumacrae
Former Team Member
Posts: 1046
Joined: Tue Apr 27, 2010 9:37 am
Location: England
Contact:

Re: [RFC] Twig as our template engine

Post by callumacrae »

Pony99CA wrote:
DavidIQ wrote:I'd go one way or the other, not support both. If the phpBB template syntax is as horrible as it's being made out to be here then go with Twig.
Sure, but provide a conversion tool that converts phpBB syntax to Twig so that MOD authors don't have to learn a new syntax if they don't want to. As Oleg said, we need to support non-technical users.

Steve
Easy!
Made by developers, for developers!
My blog

User avatar
callumacrae
Former Team Member
Posts: 1046
Joined: Tue Apr 27, 2010 9:37 am
Location: England
Contact:

Re: [RFC] Twig as our template engine

Post by callumacrae »

DavidIQ wrote:
Pony99CA wrote:
DavidIQ wrote:I'd go one way or the other, not support both. If the phpBB template syntax is as horrible as it's being made out to be here then go with Twig.
Sure, but provide a conversion tool that converts phpBB syntax to Twig so that MOD authors don't have to learn a new syntax if they don't want to. As Oleg said, we need to support non-technical users.

Steve
The way this RFC was started seems to make non-technical users an afterthought. I suppose that as part of this RFC such a tool would need to be part of it.
Twig is much easier for non-technical users. It has readable documentation (unlike phpBB), thousands of people use it, an most IDEs support syntax highlighting for it.
Made by developers, for developers!
My blog

User avatar
DavidIQ
Customisations Team Leader
Customisations Team Leader
Posts: 1904
Joined: Thu Mar 02, 2006 4:29 pm
Location: Earth
Contact:

Re: [RFC] Twig as our template engine

Post by DavidIQ »

callumacrae wrote:It has readable documentation (unlike phpBB)
You saying you can't read these?
https://area51.phpbb.com/docs/30x/codin ... templating
https://wiki.phpbb.com/Tutorial.Template_syntax

Also you said that the phpBB template syntax is "disgusting". Can you provide a side-by-side comparison to support your claim? I'm just curious to see how much better it is since I've never used Twig.
Image

User avatar
Arty
Former Team Member
Posts: 985
Joined: Wed Mar 06, 2002 2:36 pm
Location: Mars
Contact:

Re: [RFC] Twig as our template engine

Post by Arty »

DavidIQ wrote:Also you said that the phpBB template syntax is "disgusting". Can you provide a side-by-side comparison to support your claim? I'm just curious to see how much better it is since I've never used Twig.
I quite like it and don't know why would someone say that its disgusting.

However, its outdated, not flexible enough and its custom. Twig is used in other applications, so users who work with other applications don't need to learn it. Twig is way more flexible than phpBB template system could ever be.

I haven't worked properly with twig to list all advantages. Advantages that I see so far:

Filters

Filters are very powerful. You can transform data as needed for specific situation.

For example, escape language variables when used inside JavaScript, which replaces phpBB's LA_* variables. In phpBB 3.0 function LA_* does that for language variables from language pack. However, it can't be used on other variables such as text generated by sprintf(), it can't be used on custom defined variables or anything else.

Examples:
  • {{ lang.ignore_post }} = {L_IGNORE_POST}
  • {{ lang.ignore_post|format(post.ignore_url) }} = currently not possible, variable is formatted in viewtopic.php instead and LA_* doesn't work for it.
  • {{ lang.ignore_post|e }} = {LA_IGNORE_POST}

Another example is usernames. Currently in phpBB whenever username should be displayed, such as poster's name in post, phpBB generates several variables: POST_AUTHOR_FULL, POST_AUTHOR, POST_AUTHOR_COLOR, U_POST_AUTHOR. Most of those variables aren't used, making those assignments useless. Set of custom filters can be created for usernames and instead of assigning multiple variables, phpBB can assign only 1 variable: user data. Filter will generate code that user wants, for example:
  • {{ post.author|profile('full') }} = <a href="{postrow.U_POST_AUTHOR}">{postrow.POST_AUTHOR}</a>
  • {{ post.author|profile('link') }} = {postrow.U_POST_AUTHOR}
  • {{ post.author|profile('avatar') }} = sample new filter that generates link to user's avatar

Template inheritance

This is main reason I want Twig to be added as soon as possible.

Unlike phpBB's template inheritance, author can overwrite small part of template in child template instead of copying whole template. It can also be used for template events.

Example:

Code: Select all

{% block header %}
code in main style!
{% endblock %}
{% block content %}
content in main style!
    {% block content_footer %}
    some nested block
    {% endblock %}
{% endblock %}
{% block footer %}
{% endblock %}

Code: Select all

{% extends "main.html" %}
{% block footer %}
this is a custom footer in child style. no other part of template was changed!
{% endblock %}

Code: Select all

{% extends "main.html" %}
{% block footer %}
{{ parent() }}
content appended at the end of footer by some extension! no other part of template was changed. this is replacement for events.
{% endblock %}

User avatar
MichaelC
Development Team
Development Team
Posts: 889
Joined: Thu Jan 28, 2010 6:29 pm

Re: [RFC] Twig as our template engine

Post by MichaelC »

And unlike the current implementation of template events, this allows modifying the template, not just adding to it.

But yes, the reasons Arty mentioned are the main ones (although there are many others) as well as wide-adoption; hence encouraging new contributors, better performance and no need to maintain our own templating engine.

Twig uses a more php similar syntax rather than re-inventing the wheel.
so for a loop, its not

Code: Select all

<!-- BEGIN posts -->
{posts.AUTHOR}

Code: Select all

        {% for post in posts %}
            <li>{{ post.author|e }}</li>
        {% endfor %}
Also, it doesn't use <!-- --> and { } instead it uses {% %} for control structure (does something e.g blocks), {{ }} for variables and {# #} for comments which get removed when parsing. Then any comments which you wish to remain in the template can use <!-- --> and not be confused with variables or control structure that wasn't passed etc.

You can also do things like:

Code: Select all

{% set foo = 'foo' %}
{% set foo = [1, 2] %}
{% set foo = {'foo': 'bar'} %}
or

Code: Select all

{{ foo['bar'] }}
{{ foo.bar }}
as you can access methods/properties of an object or items in an array

Languages would probably be fed in as an array or object so that syntax like Arty demonstrated can be used.
Formerly known as Unknown Bliss
psoTFX wrote: I went with Olympus because as I said to the teams ... "It's been one hell of a hill to climb"
No unsolicited PMs please except for quotes.

User avatar
callumacrae
Former Team Member
Posts: 1046
Joined: Tue Apr 27, 2010 9:37 am
Location: England
Contact:

Re: [RFC] Twig as our template engine

Post by callumacrae »

DavidIQ wrote:
callumacrae wrote:It has readable documentation (unlike phpBB)
You saying you can't read these?
https://area51.phpbb.com/docs/30x/codin ... templating
https://wiki.phpbb.com/Tutorial.Template_syntax
If I were a "non-technical user" (we were talking about them earlier), then no, not at all. It also describes itself as "very brief guide". Twig has huge amounts of documentation, and people have also written books on it if people don't like online documentation.
Also you said that the phpBB template syntax is "disgusting". Can you provide a side-by-side comparison to support your claim? I'm just curious to see how much better it is since I've never used Twig.
Will do later :-)
Made by developers, for developers!
My blog

Post Reply