Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7757009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:58:17+00:00 2026-06-01T12:58:17+00:00

I need to keep a colcounter variable inside the loop that will be used

  • 0

I need to keep a “colcounter” variable inside the loop that will be used to fill a jsrender template.

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.”. Afterwards it works.

It seems the way i initialise my colcounter variable is not working but i fail to find the correct way. var colcounter =0 does not work.

UPDATE

  • jsfiddle: http://jsfiddle.net/ZX6Mk/
  • colcounter works now. I declared it in the global scope. But I have an issue with datascape.showInvisible. It also triggers the error
    Error: Cannot read property 'showInvisible' of undefined.

Thank you for your time,
a.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:58:19+00:00Added an answer on June 1, 2026 at 12:58 pm

    I took your fiddle and made a few changes. http://jsfiddle.net/johnpapa/bLSkz/

    1. The toggleButton was being referred to in jQuery without the #. So I added that.List item, otherwise the click was not being captured.
    2. Your fiddle did not reference jQuery nor JsRender, though you were using both, so I added them. (I assume you never ran the fiddle)
    3. There was no datascape.showInvisible property, so I created one.
    4. I passed showInvisible to the inner for loop using a parameter, so it could be accessed in its context.

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

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

    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. I highly recommend replacing it with helper functions (or other constructs). For example, you used allowCode to create the styling for some elements. You could have used a custom tag for this instead, and moved the logic to javascript and simplified your template. 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. Just my 2 cents 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've made a simple bash script that need to keep it's super-user privileges throughout
I need to keep a list of user_id s that have viewed a piece
I need to keep track of indexes/rows that are selected. When a row is
If I need to keep track of a key or token that identifies a
In my application I need to keep track of a list of objects that
I need to keep images inside a second level folder in res and access
I need to keep a table in sync with a recursive CTE query that
I need to keep access settings to different web pages inside of my asp.net
I need to keep track of a bunch of objects that can be identified
I need to keep a control value (possibly inside a file), in order to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.