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)) }}
returnget_username_string('mode', $user_id, $username, $user_colour, $guest_username, $custom_profile_url)
{{ avatar(userrow) }}
returnsphpbb_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