FACE Templating, Jinja, Sorting, Replacing, and dictsort, and natural sort

FACE Templating, Jinja, Sorting, Replacing, and dictsort, and natural sort

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



      • Sticky Posts

      • Website Accessibility

        I have two sites created in Zoho Sites which are both live. Some questions: 1. Are any of the template themes WCAG 2.1 AA compliant? 2. How do I change the format of a sub-heading in the header section? 3. Is there any way to add focus indication for
      • Unveiling Zoho Sites 2.0 - A new dimension in website building

        Dear Zoho Sites Users, We are thrilled to announce the launch of Zoho Sites 2.0 today! This refresh represents a significant step forward in the capabilities of Zoho Sites and is crucial for creating a lasting and positive impact on our customers' businesses.
      • Visual Editor Webinar

        Join our FREE live webinar to learn how to use the Zoho Sites Visual Editor to easily design your website. Our product specialist will demonstrate how this tool will help you customize your website to suit your requirements.  Register here: https://www.zoho.com/sites/webinars/
      • SEO for your Zoho Sites Website

        Join our live webinar to learn how to use Zoho Sites' SEO tools to boost your website's ranking. Our product specialist will demonstrate everything you need to know about optimizing your web pages to make them search engine friendly. Register here for free: https://www.zoho.com/sites/webinars/
      • Zoho Sites Webinar | Integrations

        Join our FREE live webinar to learn how to integrate both Zoho and third-party products with your Zoho Sites website. Our product expert will demonstrate how to use these integrations to grow, manage, and retain the customers you bring in. Learn how to take your business forward and build stunningly robust and efficient websites. Register here: https://www.zoho.com/sites/webinars/