package liquid_std

  1. Overview
  2. Docs
The Standard Libarary for Liquid

Install

dune-project
 Dependency

Authors

Maintainers

Sources

0.1.3.tar.gz
md5=24f974b1310954a4ee6202d659aafe96
sha512=8cacca711ef271678118f2959dc734e677d54cd878b95fd2123ab4caa9d3d0dfd1fe17a59660ae1d59374bceca3063fdb1e18cc1018f2aca5283edd4ac97beed

doc/src/liquid_std/liquid_helpers.ml.html

Source file liquid_helpers.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
open Base
open Liquid_syntax
open Syntax
open Values
open Helpers

let date ctx params =
  let do_date fmat = function
    | "now" -> String (Date.now_as_string fmat)
    | other -> String (Date.format_date_string other fmat)
  in

  match unwrap_all ctx params with
  | String date_str :: String fmat :: _ -> do_date fmat date_str |> ok
  | String date_str :: _ -> do_date "%m/%d/%Y" date_str |> ok
  | Date date :: String fmat :: _ -> String (Date.as_string date fmat) |> ok
  | [ Date date ] -> String (Date.as_string date "%m/%d/%Y") |> ok
  | other ->
      errc "date accepts a string or a date and an optional format string" other

let default ctx params =
  match unwrap_all ctx params with
  | a :: b :: Bool allow_false :: _ ->
      let comp = if allow_false then Values.is_not_nil else Values.is_truthy in
      if comp ctx a then Ok a else Ok b
  | a :: b :: _ -> if Values.is_truthy ctx a then Ok a else Ok b
  | other ->
      errc "default accepts 2 values and an optional named argument allow_false"
        other

(* TODO: Look into liquid int/float distinction *)

let json ctx params =
  match unwrap_all ctx params with
  | v :: _ -> String (Values.json_from_value ctx v) |> ok
  | other -> errc "json conversion failed!" other

let function_from_id = function
  | "date" -> Some date
  | "default" -> Some default
  | "json" -> Some json
  | _ -> None