looping through low variables
I'm having kind of a problem here with Low Variables.
Allow me to outline a bit of code here.
I have three Low Variables that control which news items are displayed on the homepage of a site:
- lv_homepage_nieuws_items: a select entries list tot select which items must be displayed onto the homepage
- lv_homepage_nieuws_count: a radio button variable for the total number of news items may be displayed on the homepage
- lv_homepage_nieuws_category : the category/categories from which news items may be selected if there can be displayed more news items (count) than news items selected
// check if there are news items specified for display
// but limit to the amount selected to display
{exp:low_variables:parse var="lv_homepage_nieuws_items" multiple="yes" limit="{lv_homepage_nieuws_count}"}
//force select these entries to display first
{exp:channel:entries channel="nieuws" entry_id="{lv_homepage_nieuws_items:data}" dynamic="false" status="open" show_expired="no" orderby="date" sort="DESC"}
<div class="headline">
<h5><a href="{url_title_path=nieuws/detail}" title="{title}">{title}</a></h5>
<span class="small">{entry_date format="%j %F %Y"}</span>
</div>
{/exp:channel:entries}
{/exp:low_variables:parse}
// Next, check if we have more news items to display
{exp:low_variables:parse var="lv_homepage_nieuws_items" multiple="yes" limit="{lv_homepage_nieuws_count}"}
{if lv_homepage_nieuws_count > lv_homepage_nieuws_items:total_results}
The entries-tag: do not select the items displayed already, use the selected categories only, limit to total amount allowed minus the already showed amount.
{exp:channel:entries channel="nieuws" entry_id="not {lv_homepage_nieuws_items}" category="{lv_homepage_nieuws_category}" dynamic="false" status="open" show_expired="no" limit="{lv_homepage_nieuws_count}-{lv_homepage_nieuws_items:total_results}"}
<div class="headline">
<h5><a href="{url_title_path=nieuws/detail}" title="{title}">{title}</a></h5>
<span class="small">{entry_date format="%j %F %Y"}</span>
</div>
{/exp:channel:entries}
{/if}
{/exp:low_variables:parse}
But here is the problem:
I have more items tot display, say 4 in total, minus 2 already displayed. I expected it to show only 2 extra items, but instead it showed those items two times, like in a loop.
What makes it go through this loop?
*edited the layout a bit ..
Replies
Low 22 Jul 2012 11:44
Hi Michel,
The main problem is you're trying to add a calculation as a parameter: limit="{lv_homepage_nieuws_count}-{lv_homepage_nieuws_items:total_results}" which is not going to work. You'll need something like MX Calculator to calculate the correct limit.
Untested, but maybe something like this: http://pastie.org/4299901
The trick is here to use the fixed_order parameter for the first channel:entries tag, making sure the entries selected in LV are in the right order. Then you use MX Calculator to calculate the {lv_homepage_nieuws_count}-{total_results}. The result is passed on to a low_variables:single tag, using the preparse:foo="bar" parameters available since v2.3.0 in Textarea variable types.
Inside that variable, which I named lv_snp_homepage_nieuws, you can add another channel:entries tag to generate the category news items.
elmystica 22 Jul 2012 12:19
Thanks for the fast reply!
I'm currently trying to implement it, but I'm a bit confused with the lv_snp_homepage_nieuws var. Is that just a dynamic var, or does it needs to be created in the admin as well?
I've implemented it so far, but I don't get the second series of items
{exp:channel:entries channel="nieuws" fixed_order="{lv_homepage_nieuws_items}" limit="{lv_homepage_nieuws_count}" status="open"}
a {title}<br />
{if count == total_results && total_results < lv_homepage_nieuws_count}
{exp:mx_calc expression="{lv_homepage_nieuws_count}-{total_results}" parse="inward"}
{exp:low_variables:single
var="lv_snp_homepage_nieuws"
preparse:limit="{calc_result}"
preparse:category="{lv_homepage_nieuws_category}"
preparse:entry_id="not {lv_homepage_nieuws_items}"
}
{/exp:low_variables:single}
{/exp:mx_calc}
{/if}
{/exp:channel:entries}
{exp:low_variables:parse var="lv_snp_homepage_nieuws" multiple="yes"}
{exp:channel:entries channel="nieuws" entry_id="{entry_id}" category="{category}" limit="{limit}" status="open"}
b {title}<br />
{/exp:channel:entries}
{/exp:low_variables:parse}
Low 22 Jul 2012 12:46
Take another look at the code I posted.
First of all, the low_variables:single tag doesn't need a closing tag.
Secondly, the lv_snp_homepage_nieuws should indeed be a variable created in the LV CP, and should contain the channel:entries tag. Just like a snippet.
The lv_homepage_nieuws_items, lv_homepage_nieuws_count and lv_homepage_nieuws_category should have Early Parsing enabled, the new doesn't need to in this scenario.
So, according to the my code example, the first channel:entries tag should be in your template, the second one should be in a LV, textarea type.
Does that make sense?
elmystica 22 Jul 2012 13:01
It does not only make sense, it also works!
I did have to enable early parsing, for some reason to make it work. And now my head gets enlightened with the power of LV
Thanks, rock on!