Twig extensions

General discussion of development ideas and the approaches taken in the 3.x branch of phpBB. The current feature release of phpBB 3 is 3.3/Proteus.
Forum rules
Please do not post support questions regarding installing, updating, or upgrading phpBB 3.3.x. If you need support for phpBB 3.3.x please visit the 3.3.x Support Forum on phpbb.com.

If you have questions regarding writing extensions please post in Extension Writers Discussion to receive proper guidance from our staff and community.
Post Reply
User avatar
mrgoldy
Former Team Member
Posts: 64
Joined: Fri Dec 18, 2015 9:41 pm
Location: The Netherlands
Contact:

Twig extensions

Post by mrgoldy »

Hello and good evening.

Thought I would just post here to ask / discuss about adding more twig extensions.
Not sure if there are huge benefits but I noticed when developing extensions I often have to listen to php events, purely to add one template variable, such as an auth check, config value, etc. It would be 'easier' to be able to do it straight from the template.
Currently there are three twig functions (\Twig_SimpleFunction) added, which are lang, lang_defined and get_class.
Also the \phpbb\user class¹ is added to the twig environment, so it's also possible to format dates in Twig with {{ user.format_date(UNIX) }}. And twig natively provides the functions to create paths (phpBB calls them routes), with {{ path() }}.

I was just wondering what are the down sides of adding more functions to this? Such as auth(), config(), get_username_string() and perhaps even phpbb_get_user_avatar(). All of these calls do not use queries or should've already been cached.

Perhaps I am thinking too much in Twig at the moment, and I understand there is a PHP side for a reason but it seems like it would save a whole lot of code in a lot of places. And give style authors perhaps more freedom, to add avatars at desired places without having to change the PHP side. (For example the last post column on viewforum).

I a willing to work on this and create the necessary code and PR, but first need to know if I am the only one thinking this way or perhaps overlooking major issues?

---

Possible functions list:
  • {{ config('variable') }}
    returns $config['variable']
  • {{ auth('auth'|['auth_1' , 'auth_2']|?, FORUM_ID) }}
    return $auth->acl_get('auth_1'), $auth->acl_gets(array('auth_1', 'auth_2')) or $auth->acl_getf('auth_1', $forum_id)
  • {{ username('mode', userrow|(user_id, username, user_colour, lang('CUSTOM_GUEST_NAME'), custom_profile_url)) }}
    return get_username_string('mode', $user_id, $username, $user_colour, $guest_username, $custom_profile_url)
  • {{ avatar(userrow) }}
    returns phpbb_get_user_avatar($user_row)

¹ the entire user class is added, with all its data variables, including the user's (hashed) password. not sure if that's ideal
Last edited by mrgoldy on Sun Dec 09, 2018 2:22 am, edited 1 time in total.
phpBB Studio Proud member of the Studio!

User avatar
GanstaZ
Registered User
Posts: 23
Joined: Wed Apr 11, 2018 4:58 pm

Re: Twig extensions

Post by GanstaZ »

Can't see any downsides, maybe only 1 if service will be heavily overloaded, but something like this can be solved. A lot of projects i have seen, use and develop new twig extensions just for the same purpose (to make life easier).

User avatar
mrgoldy
Former Team Member
Posts: 64
Joined: Fri Dec 18, 2015 9:41 pm
Location: The Netherlands
Contact:

Re: Twig extensions

Post by mrgoldy »

Do you mean phpBB extensions with those "projects"?
If so, could you link me to them and/or tell me which twig extensions they add, then I could include them in my list.
phpBB Studio Proud member of the Studio!

User avatar
GanstaZ
Registered User
Posts: 23
Joined: Wed Apr 11, 2018 4:58 pm

Re: Twig extensions

Post by GanstaZ »

Not exactly, just some random projects i have seen on github. Haven't seen many phpbb extensions that add functionality or simplicity through twig extension/s, but i know that some have tried to use(are using)/create twig extensions. I made one twig extension for one of my phpbb extensions just to learn something new and to test what can be done.

Villagran
Registered User
Posts: 1
Joined: Sat Dec 08, 2018 9:45 am

Re: Twig extensions

Post by Villagran »

GanstaZ wrote: Thu Dec 06, 2018 1:20 am Not exactly, just some healthy foods that boost testosterone and random projects i have seen on github. Haven't seen many phpbb extensions that add functionality or simplicity through twig extension/s, but i know that some have tried to use(are using)/create twig extensions. I made one twig extension for one of my phpbb extensions just to learn something new and to test what can be done.
Do you think making something a bit more elaborate and with more functionality is possible, GanstaZ?
Last edited by Villagran on Mon May 15, 2023 1:05 pm, edited 2 times in total.

User avatar
GanstaZ
Registered User
Posts: 23
Joined: Wed Apr 11, 2018 4:58 pm

Re: Twig extensions

Post by GanstaZ »

Everything is possible. If some functionality is not present (need some tweaks or simplified usage) in phpbb core at the moment and there are no prs on github (if there are some, those may be accepted or declined, only time will tell), that doesn't mean one can't make an extension to add same functionality that is expected/wanted from core.

User avatar
mrgoldy
Former Team Member
Posts: 64
Joined: Fri Dec 18, 2015 9:41 pm
Location: The Netherlands
Contact:

Re: Twig extensions

Post by mrgoldy »

I have created the related PR (https://github.com/phpbb/phpbb/pull/5480)
Unfortunately {{ auth() }} doesn't seem to be on the table at the moment, atleast not with my knowledge.
Feel free to suggest more functions, filters and tags.
Perhaps something like a switch() {} template tag..
phpBB Studio Proud member of the Studio!

Post Reply