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 / How to pass content sn...
Power Pages
Unanswered

How to pass content snippet name dynamically

(0) ShareShare
ReportReport
Posted on by 499

Hi All, I am trying to pass content snippet name dynamically but no luck. Please share your thoughts and inputs.

 

Eg: snippets["Label_Name"] - this way, I am able to read the snippet text. But I am reading "Label_Name" from back-end and need to pass that snippet name dynamically. Basically, I have already created the content snippets and stored those names in the CDS table and wanted to read them and load snippets dynamically. 

var locLblName = dataR[i]["name"];

snippets["+locLblName+"] --> This way it is not working

Categories:
  • Christian Leverenz Profile Picture
    1,214 on at

    Hi @krishnags ,

     

    the documentation on that one is a little bit misleading. In fact, i do it like this:

     

    {% assign mysnippetname = "this/is/my/fancy/snippetname" %}

    {% assign snippetvalue = snippets[mysnippetname] %}

     

    BUT 🙂 - this is liquid, which is executed on the server.

    I think, you want the snippet in a javascript variable. This requires a littbit more work. Use the upper code to get the snippet. Then add to your template:

    <script>

    var myjscontentsnippet = "{{ snippetvalue | strip_newlines | replace: '"', "&quote;" }}";

    </script>

    then you have the content in the js variable myjscontentsnippet. The strip_newlines is neccessary, because newlines would break the js code, the replacement of doublequotes to escaped version (you could also use '\"' as a replacement) is needed, because the doublequoted string would end there.

    Of course, you could also use single quotes as the outer quotes and replace singlequotes in your snippets.

     

    This is, how i do it. 

    Hopefully it fulfilles your needs as well.

      Have fun,

      Christian

  • SaiKrishnaGudluru Profile Picture
    499 on at

    Hi @chleverenz Thank you for your inputs on this, but I am trying to pass this/is/my/fancy/snippetname as a dynamic variable. I will try to provide more details for better understanding. 

    I have an array, which holds my content snippet names and looping to capture the content snippet name and reading the data as below. But as you mentioned different execution approaches blocking us to achieve this. So, as a work around, I am using switch in JS and hardcoded the snippet names and reading the values.

    for (var i = 0; i < locJSONFields.length; i++)
    {
     var eleName = locJSONFields[i]["name"];
     {% assign snip = snippets["+eleName+"] %}
    }



  • Christian Leverenz Profile Picture
    1,214 on at

    Hi @krishnags ,

    ah, good point. In this scenarios, i create a "webservice" (in fact a webtemplate which is assigned to a pagetemplate without header and footer). In this webtemplate, the snippet with name, which is passed in as a get paramter, is drawn.

    This is then completly dynamic. You could use this also via jQuery to insert complets html- subtrees 🙂

     

    May be this points in your requirement.

     

    Cheers,

      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 35

#2
M Bilal Khan Profile Picture

M Bilal Khan 33

#3
11manish Profile Picture

11manish 21 Super User 2026 Season 2

Last 30 days Overall leaderboard