Sorting and Looping Through Objects/Arrays

You may be passing some data into DocuMerge that is an array or object. On occasion, the order in which your objects and arrays are presented will not fit the need of your logic or output in document templates. We provide methods to sort by different keys and whether to sort ascending or descending. Below…

Image Description

You may be passing some data into DocuMerge that is an array or object. On occasion, the order in which your objects and arrays are presented will not fit the need of your logic or output in document templates. We provide methods to sort by different keys and whether to sort ascending or descending. Below we’ll review the process of sorting and looping through objects/arrays. And for more on using looping through arrays with the Smarty template engine, see: https://www.smarty.net/docsv2/en/language.function.foreach.tpl

Your incoming product data may look like this in JSON:

"products": [
    {
      "id": 1,
      "name": "Samsung Universe 9",
      "price": 1099,
      "stock": 53
    },
    {
      "id": 2,
      "name": "iPhone X",
      "price": 899,
      "stock": 37
    }
]

… or this as an HTTPS POST of name/value pairs:

products[0][id]=1&products[0][name]=Samsung%20Universe%209&products[0][stock]=53&products[0][price]=1099&products[1][id]=2&products[1][name]=iPhone%20X&products[1][stock]=37&products[1][price]=899


SORTING A LIST

To sort your products by Name, you’d use the |sort Modifier.

{foreach from=$products|sort:"name" item=_row}${$_row.price|number_format:2} {$_row.name}
{/foreach}


…would output…
$899.00 iPhone X
$1,099.00 Samsung Universe 9


To add an ordering or ascending or descending on the sort, you’d append a |sort Modifier variable.
:”asc” or :”desc”.

{foreach from=$products|sort:"name":"desc" item=_row}${$_row.price|number_format:2} {$_row.name}
{/foreach}

…would output…
$1,099.00 Samsung Universe 9
$899.00 iPhone X


Within a table, using HTML in the custom document template code editor, could look something like this:

<table>
<tr><td>Price</td><td>Name{foreach from=$products|sort item=_row}</td></tr>
<tr><td>${$_row.price|number_format:2}</td><td>{$_row.name}{/foreach}</td></tr>
</table>

And using it in a visual table in your editor or office documents, could look something like this:

PriceName{foreach from=$products|sort:"name" item=_row}
${$_row.price|number_format:2}{$_row.name}{/foreach}

Either above would output:

PriceName
$899.00iPhone X
$1,099.00Samsung Universe 9


SORTING BY MULTIPLE KEYS

Sometimes you’ll want to sort by more than one key; a primary key, then one or more secondary keys. This is useful when you have one or more of same values of a primary key, and want to further sort those same values by some other attribute that would vary. To sort your products by multiple keys, you’d use the |multisort Modifier. And again, you can describe the sort attributes using :”asc” or :”desc”. The below example would sort 1st by Name ascending, then by Price descending.

{foreach from=$products|multisort:"name":"asc":"price":"desc" item=_r}${$_row.price|number_format:2} {$_row.name}
{/foreach}


TABLEROW

While we recommend the {foreach operation when looping through items due to it’s greater flexibility, you can instead use {tablerow which will operate similarly to a {foreach.

Within a table, using HTML in the custom document template code editor, could look something like this:

<table>
<tr><td>Price</td><td>Name</td></tr>
<tr><td>{tablerow from=$products item=_row}${$_row.price|number_format:2}</td><td>{$_row.name}{/tablerow}</td></tr>
</table>


And using it in a visual table in your editor or office documents, could look something like this:

PriceName
{tablerow from=$products item=_row}${$_row.price|number_format:2}{$_row.name}{/tablerow}

Either above would output:

PriceName
$1,099.00Samsung Universe 9
$899.00iPhone X


You may also use {tableif within {tablerow calls. This is useful when desiring to skip the table row based on a variable’s value.

PriceName
{tablerow from=$products item=_row}{tableif $_row.stock > 50}${$_row.price|number_format:2}{$_row.name}{/tableif}{/tablerow}

…will output only…

PriceName
$1,099.00Samsung Universe 9


LISTROW

Same applies to lists as tables. Instead of {foreach, you may use {listrow and {listif within.

  • {listrow from=$products item=_row}${$_row.price|number_format:2} - {$_row.name}{/listrow}

…will output…

  • $1,099.00 – Samsung Universe 9
  • $899.00 – iPhone X


… and with {listif

  • {listrow from=$products item=_row}{listif $_row.stock > 50}${$_row.price|number_format:2} - {$_row.name}{/listif}{/listrow}

…will output only…

  • $1,099.00 – Samsung Universe 9
Tags: , , , ,

Was this article helpful to you?

Yes No

Related Articles