gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 09:46:28 24 4
gpt4 key购买 nike

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

<小时/>

它适用于简单的模板:

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

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

然后我只需将渲染的输出粘贴在 div 中:

$('#dest').html(content);   
<小时/>

模板继承

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

来自 documentation :

Whenever jSmart come across either of template inclusion tags it calls jSmart.prototype.getTemplate() method and passes it a value of tag's file parameter. The method must return the template's text.

The default implementation of getTemplate() throws an exception. So, it is up to a jSmart user to override this method and provide template's text.

<小时/>

重写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调用的模板时的控制台输出:

<div class="row">

<label for="second" class="span4">Second Name:</label>

<input type="text" class="span4" placeholder="{$secondname}" id="second" />

</div>

<p>B;lsdsfasfsfds</p>
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
<form class="well">

<div class="row">

<label for="first" class="span4">First Name:</label>

<input type="text" class="span4" placeholder="{$firstname}" id="first" />

</div>

{block name=form_include}{/block}

<input type="submit" class="btn btn-inverse" />

</form>
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 检索的),继承就会起作用。

最佳答案

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

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;

关于javascript - jsmart - 无法获取模板内容(及时?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11739922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com