User Guides

Template Helpers

As well as the tag, functions and filters that Twig comes with out of the box, LexasCMS also defines some useful helpers to make building your templates as simple as possible.


Available Helpers

LexasCMS provides the following custom template helpers:


linkTo(linkText, routeName, [params...], [options])

To create a link to a route, it is recommended that you use the linkTo tag. The linkTo tag will output an <a> tag with a pre-filled href attribute linking to the specified route. The body of the tag will also be pre-filled with the contents of the linkText argument. If the link is pointing towards the route that is currently active, an active class will also be added to the <a> tag, this is useful for adding active states to links, for example in navigation menus.

Assuming a route named blog has been defined, the example below shows how you would use the linkTo helper to create a link to the blog route.

{{ linkTo("Blog", "blog") }}

{# Output: <a href="/blog/">Blog</a> #}

Using Dynamic Segments

If the defined route has any dynamic segments, these can also be passed to the linkTo helper to be inserted into the URL. For example, in order to link to a blog post, you would do the following:

{{ linkTo(blogPost.title.html(), "blog-post", blogPost.slug.html()) }}

{# Output: <a href="/blog/post/example-post-slug/">Example Post Title</a> #}

When passing a collection items slug into a linkTo, we can actually just pass the collection item itself, and the linkTo helper will be smart enough to go and fetch the collection items slug. See below:

{{ linkTo(blogPost.title.html(), "blog-post", blogPost) }}

{# Output: <a href="/blog/post/example-post-slug/">Example Post Title</a> #}

Using Classes

Classes can be added to the link using the class key of the options argument.

{{ linkTo(blogPost.title.html(), "blog-post", blogPost, { class: "blog-post-link" }) }}

{# Output: <a href="/blog/post/example-post-slug/" class="blog-post-link">Example Post Title</a> #}

Using Attributes

Any other attributes can be added to the link using the attrs key of the options argument.

{{ linkTo(blogPost.title.html(), "blog-post", blogPost, { attrs: { title: blogPost.title.html() } }) }}

{# Output: <a href="/blog/post/example-post-slug/" title="Example Post Title">Example Post Title</a> #}

Query Parameters

Finally, using the queryParams option, you can also define any query parameters that you would like to be added to the URL. This can be done like so:

{{ linkTo("Blog", "blog", { queryParams: { page: "2" } }) }}

{# Output: <a href="/blog/?page=2">Blog</a> #}

Locale

By default, the linkTo helper will target the websites current locale. Should you want to change this, you can pass the locale option to set the locale for the generated link.

{{ linkTo("Blog", "blog", { locale: "fr" }) }}

{# Output: <a href="/fr/blog/">Blog</a> #}

routeActive(routeName, [params...])

The routeActive tag allows you to check if the specified route is currently active from within your templates. Please note, that the routeActive tag disregards any query parameters.

If can be used as follows:

{% if routeActive("blog") %}
  {# Do something if this is the blog route #}
{% endif %}

{# /blog/ === true #}
{# /blog/?page=2 === true #}
{# /blog/?page=2&sort=date === true #}
{# /contact/ === false #}

With Dynamic Segments

The routeActive helper also understands dynamic segments, and can be used to check if a particular variant of a route is active. For example:

{% if routeActive("blog-post", "example-post-slug") %}
  {# Do something if this is the route for the blog post with a slug of 'example-post-slug' #}
{% endif %}

{# /blog/post/example-post-slug/ === true #}
{# /blog/post/another-example-post-slug/ === false #}

routeHref(routeName, [params...], [options])

The routeHref helper allows you to retrieve the URL of the specified route. See the following example:

{{ routeHref("blog") }}

{# Output: /blog/ #}

With Dynamic Segments

The routeHref helper can also be used to return the URLs of routes that contain dynamic segments.

{{ routeHref("blog-post", "example-post-slug") }}

{# Output: /blog/post/example-post-slug/ #}

With Query Parameters

Query parameters can be passed to the routeHref tag in order to have them appended onto the end of the routes URL.

{{ routeHref("blog", { queryParams: { page: "2" } }) }}

{# Output: /blog/?page=2 #}

Locale

By default, the routeHref helper will generate links using the websites current locale. Should you want to change this, you can pass the locale option to set the locale for the generated link.

{{ routeHref("blog", { locale: "fr" }) }}

{# Output: /fr/blog/ #}

translate(key, [context], [locale]) (alias: t)

The translate helper allows you to access your websites configured static translations based on the websites current locale.

{{ translate("hello_world") }}

{# English Output: Hello World! #}
{# French Output: Bonjour le monde! #}

With Context

You can provide additional context to the translate helper, allowing variables to be passed into the string.

{{ translate("hello_user", { name: "Steven" }) }}

{# English Output: Hello Steven! #}
{# French Output: Bonjour Steven! #}

Override Locale

You can override the websites current locale settings by providing a specific locale to the translate helper.

{{ translate("hello_user", { name: "Steven" }, "fr") }}

{# Output: Bonjour Steven! #}

Alias

The translate helper also has a t alias.

{{ t("hello_world") }}

{# English Output: Hello World! #}
{# French Output: Bonjour le monde! #}