0

我一直在使用 jTemplates,并尝试完全使用 jQuery + jTemplates + Web Services 来构建一个站点。事情真的很好,直到我偶然发现这样的情况,我需要添加一些服务器控件或需要在模板渲染过程中使用代码块 <%= DoSomething(param) %> 从后面的代码中解析一些数据.

考虑以下 jTemplates 的精简版本:

<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>

现在我因为未能实现如此简单的事情而将头撞到墙上……这是我错过的事情吗?还是 jTemplates 真的应该只用于呈现简单的 HTML 布局?模板引擎是否能够解析服务器代码块?还是我只是注定要失败?

4

1 回答 1

0

这就是我最终得到的:

<p>Vendor Name: <span class="vendor-name" vid="{$T.VendorId}"></span></p>

我没有尝试在渲染过程中执行服务器代码(这是不可能的),而是在占位符上渲染 id <span>,然后稍后使用 jQuery 注入值。

// After template is rendered
$.each($(".vendor-name"), function(){
    $this = $(this);
    var vid = $this.attr("vid");
    $this.append("<%= GetVendorName(" + vid + ") %>")
});

哈基,但至少它有效;)

于 2012-05-04T06:31:42.467 回答