cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
krishnags
Responsive Resident
Responsive Resident

How to pass content snippet name dynamically

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

3 REPLIES 3
chleverenz
Super User
Super User

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

krishnags
Responsive Resident
Responsive Resident

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+"] %}
}



chleverenz
Super User
Super User

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

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Carousel News & Announcements 768460.png

What's New in the Community?

Check out the latest News & Events in the community!

MPP IDEAS updated 768x460.png

Ideas

Discover ideas and concepts from users like you for how to use Power Pages and take your work to the next level.

Users online (3,530)