hypejunction / hypetwig
Twig template integration
Requires
- php: >=5.5
- composer/installers: ~1.0
- twig/twig: ^2.4
This package is not auto-updated.
Last update: 2020-01-18 13:41:38 UTC
README
Adds support for twig templates in Elgg
Using Templates
To add a twig template, simply add a file with .twig
extension into your plugin's views, under default
viewtype.
<!-- my_plugins/views/default/test.twig --> <div class="hello">{{ Hello, {{ name }}</div>
To render a template use ``elgg_twig()`:
$user = elgg_get_logged_in_user_entity(); echo elgg_twig('test', ['name' => $user->getDispayName()];
You can override templates like any other view, but you can not extend them or filter them using hooks.
Development
To simplify development, set environment
config value to development
, otherwise you need to flush caches to reload template changes.
elgg_set_config('environment', 'development');
Globals
Globals available in templates
-
app
- application dataapp.user
- logged in user entityapp.site
- site entityapp.registrationUrl
- registration URLapp.loginUrl
- login URL
-
faker
- fake data generator (https://github.com/fzaninotto/Faker)
Functions
Function available in templates
echo()
- equivalent ofelgg_echo()
view()
- equivalent ofelgg_view()
assetUrl()
- equivalent ofelgg_get_simplecache_url()
requireJs()
- equivalent ofelgg_require_js()
formatHtml()
- equivalent ofelgg_format_html()
menu()
- equivalent ofelgg_view_menu()
Notes
I have initially taught elgg_view()
to render twig templates, but it had a negative
performance impact, likely because of a view_vars
hook registered for all views.
Until there is a wildcard hook registration, elgg_twig()
is the way to go,
however it does not allow view vars to be filtered or views to be extended, which in the end
is somewhat a win, because of simplicity.