Currently I’m looping through some results on an athlete’s bio page to get their individual win/loss record… Something like {% if matchWon is defined %}{% set wins = wins + 1 %}{% endif %} is looping in a table of their records. This works great, and will accumulate their record just fine, but what if I want to show the athlete’s record ​​*before*​​ the table where all that logic is run? Any way to display those final win/loss values out earlier in the dom?

  • Can you post a bit more of your template code? i.e. where you're retrieving your results and grabbing matchWon.
    – Brad Bell
    Dec 5 '15 at 22:55

Just do a {% set myEntries = craft.entries.whatever.find() %} to load the entries into an array. Then do two loops, the first to get whatever totals you want, the second to display the players stats.

Yes, you're doing two loops. But you're only hitting the database once to load into the array, which is where the performance bottleneck will be unless there are a ton of entries.

In any event, wrap it all in a cache tag.

  • Definitely thought I would be doubling my calls, looks to have no affect. This'll do! Dec 6 '15 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.