0

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 :)

4

1 に答える 1

2

これは、テンプレート内にスクリプト ブロックを作成することで、Dust.js で実行できます。

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

|s|jセキュリティ フィルタリングにとって重要な に注意してください。

ただし、特定のユース ケースでは、JSON でユーザーの名前と ID を送信する方がよいでしょう。

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

// JSON
{
  id: 1,
  name: smfoote
}
于 2013-06-04T12:47:31.997 に答える