Using Liquid and Web Roles together in Power Pages

Calum HarrisonCalum Harrison
2 min read

Do you want to only show or apply something in Power Pages to someone who has a particular web role in Liquid? {.lead}

You could use this technique to show some relevant text on a page or even use it as a extra layer of security. {.lead}

Example has_role

The has_role method allows you to check for a individual role. In the example below, it checks if the user logged in either has the "Manager" or "Director" web role. If they do, then it would show some text relevant to them.

{% if user %}

  {% assign manager = user | has_role: 'Manager' %}
  {% assign director = user | has_role: 'Director' %}

  {% if manager or director %}
    <p> Welcome to the management section... </p>
  {% endif %}

{% endif %}

Example contains

The contains method allows you to check for multiple roles. In the example below, it shows the management section to anyone who's web role contains "manager".

{% if user %}

  {% assign userRoles = user.roles | downcase %}

  {% if userRoles contains "manager" %}
    <p>Welcome to the management section.</p>
  {% endif %}

{% endif %}

Debug tip

Sometimes you will just want to see the web roles assigned to the user, to do this use the following code below.


<p>Roles for the user: {{ user.roles | join: ', ' }}</p>

Conclusion

To recap use has_role to check individual roles and use contains to check for multiple web roles. Web roles are a great way to control what a user can view on a web page thanks to Liquid and at the same time adds an extra layer of security. {.lead}

As always thanks for reading and take it easy! {.lead}

0
Subscribe to my newsletter

Read articles from Calum Harrison directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Calum Harrison
Calum Harrison