Repeat tag

From The Smartest Wiki
Jump to: navigation, search

The <?sm:repeat:?><?sm:/repeat:?> tag is one of the main tools for retrieving and formatting content in Smartest. It can be used to display lists of items, pages, files or users. In coding terms, the repeat tag is highly similar to a foreach loop. Smartest also provides a foreach tag for basic array looping, but the repeat tag adds a lot of functionality.

Attributes

  • from - required. Determines the source data for the loop, in other words, what is being looped? This can be a string, the name of a data set, or with a gallery: prefix, the name of a file gallery, or with a pagegroup: prefix, the name of a page group, or with a tag: prefix, to retrieve all elements tagged with a given tag, the name of a tag. It can also be 'authors' or 'users' for a list of all authors on the website. Objects are also accepted: SmartestCmsItemSet object, a SmartestArray object, a file group or file gallery as a SmartestAssetGroup object, a SmartestPageGroup object. Finally, ordinary PHP arrays are also accepted. See examples below for more on the from="" attribute.
  • item - optional. This attribute names the repeated item object variable, so item="blog_post" means you can then access $blog_post for each item in the loop.
  • limit - optional. Stop after how many repeats? The default is 0, which means repeat until the end of the data.
  • char_limit - optional. An alternative way to limit the loop, you can specify a character limit here, and the names of the repeated items will be concatenated until the limit is reached. This is good if you are trying to keep columns balanced and need the number of items to be more if their titles are shorter.

Basic variables that are always present

  • $key - the counter for the loop. Starts at zero and increments for each time the loop repeats. A custom key name can also be provided with the key="" attribute, but $key will also be set, regardless.
  • $iteration - the same as $key, but starts at 1 instead of zero. This is often more useful for user-facing applications.
  • $next_key - If there is another item still to come in the loop, this variable will hold its key - usually $key+1; This avoids having to do any math in the template itself.
  • $last_key - If this is not the first item in the loop, this variable will hold the key of the previous item - usually $key-1; This avoids having to do any math in the template itself.
  • $is_first - A straightforward boolean to allow the template to know whether the current loop is the first time the loop has run.
  • $is_last - A straightforward boolean to allow the template to know whether the current loop is the last time the loop will run.
  • Repeated item itself. If you are looping through items, this will be $repeated_item. Through pages, it will be $repeated_page, and through files it is $repeated_asset. In all cases, this can be overridden with the item="" attribute.

Repeating through data sets

Repeating through file galleries

Unlike ordinary file groups, file galleries are designed for presentation on the front end of your website. They allow ordering, and each member of the group can be captioned. For more on file galleries, click here