{_loop_var} not working?

Post

Posted
Rating:
#4337 (In Topic #903)
Joe
Avatar
Standard member
Joe is in the usergroup ‘Honoured member’
According to the documentation:

Code

{+START,LOOP,A\,B\,C\,D\,E\,F\,G\,H\,I}
  <div style="background: {$CYCLE,bgcol,#EEE,#AAA}">
    {_loop_var}
  </div>
{+END}

…_loop_var will display the current loop value for that (i.e. A).

Yet when I try to duplicate this, {_loop_var} just displays as text and doesn't return the current value. Am I missing something?

I'm trying to populate CPF values in an HTML table. My hope is that I can use loops to display a new table row once X amount of columns are shown, but I'd need to use something like {_loop_var} in an IF directive to check the value of X and start the new row if it exceeds a certain amount of columns.

I don't know if that'll work, but I'm certainly gonna try :thumbs: If anyone can chime in on a better solution to achieve this, I'm all ears.

To elaborate:
Let's assume my CPF has 10 values (1,2,3,4,etc..)

The table should display the values but not exceed 5 columns. The 6th loop should start a new row.

The documentation also states that the LOOP directive:
…also has support for automatic HTML table columnisation and sorting, as an advanced feature (very rarely used). If you want to use that check at the implementation in the PHP code to see how it works.

I wouldn't know where to look for that unfortunately :lol:
Online now: No Back to the top

Post

Posted
Rating:
#4338
Avatar
Site director
Chris Graham is in the usergroup ‘Administrators’
Hi,

I think you're running the code from Comcode. I think this is a little bug, it only working from templates, due to loop_key/loop_var being lower case or possibly by Comcode not supporting parameters (only symbols, directives, language string references). I'll see if I can get that fixed tomorrow by making a special exception for it.


Become a fan of Composr on Facebook or add me as a friend. Add me on on Mastodon. Follow me on Minds (where I am most active). Support me on Patreon

Was I helpful?
  • If not, please let us know how we can do better (please try and propose any bigger ideas in such a way that they are fundable and scalable).
  • If so, please let others know about Composr whenever you see the opportunity or support me on Patreon.
  • If my reply is too Vulcan or expressed too much in business-strategy terms, and not particularly personal, I apologise. As a company & project maintainer, time is very limited to me, so usually when I write a reply I try and make it generic advice to all readers. I'm also naturally a joined-up thinker, so I always express my thoughts in combined business and technical terms. I recognise not everyone likes that, don't let my Vulcan-thinking stop you enjoying Composr on fun personal projects.
  • If my response can inspire a community tutorial, that's a great way of giving back to the project as a user.
Online now: No Back to the top

Post

Posted
Rating:
#4339
Joe
Avatar
Standard member
Joe is in the usergroup ‘Honoured member’

Chris Graham said

I think you're running the code from Comcode…


That's correct. I added my Tempcode into a template and included it on the Comcode page to test it out.

This works, but is there a more professional way of writing this?

Code

<table style="width:100%">
  <tbody>
    <tr>
      {+START,LOOP,{$CPF_LIST,Background,\,,0}}
        <td>
          {_loop_key} - {_loop_var}<br />
          <a target="_blank" title="{!LINK_NEW_WINDOW*}" href="{$BASE_URL*}/images/user_bgs/{_loop_var}.jpg">
            <img style="border:2px solid #ff7f00;" src="{$BASE_URL*}/images/user_bgs/thumbs/{_loop_var}.png" height="73px" width="109px" alt="{_loop_var}.jpg" />
          </a>
        </td>
        {+START,IF,{$EQ,{_loop_key},4}}</tr><tr>{+END}
        {+START,IF,{$EQ,{_loop_key},9}}</tr><tr>{+END}
        {+START,IF,{$EQ,{_loop_key},14}}</tr><tr>{+END}
      {+END}
    </tr>
  </tbody>
</table>

I have to hard code all "manual checks" to see if {_loop_key} exceeds 5 (max # of columns in my table), in order to start a new row. If my CPF has 50 values, you could imagine how ugly this would make the template.
Online now: No Back to the top

Post

Posted
Rating:
#4340
Avatar
Site director
Chris Graham is in the usergroup ‘Administrators’
Good idea. I tried to fix Comcode to support _loop_key/_loop_var, but it is not possible without a lot of work, so I'm documenting it's not supported. The problem is that Comcode processed each bit of Tempcode in isolation because of how it is weaved in with Comcode - so even if I added parameter support to Comcode, it would not be able to bind in the loop values.

The way to detect your boundary conditions is using remainders.
Something like this:

Code

<table style="width:100%">
  <tbody>
    <tr>
      {+START,LOOP,{$CPF_LIST,Background,\,,0}}
        {+START,IF,{$AND,{$NEQ,{_loop_key},0},{$EQ,{$REM,{_loop_key},5},0}}}</tr><tr>{+END}
        <td>
          {_loop_key} - {_loop_var}<br />
          <a target="_blank" title="{!LINK_NEW_WINDOW*}" href="{$BASE_URL*}/images/user_bgs/{_loop_var}.jpg">
            <img style="border:2px solid #ff7f00;" src="{$BASE_URL*}/images/user_bgs/thumbs/{_loop_var}.png" height="73px" width="109px" alt="{_loop_var}.jpg" />
          </a>
        </td>
      {+END}
    </tr>
  </tbody>
</table>

I haven't tested that. The principle is that if the loop key divides cleanly into 5 (i.e. no remainder) then that means the start of a new row. The first row is handled as a special case, as there's no row to close and you started a new row manually.




Become a fan of Composr on Facebook or add me as a friend. Add me on on Mastodon. Follow me on Minds (where I am most active). Support me on Patreon

Was I helpful?
  • If not, please let us know how we can do better (please try and propose any bigger ideas in such a way that they are fundable and scalable).
  • If so, please let others know about Composr whenever you see the opportunity or support me on Patreon.
  • If my reply is too Vulcan or expressed too much in business-strategy terms, and not particularly personal, I apologise. As a company & project maintainer, time is very limited to me, so usually when I write a reply I try and make it generic advice to all readers. I'm also naturally a joined-up thinker, so I always express my thoughts in combined business and technical terms. I recognise not everyone likes that, don't let my Vulcan-thinking stop you enjoying Composr on fun personal projects.
  • If my response can inspire a community tutorial, that's a great way of giving back to the project as a user.
Online now: No Back to the top
1 guest and 0 members have just viewed this.

Statistics

Forum statistics:
  • 2,052 topics, 7,195 posts, 10,827 members
  • Our newest member is LowfareMart
Back to Top