Power Pages: conditional display, site markers, and Liquid filters

Nick DoelmanDyn365CE9 months ago9 Views

Using Liquid in Power Pages allows you to create code to conditionally display both static and dynamic content based on certain conditions. A very simple example of this would be this piece of Liquid code that you can place in the source of a web page, web template, or a content snippet:

{% if user %}
  Hello {{ user.fullname }}
{% else %}
  Hello stranger
{% endif %} 

The above code checks to see if a user is logged in, if so, show a warm greeting, if not, greet the stranger.

More of video person? Check out the YouTube version of this post here:

Conditional display of an entire web page

An interesting use case came up the other day in the project I am working on with my BOOST podcast partner and bff Ulrikke Akerbæk.

We wanted to display a different home page depending if the user was logged in to the website or not.

While we could easily use Liquid to display different content depending if the user was logged in or not, we also have a goal to ensure that the web site admins would be able to use the design studio to add/update/maintain static content on the home page. Once we add conditional Liquid code to a webpage to manage it all, it becomes much more complicated.

Site markers to the rescue

Site markers are an obscure feature of Power Pages that don’t get the attention or recognition they deserve, there wasn’t even documentation until I wrote the topic while I was working at Microsoft. Site markers are references to web pages, a common use case is to use site markers for hyperlinks on a webpage or web template, and if the corresponding web page moves to a different place in the website site map, the hyperlinks will continue to work.

For example, here we can create a site marker pointing to specific page:

And then we can put Liquid code on web page, web template or content snippet:

<!-- Example of Site Markers -->
{% assign homepage_marker = sitemarkers["HomeAuth"] %}
<p><a href="{{homepage_marker.url}}">Home Page Authorized User</a></p>

Ulrikke pointed out to me that you can also refer to the actual web page copy via the site marker as well! So by placing the following code on an existing web page, web template or content snippet, you can then display that pages content on a different webpage:

{{ sitemarkers["HomeAuth"].adx_copy }}

Note about enhanced data model: You might be tempted to use .mspp_copy as the field name if you are using the enhanced data model, but actually .adx_copy continues to work in this context.

So what we can now do is setup our different web pages in the design studio, (notice these are not added to the main navigation) and future website admins can use the design studio to update and maintain the content on these pages;

And then configure corresponding site markers using the Power Pages management app:

Taking that one step further, we can then add conditional logic on the home page to display different web page content depending if a user is logged in or not:

{% if user %} 
   {{ sitemarkers["HomeAuth"].adx_copy }} 
{% else %} 
   {{ sitemarkers["HomeAnon"].adx_copy }} 
{% endif %}

And the corresponding web page will display on the home page.

What about Liquid! Doh!

This was all good until we added some Liquid code to the content pages.

Using the {{ sitemarkers["HomeAuth"].adx_copy }} method, the Liquid code on the content pages DID NOT render!

This really was the core of the issue and really what started the conversation, how to render embedded content that had Liquid code?

After some trial and error… it wasn’t until I read to the end of a certain docs topic that I found the liquid string filter!

{{ sitemarkers["HomeAuth"].adx_copy | liquid }}

Adding the “| liquid” filter to the end of the site marker code then rendered the Liquid code successfully, and we learned something new!

Final home page code:

{% if user %} 
  {{ sitemarkers["HomeAuth"].adx_copy | liquid }} 
{% else %} 
  {{ sitemarkers["HomeAnon"].adx_copy | liquid }} 
{% endif %}

And the page rendered correctly:

Summary

This might be an obscure use case, but it goes to show that there are a lot of hidden and interesting ways to use existing and not well known Power Pages features like site markers, liquid and filters. I hope this post does help someone in a similiar situation.

Interested in more tips and tricks like this one? Join Ulrikke and I at the European Power Platform Conference for our session Boost your Power Pages with these top 10 tips and tricks! and register using the discount code “BOOST”.

Nick Doelman is blue-haired Microsoft MVP, podcaster, trainer, public speaker, competitive Powerlifter and really a decent guy. Follow Nick on X at @readyxrm or LinkedIN. Listen or watch the the Power Platofrm Boost podcast every second week for news and updates from the Power Platform community.

Original Post https://readyxrm.blog/2024/05/09/power-pages-conditional-display-site-markers-and-liquid-filters/

0 Votes: 0 Upvotes, 0 Downvotes (0 Points)

Leave a reply

Follow
Sign In/Sign Up Sidebar Search
Popular Now
Loading

Signing-in 3 seconds...

Signing-up 3 seconds...