【问题标题】:Render handlebar helpers in jQuery在 jQuery 中渲染车把助手
【发布时间】:2019-02-03 07:52:02
【问题描述】:

我正在尝试使用 jquery 将 sn-p 附加到我的主模板(车把)中

var appendToDiv = function(data){
   return `
       <div class='some class'>
          {{{formatDate ${data.created_at} 'MMM DD YYYY'}}}
        </div>
    `
}

其中 formatDate 是车把助手。

$('#content').append(appendToDiv(data))

这里的问题是,车把助手没有被编译。希望问题足够清楚。任何帮助将不胜感激。提前致谢

【问题讨论】:

  • 您的意思是该函数将小胡子作为文本返回吗?在附加 html 文本之前,您肯定需要调用模板引擎。
  • 你是在前端还是后端使用车把
  • 前端的车把
  • @Artem,该函数应该返回一个我可以附加到模板中的 div 的字符串。 &lt;div class='someclass'&gt;May 02 2018 &lt;/div&gt;
  • @lacasera 是的,我明白了。但它会调用把手模板吗?

标签: jquery node.js express handlebars.js


【解决方案1】:

这就是我最终的方式。

在主模板文件index.handlebars,我创建了一个像这样的部分

<div id='content'></div>

<script id="template" type="text/x-handlebars-template">
    <div class="someclass">
       {{formatDate mydate 'MM DD YYYY'}}
    </div>
</script>

在js文件中,

var template = document.getElementById('template')
var view = Handlebars.compile(template)
var templateToDisplay = view(mydate)
$('#content').append(templateToDisplay)

希望其他人觉得这很有用。

【讨论】:

    猜你喜欢
    • 2014-04-20
    • 2012-07-21
    • 2020-04-02
    • 1970-01-01
    • 2013-02-05
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多