We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Counting/switching inside template variables

Development and Programming

rschmitty's avatar
rschmitty
1 posts
14 years ago
rschmitty's avatar rschmitty

I’ve got 2 template variables that are tied to a 3rd party webservice (not channel/entries) and when I try {count} or {switch} it is always 1 or doesnt switch

{exp:mythirdparty:userinfo}
{first_name} {last_name}


{call_history}
{count} {call_date} {call_person}

{/call_history}


{transactions}
{count} {trans_date} {trans_amt}

{/transactions}

{/exp:mythirdparty:userinfo}

Every count is 1, which I’m assuming because there is only 1 userinfo record

Is it possible to count when looping in a nested tag or do you need to bake in your index/increment into the arrays

       
onewed's avatar
onewed
13 posts
14 years ago
onewed's avatar onewed

Bumping this because I’ve not yet found a good solution other than rolling my own {index} variable into my custom loops.

For instance:

{workbook_entries}
  <h3>{entry_title}</h3>
  {if entry_index < 2}
    {entry_body}
  {/if}
{/workbook_entries}

I’ve looked at the switch tag which doesn’t appear to be appropriate since it’s meant for rotating through values.

       
the3mus1can's avatar
the3mus1can
426 posts
14 years ago
the3mus1can's avatar the3mus1can

Are you using $this->EE->TMPL->parse_variables() in your addon?

http://ellislab.com/expressionengine/user-guide/development/usage/template.html#parsing_variables_pair_vars

       
onewed's avatar
onewed
13 posts
14 years ago
onewed's avatar onewed

We’re using parse_variables, I just realized though that the {count} and {total_results} is only generated for the outermost array of the template variables. We might reorganize some calls to alleviate this issue or make use of some {embed} calls.

For instance this doesn’t create {count} for the inner array ‘articles’

$vars = array(
  array(
    'section' => 'foo', 
    'articles' => array(
                    array('article_id' => 3, 'summary' => '...',
                    array('article_id' => 5, 'summary' => '...')
                  )
  ),

  array(.....)
);

template

{exp:our_call}
  Here are the articles for {section} 

  outer count: {count}  {!-- this is valid --}
  <ul>
  {articles}
    <li>{count}: {summary}</li>  {!-- this is NOT valid --}
  {/articles}
  </ul>

{/exp:our_call}

This seems potentially like a bug, considering the fact that the array loop {articles} is parsed and therefore no reason it can’t have its own {count}, like we did manually. I can see a problem with variable name collision, but could be worked around by incrementing the count and total_results variables, like using {count_1} in the {articles}..{/articles} loop.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.