问题标签 [jquery-templates]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
5 回答
2510 浏览

ajax - Building HTML with templates versus building it in javascript?

I recently started making web apps, and while I make some stuff that works, I'm not sure of the best practices.

Up to now, I've mostly used the templating systems of Django, web.py and PHP. But now that I'm using jQuery and nice ajaxy magic to get data from the server without refreshing the client, I'm seeing the advantages of building the HTML directly in javascript (so I can just send a small json object to the client and have him figure out what to change and how).

So now I have some bits that are rendered with templates, and some that are built in javascript, and even one horrid case where there's a mix of both, a web.py template that generates a javascript function that creates a HTML table - feels like C macros all over again! (I'll refactor that away eventually)

Is this a common problem in web development? Any recommended best practices, such as "use json for everything, render as much as possible in javascript", "use library foo", etc.? Any good heuristics for what to handle with templates, and what to handle with javascript?

Searching a bit here, I found someone asking about javascript templates, which does seem like a possible solution.

0 投票
3 回答
2148 浏览

jqtouch - jQuery-mobile 不能很好地与 jQuery-tmpl 配合使用?

有什么理由为什么相同的模板和 JavaScript

将按照以下 jqtouch 标记的记录工作:

但导致模板在无序列表之外(之后)呈现,并带有以下 jquery-mobile 标记?

我意识到 jquery-mobile 处于 alpha 版本,但到目前为止它运行良好,我不想在这个阶段切换到 jqtouch。有没有人看到这种行为并找到解决方法?

0 投票
1 回答
1798 浏览

jquery - 通过 AJAX 加载部分 HTML 时,脚本标签中的 jQuery 模板消失

我对 jQuery tmpl 插件有疑问。

这是一个供您尝试的测试用例(因为 console.log 在没有 Firebug 的 Internet Explorer 上不起作用)。

一个文件 ajaxed_tpl.html:

测试.html:

这个加载很好 - 我收到警报并且模板 html() 看起来不错。

但我也得到了“我不需要通过 AJAX”这一行。所以很明显,我只需要加载 div id="some_inner" 的内部部分,而且我还需要执行 Javascript,所以唯一的方法是在获取 some_inner div 之前将数据插入 DOM。

所以让我们稍微修改一下 test.html:

哦,伙计,模板去哪儿了?为什么脚本标签中的 Javascript 被执行,但脚本标签中的模板不见了?

但是现在让我们修改 ajaxed_tpl.html 中的模板:

并再次运行 test.html。耶!这次 console.log 输出我们的模板!看来,如果它在 div 标签中,它不会被撕掉。

但有一个问题。如果我将模板放在 div 而不是脚本中,浏览器会搞砸我的

所以现在它看起来像:

即使我仅加载通过 AJAX 接收的部分数据,是否有任何方法可以正确加载模板(无需浏览器对那些 ${} 进行编码)?

为什么那些模板脚本标签被撕掉了,是谁在做的——浏览器还是 jQuery?

0 投票
1 回答
1049 浏览

jquery - 带有组的 jQuery 模板

我正在尝试使用具有组标题的 jQuery 模板插件创建模板。组标题事先不知道,因为它是数据驱动的。我的模板是:

我拥有的数据是:

我希望输出是

我注意到如何正确设置模板来实现这一点。

0 投票
2 回答
2013 浏览

jquery - 使用外部复合 jQuery 模板

在受到这 2 篇博文的启发后,我想尝试 jQuery 模板

好吧,这对我来说不太有效。如果我在页面本身上有模板代码,它可以正常工作,但远程加载对我不起作用。似乎正在检索模板。这里有什么问题?

外部模板:

在我的页面上,我使用此代码来检索和加载模板:


更新:

Encosia 上的一篇新博客文章解释了这个问题和答案......

http://encosia.com/2010/12/02/jquery-templates-composite-rendering-and-remote-loading/

0 投票
4 回答
16999 浏览

jquery - 如何使用 jQuery 模板格式化日期/时间?

我刚刚开始使用jQuery 模板作为我的 javascript 模板引擎。我的问题是,我如何格式化日期(从 ASP.NET Json ActionResult 返回)格式为:

我尝试执行以下操作:

注意上面使用了新的jquery全球化插件来添加$.format方法。另请注意,这{{= comment.DateCreated }}是长手的说法${comment.DateCreated}

如果您能提供帮助,我将不胜感激。

0 投票
1 回答
2303 浏览

jquery - 如何将数据附加到jquery tmpl

有谁知道如何将用于呈现 jQuery 模板的数据项附加到生成的 dom 对象?

给定这个tmpl:

还有这个 :

我想通过 .data('section', data) 将每个部分附加到结果中,以便稍后我可以访问所有数据,因为并非所有数据都被渲染,例如我需要掌握的 id 等稍后在执行删除之类的操作时。

也许像这样的东西......

但我不知道如何获取用于呈现此 tmpl 的当前数据项。

Perhpas 有办法将它包含在模板本身中吗?

非常感谢任何帮助。

0 投票
4 回答
1480 浏览

jquery - 如何从 jQuery tmpl 模板的 {{each}} 块中访问外部范围的变量?

例如,在这段代码中:

第一个${id}打印外部对象的id属性,但第二个${id}打印与${user.id}. 我想id{{each}}块内抓取外部范围。

这可能吗?或者我是否必须确保为变量命名,以免发生这样的冲突?

0 投票
3 回答
23775 浏览

jquery - 从外部文件加载 jQuery 模板?

我刚开始使用 jQuery 的模板引擎。到目前为止看起来相当不错。但是我想知道是否可以以某种方式从外部文件加载模板。想象一下有很多模板。这会弄乱 html 代码,并且也无法缓存,并且必须在每次请求时下载。

我希望有一种方法可以将它们全部定义在一个外部文件中,然后加载它们并将编译的模板存储到 localStorage 中。

有谁知道如何从外部文件加载它们?

0 投票
2 回答
6386 浏览

javascript - mustache.js 与 jquery-tmpl

我第一次看 javascript 模板,而 mustache 和 jquery-tmpl 是目前最有力的竞争者。

我的一些要求:

  • 模板将存在于单独的文件中,以包含在多个页面上
  • 所有(或几乎所有)数据将来自对返回 json 的 restful api 的调用
  • 我们是一家 java/eclipse 商店,所以语法突出显示和与之兼容会很好,如果这是一个问题的话

有人知道在速度、易用性、灵活性、稳定性方面有什么比较吗?还有其他我应该考虑的因素吗?其他顶级模板引擎?

(我知道关于这个一般主题还有其他 问题,但我没有看到这两者之间有任何直接、广泛的比较。)