In dustjs, can I put a template inside a template, where the inner template is defined in partial tag with a string literal?

StackOverflow https://stackoverflow.com/questions/20558182

  •  01-09-2022
  •  | 
  •  

Question

So I have an object like this {"templateName":"myTemplate","data":{"one":1}}

Here is my template:

{templateName}
{>"{templateName}":data/}

This does not render though (with no error message)...however, it works when I change it to this:

{templateName}
{>"myTemplate":data/}

It renders like this in the view:

myTemplate
[then here it shows myTemplate, rendered with data passed to it]

It renders perfectly and even shows the correct template name on top. I thought that putting the key in the quotes would work, but I guess I am misreading the dustjs guide. How can I accomplish this?

Was it helpful?

Solution

After doing a little digging, I believe I have found the problem. By using the syntax {>"{templateName}":data/}, you are changing the context from root to data. When this happens, templateName is no longer accessible when Dust tries to resolve the template name. So, Dust ends up searching for a template called "". I have filed an issue for this bug.

Having said that, if I were to write a book called "Dust: The Good Parts", I would leave contexts out of it (e.g. {#myData:myContext}). I have found that they cause more problems than they solve.

As a workaround, you can use this syntax:

{templateName} {>"{templateName}"/}

And then your "myTemplate" would need to do something like:

{data.one}

Here is an example of this working.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top