Built-in Template Events

Finch injects a set of built-in variables and helper functions into every Jinja template. These are accessible via special prefixes alongside your own addParam() data.

Global Variables

{{ isLocalDebug }}   {# true when running in debug mode #}
{{ data }}          {# map of all params added via rq.addParam() #}
{{ session }}       {# server-side session map #}
{{ $rq }}           {# the Request object #}

Asset Helpers

{{ assets.js() }}      {# renders all <script> tags added via rq.addAsset() #}
{{ assets.css() }}     {# renders all <link> tags #}
{{ assets.dataJs() }}  {# renders data attributes for JS access #}

Route and URL Helpers ($e)

{{ $e.route }}                              {# current route key (e.g. 'root.panel') #}
{{ $e.routePath }}                          {# full resolved route path #}
{{ $e.routeKey }}                           {# same as $e.route #}
{{ $e.isKey('root.panel') }}                {# true if current route matches key #}
{{ $e.hasKey(['root.panel', 'root.form']) }} {# true if current route is one of the keys #}

{{ $e.routeUrl('key') }}                           {# URL for a named route #}
{{ $e.routeUrl('users.show', {'id': '42'}) }}       {# URL with path params #}
{{ $e.routeUrl('search', {}, {'q': 'hello'}) }}     {# URL with query params #}

{{ $e.uri }}            {# current Uri object #}
{{ $e.uriString }}      {# current URI as string #}
{{ $e.path }}           {# current path segments #}
{{ $e.pathString }}     {# current path as string #}
{{ $e.isPath('/example/form') }}  {# true if current path matches #}
{{ $e.endpoint }}       {# the matched endpoint path #}

{{ $e.url('/about') }}                    {# build an absolute URL from a path #}
{{ $e.urlParam('/users', {'id': '5'}) }}   {# URL with appended params #}
{{ $e.urlToLanguage('fa') }}              {# current URL switched to another language #}
{{ $e.getCookie('theme', 'light') }}  {# read a cookie with default value #}

Language Helpers

{{ $e.ln }}            {# current language code, e.g. 'en' #}
{{ $e.dir }}           {# language direction: 'ltr' or 'rtl' #}
{{ $e.langs }}         {# list of all configured languages: [{code, label, country}] #}

Utility Helpers

{{ $e.widgetPath('partials/nav') }}   {# full path to a widget file #}
{{ $e.randomString(8) }}             {# random string of 8 characters #}
{{ $e.toString(value) }}             {# coerce any value to string #}

Translation

{{ $t('logo.title') }}                          {# translate a key #}
{{ $t('greeting', {'name': user.name}) }}       {# translate with parameters #}

Nested Data Navigation

{{ $n('user/address/city', 'Unknown') }}  {# navigate nested params safely #}

Debug Dump

{{ dump(data) }}   {# dump any variable visually in the browser (debug only) #}

Custom Local Events

Define your own global functions in app.dart using Request.localEvents:

Request.localEvents.addAll({
  'currentYear': () => DateTime.now().year,
  'appName': () => 'My App',
});

Access them in templates with the $l. prefix:

<footer>© {{ $l.currentYear() }} {{ $l.appName() }}</footer>