简体   繁体   English

在 jsrender 模板中定义一个变量

[英]define a variable inside a jsrender template

I need to keep a "colcounter" variable inside the loop that will be used to fill a jsrender template.我需要在循环中保留一个“colcounter”变量,用于填充 jsrender 模板。

Here is my template code这是我的模板代码

  <script id="datascapeTemplate" type="text/x-jsrender">

<div id="dsViewport">

    <div class="ds-column" style="width:{{:(name.length*100)}}px;">
        <h1 id="datascapeName">{{:name}}</h1>
        <div><span id="dsToggle">toggle</span></div>
    </div>
{{=colcounter}}
    {{for sections}}
    <div class="ds-section">

        <h3>{{:label}}</h3>
        <div class="ds-column" id="start">

        {{for items}}

            {{* if (colcounter > 4){ 
                colcounter = 1;
                }} 
        </div>
        <div class="ds-column" id="start">

            {{* } }}

            {{* 
            if ( data.selected || datascape.showInvisible) {  }}     
            <div class="ds-item {{* if (data.featured){ }} nowActive {{*} }} {{* if (data.active){ }} nowActiveRed {{*} }}"  background="{{:background}}" bgcolor="#000000" fgcolor="#FFFFFF">
                <div class="ds-item-container">
                    <h4>{{:title}}<br/>{{:time}}</h4>

                    <p><a item="{{:id}}" href="{{:url}}" class="itemLink">view file {{:colcounter}}</a></p>
                </div>
            </div>

            {{* colcounter++; }}

            {{* } }}

        {{/for}}
        </div>
        {{* colcounter=1; }}
        </div>
    {{/for}}
    {{* colcounter=1; }}
</div>


</script>

Unfortunately, it prints, on the very first iteration of the loop "Error: colcounter is not defined.".不幸的是,它在循环的第一次迭代中打印“错误:未定义 colcounter”。 Afterwards it works.之后它起作用了。

It seems the way i initialise my colcounter variable is not working but i fail to find the correct way.似乎我初始化 colcounter 变量的方式不起作用,但我找不到正确的方式。 var colcounter =0 does not work. var colcounter =0不起作用。

UPDATE更新

  • jsfiddle: http://jsfiddle.net/ZX6Mk/ jsfiddle: http://jsfiddle.net/ZX6Mk/
  • colcounter works now. colcounter 现在工作。 I declared it in the global scope. But I have an issue with datascape.showInvisible.我在全局 scope 中声明了它。但我对 datascape.showInvisible 有疑问。 It also triggers the error Error: Cannot read property 'showInvisible' of undefined .它还会触发错误Error: Cannot read property 'showInvisible' of undefined

Thank you for your time, a.谢谢你的时间,一个。

I took your fiddle and made a few changes.我拿走了你的小提琴并做了一些改动。 http://jsfiddle.net/johnpapa/bLSkz/ http://jsfiddle.net/johnpapa/bLSkz/

  1. The toggleButton was being referred to in jQuery without the #. jQuery 中提到的 toggleButton 没有#。 So I added that.List item, otherwise the click was not being captured.所以我添加了那个.List 项,否则没有捕获到点击。
  2. Your fiddle did not reference jQuery nor JsRender, though you were using both, so I added them.你的小提琴没有引用 jQuery 也没有 JsRender,虽然你同时使用了两者,所以我添加了它们。 (I assume you never ran the fiddle) (我假设你从来没有跑过小提琴)
  3. There was no datascape.showInvisible property, so I created one.没有 datascape.showInvisible 属性,所以我创建了一个。
  4. I passed showInvisible to the inner for loop using a parameter, so it could be accessed in its context.我使用参数将 showInvisible 传递给内部 for 循环,因此可以在其上下文中访问它。

     {{for sections ~showIt=showInvisible}} {{if (editorspick_amount > 0 || ~showIt)}}
  5. The template you were trying to render did not exist, so I changed the rendering code to use the script tag you created.您尝试呈现的模板不存在,因此我更改了呈现代码以使用您创建的脚本标签。 This also sets the allowCode=true, which is required to safely turn on the allowCode feature.这也设置了 allowCode=true,这是安全打开 allowCode 功能所必需的。

     $.templates("myTmpl", {markup: "#datascapeTemplate", allowCode: true }); $('#toggleButton').click(function(){ if(;rendered){ rendered = true. $("#datascape").html( $.render.myTmpl( datascape.json ) );show(); } });
  6. I changed one place where you used {{* }} to instead use an {{if}} block since there was no need to use allow code.我更改了一个您使用{{* }}的地方,改为使用{{if}}块,因为不需要使用允许代码。

This allowed all of the code to run and the template to render, though I admittedly did not follow all of what you were trying to do.这允许所有代码运行并呈现模板,尽管我承认我没有遵循你试图做的所有事情。

Hope this helps.希望这可以帮助。

One suggestion... the allowCode feature makes for really ugly templates and hard to maintain and read.一个建议...... allowCode 特性使得模板非常丑陋并且难以维护和阅读。 I highly recommend replacing it with helper functions (or other constructs).我强烈建议用辅助函数(或其他构造)替换它。 For example, you used allowCode to create the styling for some elements.例如,您使用 allowCode 为某些元素创建样式。 You could have used a custom tag for this instead, and moved the logic to javascript and simplified your template.您可以为此使用自定义标记,并将逻辑移至 javascript 并简化您的模板。 The colcounter could be moved to a helper function. It's just much more readable to move the logic to javascript, and keep the template/html clean.可以将 colcounter 移动到助手 function。将逻辑移动到 javascript 并保持模板/html 清洁更具可读性。 Just my 2 cents:)只是我的 2 美分:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM