web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Pages / Variable from Javascri...
Power Pages
Answered

Variable from Javascript to Liquid

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hi All,

I have a Web Page with HTML and Javascript code that show a dropdown item, i need to execute various Fetch XML query on entities that depends on dropdown item selected.

How it's possible to write an IF-ELSE statement in Liquid based on the previous selection?

I've made some research, but I don't found anything that explain that.

 

For example:

I have this:

<script> var JSVariable = 5; </script>

 

and I want to do this

{% assign LiquidVariable = --JSVariable-- %}

 

Thank's in advance

Categories:
  • Verified answer
    - Matt Bayes - Profile Picture
    91 on at

    Hi Aleloberto,

     

    Unfortunately based on your requirement you are trying to reference client-side code via a server-side language, which is a no-go. You are able to go the other way (surface server-level data through Javascript) but in your case it's just not possible.

     

    The only way I can come to think of achieving the functionality you want would be to build a JSON service via a Web Template that you can pass a query string parameter to (your drop-down value). This Web Template can take the query string parameter, do some liquid formatting, and return whatever you need. Since you are looking to utilize fetchxml queries this would be a good solution.

     

    A helpful blog can be found below to achieve what you are looking for:

    https://colinvermander.com/2017/04/17/dynamics-365-portals-use-liquid-to-return-json-or-xml/

     

    If you have any questions please let me know!

    Thanks!

     

    Matt Bayes

  • Christian Leverenz Profile Picture
    1,214 on at

    Hi together,

    well, in fact you can do it, but it is really dirty 🙂 :

    {% assign hugo = 42 %}
    <script language="javascript">
    alert("{{ hugo }}");
    </script>
    This works but it mixes javascript and serversidecode.
    The advantage of this is, that this executes really fast on the server. So, when i need autocompletionlists, i usually make a fetch in the template and render a function which just returns a static json, aht is rendered on the server via liquid.
    As i said: Pretty ugly but pretty fast 🙂
    Greetings,
      Christian

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Pages

#1
Mohsin Ali Profile Picture

Mohsin Ali 20

#2
11manish Profile Picture

11manish 18 Super User 2026 Season 2

#3
DianePana Profile Picture

DianePana 9

Last 30 days Overall leaderboard