jsmart – 无法获取模板内容(及时?)

我正在尝试使用jsmart在客户端渲染Smarty 3模板。 如果您对它们没有经验,请继续阅读,因为它可能只是我正在制作的一个简单的JavaScript错误。


它适用于简单的模板:

我根据文档创建模板(我通过AJAX接收),然后渲染它(传递数据):

var template = new jSmart(templateReceivedViaAJAX); var content = template.fetch({"firstname":"adam", "secondname":"lynch"}); 

然后我只是将渲染的输出粘贴在div

 $('#dest').html(content); 

模板inheritance

尝试渲染包含includeextends等的模板时出现问题。

从文档 :

每当jSmart遇到模板包含标记时,它都会调用jSmart.prototype.getTemplate()方法并向其传递tag的file参数值。 该方法必须返回模板的文本。

getTemplate()的默认实现会引发exception。 因此,jSmart用户可以覆盖此方法并提供模板文本。


getTemplate()函数:

 jSmart.prototype.getTemplate = function(name) { $.ajax({type: 'GET', url: name, async:false, success: function(data) { console.log('got template at '+name+'. The following is the contents:'); console.debug(data); return data; }}); } 

呈现包含对子模板的include调用的模板时的控制台输出:

 

B;lsdsfasfsfds

Uncaught Error: No template for /bundles/templatedemo/templates/form_include.html.smarty

呈现包含对模板的extend调用的模板时的控制台输出:

 got template at /bundles/templatedemo/templates/form.html.smarty. The following is the contents: templates:58 
{block name=form_include}{/block}
Uncaught Error: No template for /bundles/templatedemo/templates/form.html.smarty (expanded:) S (anonymous function) jSmart.fetch (anonymous function) f.event.dispatch f.event.add.h.handle.i

编辑:

如果模板内容预先存在(如果它们是硬编码的,而不是通过AJAX检索),则inheritance有效。

使用函数指针而不是匿名函数:

 function foo() { $.ajax({type: 'GET', url: foo.url, async:false, success: bar}); } function bar(data) { console.log(['got template at', foo.url, 'The following is the contents:']); console.debug(data); return data; } foo.url = "http://www.stackoverflow.com"; jSmart.prototype.getTemplate = foo;