I have written a view helper where I need to compare the current index of the currently looped section to the total number of elements in that section's array.
There doesn't seem to be a way from within the view helper function to access the array representing the current section. There is a way, from the helper, to find out the currently iterated index within that array, but not the former. I've had to work around this by designing the helper to accept the section array as an argument to the function; this is kind of redundant and is prone to error. It should happen automatically. Here's my function:
/**
* Displays the given delimeter (or ", " if not provided) if currently iterated
* element within the section is not the last element.
* @param {array} section Array of nearest section to check index against
* @param {string} delimiter Delimiter to use between items
* @return {string} Delimiter if not at the last element
*/
helpers.sep = function(section, delimiter) {
delimiter = delimiter || ', ';
if ( this.ctx.$index < section.length - 1 ) {
return delimiter;
}
return "";
};
I would really like to be able to avoid having to pass this section argument. Helpers in other templating languages have convenient access to the array, but in my inspection of this (the only contextual object I know of from within the helper) I can't find that array referenced anywhere.
I have written a view helper where I need to compare the current index of the currently looped section to the total number of elements in that section's array.
Basically I'm trying to recreate something similar to Dust's
{@sep}
helper: https://github.com/linkedin/dustjs/wiki/Dust-Tutorial#wiki-sep__Separator_helper_Available_in_Dust_V10_releaseThere doesn't seem to be a way from within the view helper function to access the array representing the current section. There is a way, from the helper, to find out the currently iterated index within that array, but not the former. I've had to work around this by designing the helper to accept the section array as an argument to the function; this is kind of redundant and is prone to error. It should happen automatically. Here's my function:
I would really like to be able to avoid having to pass this
section
argument. Helpers in other templating languages have convenient access to the array, but in my inspection ofthis
(the only contextual object I know of from within the helper) I can't find that array referenced anywhere.