Jquery jTemplates中的服务器代码块?可能的
我已经使用jTemplates有一段时间了,并试图用jQuery+jTemplates+Web服务完全构建一个站点。事情进展得非常顺利,直到我偶然发现这样一种情况,在这种情况下,我需要添加一些服务器控件,或者需要在模板渲染过程中使用代码块解析代码背后的一些数据 考虑以下jTemplates的精简版本:Jquery jTemplates中的服务器代码块?可能的,jquery,asp.net,jtemplates,Jquery,Asp.net,Jtemplates,我已经使用jTemplates有一段时间了,并试图用jQuery+jTemplates+Web服务完全构建一个站点。事情进展得非常顺利,直到我偶然发现这样一种情况,在这种情况下,我需要添加一些服务器控件,或者需要在模板渲染过程中使用代码块解析代码背后的一些数据 考虑以下jTemplates的精简版本: <script type="text/html" id="ContentTemplates"> {#template MAIN} {#foreach $T.Pr
<script type="text/html" id="ContentTemplates">
{#template MAIN}
{#foreach $T.Products as product}
<div id="div-product-{$T.product.Id}" class="product-wrapper">
{#include Content root=$T.product}
</div>
{#/for}
{#/template MAIN}
{#template Content}
<div class="view-container">
<p>Id: {$T.Id}</p>
<p>Desc: {$T.Desc}</p>
<p>Vendor Id: {$T.VendorId}</p>
<!-- Vendor name doesn't supplied by the web service,
it needs to be resolved using ASP.NET server code -->
<p>Vendor Name: <%= GetVendorName({$T.VendorId}) %></p>
<!-- i was hoping to do something like this,
but apparently it wouldn't work this way -->
</div>
{#/template Content}
</script>
{#模板主}
{#foreach$T.Products作为产品}
{#include Content root=$T.product}
{#/for}
{#/template MAIN}
{#模板内容}
Id:{$T.Id}
描述:{$T.Desc}
供应商Id:{$T.VendorId}
供应商名称:
{#/template Content}
现在我正为没能完成这么简单的事情而大发雷霆。。是不是我错过了什么?还是jTemplates真的应该只用于呈现简单的HTML布局?模板引擎是否能够解析服务器代码块?还是我只是命中注定 这就是我的结局:
<p>Vendor Name: <span class="vendor-name" vid="{$T.VendorId}"></span></p>
供应商名称:
我没有在呈现过程中尝试执行服务器代码(这是不可能的),而是在占位符
上呈现id,然后使用jQuery稍后注入值
// After template is rendered
$.each($(".vendor-name"), function(){
$this = $(this);
var vid = $this.attr("vid");
$this.append("<%= GetVendorName(" + vid + ") %>")
});
//呈现模板后
$.each($(“.vendor name”),函数(){
$this=$(this);
var vid=$this.attr(“vid”);
$this.append(“”)
});
哈奇,但至少它是有效的;)