I'm using dust.js to do a client side templating. I would like to use a javascript function in my template, the function would get it's argument during templating i.e

Ex:

mytemplate = " <span> Hi getName({id}) </span>"
myjson = { id : 1 }

In this case, both the template and json-data are sent from server and templating happens on client side.

In the above example, I would get the 'id' from json data and want to display the name of user corresponding to that id.

I'm new to templating. I would like to know how this can be done using dust.js.

Thank you :)

有帮助吗?

解决方案

This can be done with Dust.js by creating a script block within your template:

{! Dust template !}
<script type="text/javascript">
  var userName = getName('{id|s|J');
  // Do whatever you want with the username
</script>

Note, the |s|j, which is important for security filtering.

In your particular use case, however, it would probably be better to just send the user's name and id in the JSON:

{! Dust template !}
  <span id="user-{id}"> Hi {name} </span>

// JSON
{
  id: 1,
  name: smfoote
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top