Hello,
After some research it looks like Face is a derivative of python's Jinja templating system.
This is great in the sense that there is actually some rather comprehensive documentation and examples online that can loosely be used to diagnose some issues in your own Face code, but there seem to be enough deviations from Jinja to occasionally make it a little aggravating.
With this in mind, some experimenting has led me to discover two undocumented filters that are quite useful for those using Face. (I imagine there are probably others):
The Sort Filter:
The following appears to work as an undocumented "sort" filter:
{% sorted_values = presorted_values | sort(attribute="value") %}
This would appear to corollate to the functionality in Jinja's implementation as described here:
The Replace Filter:
Another is the "replace" filter. The following code works:
{% sorted_options = sorted_options | replace("}{","},{") %}
This would appear to corollate to the functionality in Jinja's implementation as described here:
The one however that has me a bit stumped is that the documented Face filter "dictsort" does not seem to do anything particularly useful insofar as I can tell - at least for my application.
In my case, I'm trying to sort key:value pairs in Zoho Commerce. To be more specific, I'm trying sort a list of attribute options by the option name (or value) rather than the option id (which really is only useful in knowing the order in which a set of options was entered, as they're always assigned such that each one is id'd higher than the ones before).
The following is Zoho's documentation for Face's dictsort implementation:
dictsort
This filter sorts the given values in dictionary, i.e., alphabetical order.
Syntax
{{ map_variable | dictsort }}
Example
{ % assign map2="{"ant" : "black", "dog" : "brown" , "cat" : "yellow"} %}
{{ map2 | dictsort }}
Output
{ant=black, cat="yellow," dog="brown}
The description is a bit vague and doesn't really explain what dictsort does. Based on the description alone, I might assume that it would be sorting based on the value. This is not the case. By looking at the example, it's actually sorting key/value combinations based on the key (the option id in my case) - not the value.
Hoping that the Jinja guide to provide some additional reference, it appears that the jinja dictsort filter should allow you to sort by value:
{% for item in mydict|dictsort(false, 'value') %}
sort the dict by value, case insensitive
Unfortunately, I can't seem to get this working in Face in similar fashion.
Given its usefulness - especially for applications related to Zoho Commerce, can Zoho please expand dictsort to more closely mirror the behavior of the Jinja implementation to support sorting based on values instead of keys?
Lastly, I'd like to ask that Zoho might consider implementing one other filter type. There are several situations in which you might have options, product descriptions, or the like that are sequenced numerically. For example, if I'm selling lightbulbs, my attributes might be something like:
50 Watt
75 Watt
100 Watt
If I sorted these using a dictionary sort, they would end up as:
100 Watt
50 Watt
75 Watt
(This same problem is shown when sorting numeric skus in Inventory)
This of course is because 1 sequentially comes before 5, but the sorting code doesn't recognize that the value 100 is greater than the value of 50.
It would be far better to have a "natural sort" filter. If python is fact the backend for Face, the code shown here might be useful in doing do:
Thanks,
Bryan