Skip to content
Franco Mussopower pages
Raindrops hanging from a ceiling
Photograph · Wang Yifang / Unsplash
Guide No.005
TutorialLiquid

IF – a deep-dive into conditions and graceful degradation with Liquid for Power Pages

Why are if statements important? Conditions allow us to plan for different scenarios and cater for the inevitable data gaps.

Level
03CustomiseStage 3 of 4
Read
13 min
Includes
18 snippets
Written
Jan 2021

Why are if statements important? Conditions allow us to plan for different scenarios and cater for the inevitable data gaps. Here are some real examples I had to work around on the Spotify clone project:

They also come in handy for graceful degradation. One example was stopping the layout from breaking if a track had no artwork. I used if to swap in a placeholder image instead (see example below :))

Here’s how an if is written in liquid..

Liquid
{% if condition == 'met' %}
Do this
{% endif %}

You can also provide a ‘default’ or false option using else:

Liquid
{% if condition == 'met' %}
Do this
{% else %}
Do this other thing
{% endif %}

…and you can also use elsif to branch and test for other things like so:

Liquid
{% if condition1 == 'met' %}
Do this
{% elsif condition2 > 0 %}
Do this instead
{% else %}
Do this as a last resort
{% endif %}

A few gotchas from my experience

It’s worth getting familiar with the various operators available. Here are some of the basics:

Symbol / keywordMeaning
== Equal to
contains contains
!= Not equal to
>  Greater than
>= Greater than or equal to
<  Less than
<= Less than or equal to
empty Use to check whether an array or string is empty
or Combine multiple conditions with an OR relationship (one or more must be met)
and Combine multiple conditions with an AND relationship (all must be met)

How to use IF with the various Dataverse field types

Is a text field, lookup field, option set, date or numeric field populated?
Note that all we need is the name of the field. We don’t need to specify nil or an empty string

Liquid
{% if user.firstname %}

Is a text field over a certain length?

Liquid
{% if user.firstname.size > 5 %}

Is a text field equal to a certain value?

Liquid
{% if user.fullname == 'Franco Musso' %}

Does a text field contain a certain value?

Liquid
{% if user.fullname contains 'Franc' %}

Is a date field in the past (earlier than today’s date?)

Liquid
{% if createdon < now %}

Is a number field positive?

Liquid
{% if user.musdyn_age > 0 %}

Is a variable set?

Liquid
{% if thisAccount %}

Examples from the Spotify clone project

What better way to learn than by example? Here’s every bit of conditional code I used in my Spotify clone project:

If the visitor is a logged in user (show one web template if yes or another if not);

Liquid
{% if user %}
    {% include 'Dashboard' %}
{% else %}
    {% include 'Home Page Grid' %}
{% endif %}

If a GUID has been passed in the URL:

Liquid
{% if request.params.id %}

If your fetch XML query returned one or more results (in this example, the fetch XML query is named ‘playlist_query’):

Liquid
{% if playlist_query.results.entities %}

If the track has artwork, show the image. If not, display a placeholder image instead:

Liquid
          {% if result['track.musdyn_artworknoteguid'] %}
            <img src="/_entity/annotation/{{ result['track.musdyn_artworknoteguid'] }}" alt="Cover artwork for {{ result['track.musdyn_name'] }} by {{ result['track.musdyn_mainartist'].name }}">
          {% else %}
            <img src="https://songify3.powerappsportals.com/artwork-not-found.svg" alt="Generic music icon, cover artwork not found">
          {% endif %}

If a text field is populated

Liquid
{% if result.musdyn_artworkguid %}

If a text field from a related record is populated

Liquid
{% if result['track.musdyn_artworknoteguid'].size %}

If lookup field is populated

Liquid
{% if result.musdyn_featuring %}

If this record from query A also appears in query B…

This one took me a little longer to master. Here’s the requirement:

The template is listing all tracks on a given playlist (using a fetch XML query called playlist_track)

For each record we loop through in the playlist_tracks query, if the user has already liked the track, display a tick / checkmark icon. Otherwise, show a like button.

So we perform an additional fetch XML query to retrieve all tracks the logged in portal user has previously liked. This query is called liked_songs.

In the example below, we’re checking whether the GUID of the current track in the playlist_tracks query also appears in the liked_songs query:

Liquid
{% if liked_songs contains result.musdyn_trackid %}
   <span class="glyphicon glyphicon-ok" aria-hidden="true"></span>
{% else %}
   <a class="like-track-button" item-name="{{ result.musdyn_name }}" item-guid="{{ result.musdyn_trackid }}" item-entity="Track" href="#" title="Like Button"><span class="glyphicon glyphicon-heart" aria-hidden="true"></a></span>
{% endif %}
Go deeper · Power Pages Mastery

This guide is one technique. The course is the method.

The course covers this in Level 3 · Advanced customisation: “Liquid templating for dynamic components”, alongside “Transforming out-of-the-box lists”. A structured programme distilled from dozens of real portal launches — from messy stakeholder requirements to successful go-live.

  • 6 weeks
  • 3 levels
  • Live training + video lessons
Where this fits
  1. Level 1Foundations
  2. Level 2Extending the platform
  3. Level 3Advanced customisation
    • Liquid templating for dynamic components
    • Transforming out-of-the-box lists
Franco’s engaging teaching style made training a pivotal contribution to my professional growth. The walkthrough of a Power Pages site resembling Spotify left an indelible mark.
Bhargav Reddy — Solution Architect — TCS
Filed underLiquid

2 comments

  1. Kevin

    Fantastic content, well done!

    Is there anyway to get liquid to call an external api in power pages? It’s possible in shopify I believe but wonderings if so in power pages.

    1. Franco MussoAuthor

      Thanks Kevin 🙂
      We can’t make API calls with Liquid. I’d lean on JavaScript / jQuery with AJAX for that. We also have a native Power Automate connection due later this year that could aid with this.
      Even if it was possible with liquid, the struggle would be that once the page has loaded, the door is closed for anything liquid so we’d still need to use something like Js & AJAX to respond to user events / interactions and load / reload that liquid template

Leave a comment

Your email address won’t be published. Comments are moderated.

Consulting

Need a Power Pages build that editors love and brands survive?

I help teams design and build Power Pages sites with bespoke UX. Tell me about yours.

Search

Book a discovery call

30 minutes. Bring your trickiest Power Pages problem — I’ll tell you honestly whether I’m the right fit.