Parsing CSV with Expr

This example parses the products.csv file directly in the template by using the Expr expression language.

NamePriceCategoryStock
Laptop1200Electronics15
Coffee Maker80Appliances30
Desk Chair250Furniture10
Notebook5Stationery100
Monitor300Electronics25
// the above is parsed with
let lines = split(csvData, "\n");
let header = map(split(trim(lines[0]), ","), { trim(#) });
let dataLines = filter(lines[1:], { trim(#) != "" });
map(dataLines, {
    let fields = split(trim(#), ",");
    let pairs = map(header, {
        let value = fields[#index];
        let isInt = value matches '^-?[0-9]+$';
        let isFloat = value matches '^-?[0-9]+\\.[0-9]+$';
        [#, isInt ? int(value) : (isFloat ? float(value) : value)]
    });
    fromPairs(pairs)
})

See the Expr documentation and language definition for more about the language.

Filtered products: electronics only

This section only displays products with the category set to “Electronics”.