• Stars
    star
    243
  • Rank 166,489 (Top 4 %)
  • Language
    Go
  • License
    MIT License
  • Created over 7 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Liquid template engine in Go

Liquid Template Parser

go badge Golangci-lint badge Go Report Card badge Go Doc MIT License

liquid is a pure Go implementation of Shopify Liquid templates. It was developed for use in the Gojekyll port of the Jekyll static site generator.

Installation

go get gopkg.in/osteele/liquid.v1 # latest snapshot

go get -u github.com/osteele/liquid # development version

Usage

engine := liquid.NewEngine()
template := `<h1>{{ page.title }}</h1>`
bindings := map[string]interface{}{
    "page": map[string]string{
        "title": "Introduction",
    },
}
out, err := engine.ParseAndRenderString(template, bindings)
if err != nil { log.Fatalln(err) }
fmt.Println(out)
// Output: <h1>Introduction</h1>

See the API documentation for additional examples.

Command-Line tool

go install gopkg.in/osteele/liquid.v0/cmd/liquid installs a command-line liquid executable. This is intended to make it easier to create test cases for bug reports.

$ liquid --help
usage: liquid [FILE]
$ echo '{{ "Hello World" | downcase | split: " " | first | append: "!"}}' | liquid
hello!

Documentation

Status

These features of Shopify Liquid aren't implemented:

  • Filter keyword parameters, for example {{ image | img_url: '580x', scale: 2 }}. [Issue #42]
  • Warn and lax error modes.
  • Non-strict filters. An undefined filter is currently an error.

Drops

Drops have a different design from the Shopify (Ruby) implementation. A Ruby drop sets liquid_attributes to a list of attributes that are exposed to Liquid. A Go drop implements ToLiquid() interface{}, that returns a proxy object. Conventionally, the proxy is a map or struct that defines the exposed properties. See http://godoc.org/github.com/osteele/liquid#Drop for additional information.

Value Types

Render and friends take a Bindings parameter. This is a map of string to interface{}, that associates template variable names with Go values.

Any Go value can be used as a variable value. These values have special meaning:

  • false and nil
    • These, and no other values, are recognized as false by and, or, {% if %}, {% elsif %}, and {% case %}.
  • Integers
    • (Only) integers can be used as array indices: array[1]; array[n], where array has an array value and n has an integer value.
    • (Only) integers can be used as the endpoints of a range: {% for item in (1..5) %}, {% for item in (start..end) %} where start and end have integer values.
  • Integers and floats
    • Integers and floats are converted to their join type for comparison: 1 == 1.0 evaluates to true. Similarly, int8(1), int16(1), uint8(1) etc. are all ==.
    • [There is currently no special treatment of complex numbers.]
  • Integers, floats, and strings
    • Integers, floats, and strings can be used in comparisons <, >, <=, >=. Integers and floats can be usefully compared with each other. Strings can be usefully compared with each other, but not with other values. Any other comparison, e.g. 1 < "one", 1 > "one", is always false.
  • Arrays (and slices)
    • An array can be indexed by integer value: array[1]; array[n] where n has an integer value.
    • Arrays have first, last, and size properties: array.first == array[0], array[array.size-1] == array.last (where array.size > 0)
  • Maps
    • A map can be indexed by a string: hash["key"]; hash[s] where s has a string value
    • A map can be accessed using property syntax hash.key
    • Maps have a special size property, that returns the size of the map.
  • Drops
    • A value value of a type that implements the Drop interface acts as the value value.ToLiquid(). There is no guarantee about how many times ToLiquid will be called. [This is in contrast to Shopify Liquid, which both uses a different interface for drops, and makes stronger guarantees.]
  • Structs
    • A public field of a struct can be accessed by its name: value.FieldName, value["fieldName"].
      • A field tagged e.g. liquid:”name” is accessed as value.name instead.
      • If the value of the field is a function that takes no arguments and returns either one or two arguments, accessing it invokes the function, and the value of the property is its first return value.
      • If the second return value is non-nil, accessing the field panics instead.
    • A function defined on a struct can be accessed by function name e.g. value.Func, value["Func"].
      • The same rules apply as to accessing a func-valued public field.
    • Note that despite being array- and map-like, structs do not have a special value.size property.
  • []byte
    • A value of type []byte is rendered as the corresponding string, and presented as a string to filters that expect one. A []byte is not (currently) equivalent to a string for all uses; for example, a < b, a contains b, hash[b] will not behave as expected where a or b is a []byte.
  • MapSlice
    • An instance of yaml.MapSlice acts as a map. It implements m.key, m[key], and m.size.

References

Contributing

Bug reports, test cases, and code contributions are more than welcome. Please refer to the contribution guidelines.

Contributors

Thanks goes to these wonderful people (emoji key):


Oliver Steele

💻 📖 🤔 🚇 👀 ⚠️

James Littlejohn

💻 📖 ⚠️

nsf

💻 ⚠️

Tobias Salzmann

💻

Ben Doerr

💻

Daniil Gentili

💻

Carolyn Van Slyck

💻

Kimmo Lehto

💻

Victor "Vito" Gama

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

Attribution

Package Author Description License
Ragel Adrian Thurston scanning expressions MIT
gopkg.in/yaml.v2 Canonical MapSlice Apache License 2.0

Michael Hamrah's Lexing with Ragel and Parsing with Yacc using Go was essential to understanding go yacc.

The original Liquid engine, of course, for the design and documentation of the Liquid template language. Many of the tag and filter test cases are taken directly from the Liquid documentation.

Other Implementations

Go

Other Languages

See Shopify's ports of Liquid to other environments.

License

MIT License

More Repositories

1

functional-javascript

Functional is a library for functional programming in JavaScript. It defines the standard higher-order functions such as map, reduce (aka foldl), and select (aka filter). It also defines functions such as curry, rcurry, and partial for partial function application; and compose, guard, and until for function-level programming.
JavaScript
381
star
2

gojekyll

A fast Go implementation of the Jekyll blogging engine
Go
317
star
3

callgraph

Magic to display dynamic call graphs of Python function calls
Python
64
star
4

dart-tonic

Music theory Dart package
Dart
50
star
5

jquery-profile

jQuery plugin to profile calls to jQuery selectors.
JavaScript
39
star
6

p5-server

Command-line tool to create and run p5.js sketches. It runs a server with live reload, sketch-aware directory listings, automatic libraries for JavaScript-only sketches.
TypeScript
38
star
7

collections-js

Framework-independent JavaScript collection methods.
JavaScript
32
star
8

ipython-secrets

A Python package that simplifies the use of secrets in a Jupyter notebook
Python
21
star
9

matrix-archive

Export a Matrix room message archive
Python
20
star
10

cl-spec

BDD for Common Lisp
Common Lisp
18
star
11

jsspec

A clone of Alan Kang's JSSpec
JavaScript
17
star
12

tonic.ts

TypeScript music theory, pitch constellation diagram, and guitar chord calculator
TypeScript
15
star
13

sequentially

Sequentially is a library of temporal and frequency adverbs for JavaScript. It provides methods to queue a function for deferred or periodic execution, and to throttle the rate or number of times that a function can be called. You could think of it as a kind of memoization, where instead of caching the result it modifies when and whether a function is called.
JavaScript
14
star
14

imu-tools

Send sensor data from an ESP + BNO055 → MQTT and/or serial port
Python
12
star
15

tuesday

Ruby-compatible strftime for golang
Go
12
star
16

mop-js

JavaScript utilities for Metaobject Programming
JavaScript
12
star
17

fluently

Fluent programming (chained method calls) for JavaScript.
JavaScript
12
star
18

Arduino-BLE-IMU

Publish IMU data over BLE
C++
10
star
19

javascript_fu

A Rails plugin to add more support for javascript files.
Ruby
9
star
20

banyan

Visualize Dropbox file and folder sizes
Elm
9
star
21

pyfsa

Python FSA constructor, determinizer, and minimizer.
Python
9
star
22

vscode-p5server

VSCode extension to launch a live server that is aware of p5.js
CSS
8
star
23

fingerboard

Display the violin, viola, and cello fingerings for each scale
CoffeeScript
8
star
24

gsheet-keyring

Python Keyring backend backed by a Google Sheet
Python
7
star
25

db_content

Rails plugin to add sql dump and restore tasks
Ruby
7
star
26

bootle

“Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration.” — Stan Kelly-Bootle
Python
7
star
27

cfdg-js

A JavaScript implementation of Chris Coyne's Context Free Design Grammar.
JavaScript
7
star
28

p5.libs

JavaScript
6
star
29

flinx

Configuration-free Sphinx documentation
Python
5
star
30

p5-react

A component that embeds a p5.js canvas in a React application
JavaScript
5
star
31

classroom-tools

Tools for collecting and analyzing assignments – mostly related to GitHub and Jupyter notebooks – plus a few other tasks.
Python
5
star
32

sneetches

A Chrome extension that adds star counts next to GitHub repo links
TypeScript
5
star
33

openlaszlo-json

JSON library for OpenLaszlo
JavaScript
5
star
34

multiclone

Clone forks of a GitHub repo, or copies of a GitHub Classroom assignment
Go
4
star
35

lzosutils

OpenLaszlo utilities: flash bridge, ajax, etc.
JavaScript
4
star
36

openlaszlo_plugin

The OpenLaszlo Rails plugin makes it easy to use OpenLaszlo client-side applications with Rails.
JavaScript
4
star
37

jcon

Conformance checking of JSON values against ECMAScript 4.0 types
Ruby
4
star
38

fretboard

Tools for rendering guitar fretboard and chord diagrams.
JavaScript
4
star
39

git-timelapse

Create time-lapse movies of git repo files, metrics, or other artifacts
Python
4
star
40

wideurl.com

The PHP behind 2006's wideurl.com.
PHP
3
star
41

html2dbk

HTML to Docbook converter
HTML
3
star
42

QtTileDual

Draw a graph and its duals. I wrote this to learn Qt.
C++
3
star
43

python-utils

Oliver's Python utilities
Python
3
star
44

p5pose

Starter kit for exploring WebCam-based pose recognition with PoseNet and p5.js
JavaScript
3
star
45

p5-template

https://www.notion.so/P5-js-Particle-Workshop-aba7992a689c457a8cca10e2f49e6a04
HTML
3
star
46

storyboard

An ruby-processing extension for scripting storyboarded explanatory visualizations. In progress.
Ruby
2
star
47

gem_recent-updates

A gem command plugin that displays the tops of the history files of recently updated gems.
Ruby
2
star
48

code.osteele.com

Elm
2
star
49

notebooks

Miscellaneous Jupyter notebooks.
Jupyter Notebook
2
star
50

inventiveminds.xyz

Source to Inventive Minds web site
Ruby
2
star
51

p5pose-optitrack

Use p5.js to render data from an OptiTrack CSV -> WebSocket server.
JavaScript
2
star
52

osteele.com

Source to personal web site
PHP
2
star
53

gh-forkstats

Displays GitHub forks and their stats, to identify successors to abandoned repos.
JavaScript
2
star
54

ffmachine

DEC Digital Logic Module editor and simulator
CoffeeScript
2
star
55

lztestkit

BDD for OpenLaszlo.
JavaScript
2
star
56

hackingthelibrary.org

Hacking the Library 2017 and 2018 @ Olin College
HTML
2
star
57

imu-client-examples

Example web programs that use the imu-tools npm package to connect via BTLE or MQTT to an ESP32 + BNO055
JavaScript
2
star
58

ropenlaszlo

ROpenLaszo is a Ruby interface to the OpenLaszlo compiler. It allows you to compile OpenLaszlo programs from within Ruby, in order to integrate OpenLaszlo development into Rake or Rails applications.
Ruby
2
star
59

git-keychain-secrets

Store some parts of a repo file in the macOS Keychain; keep the rest un-encrypted
Ruby
2
star
60

minimal-keys

Compute the unique minimal keys from a collection of strings or sequences.
Python
2
star
61

old-blog

Ruby
1
star
62

tidal-memories

On-the-ground piece for Dinacon
JavaScript
1
star
63

matrix-photo-gallery

A photo gallery for photos from a Matrix room.
JavaScript
1
star
64

liquid-tabulator

Olin Election Techology Co-Curricular Vote Tabulator
Python
1
star
65

html2cheatsheet

Create a Dash Cheatsheet from HTML on apple.com
Python
1
star
66

terminal-codes-to-html

Convert strings that include terminal color codes to HTML or plain text.
TypeScript
1
star
67

changelog-parser

An npm package that parses CHANGELOG files from markdown to JSON.
CSS
1
star
68

nbcollate

Collate Jupyter assignment notebooks
Jupyter Notebook
1
star
69

p5-orientation-and-motion-example

JavaScript
1
star
70

expialidocious

Timeline tag browser for delicious, in OpenLaszlo
JavaScript
1
star
71

protodoc

JavaScript documentation generator and pydoc equivalent
JavaScript
1
star
72

signage

Web site playlist manager for digital signage
JavaScript
1
star
73

openlaszlo-rails-example

Ruby
1
star
74

volatility

Web page that displays the current value of Bitcoin alongside the face of a six-sided die
Elm
1
star
75

elm-extras

Personal Elm extras. Extracted from Banyan.
Elm
1
star
76

pwm-explorer

Interactive visualization of Pulse Width Modulation (PWM)
JavaScript
1
star
77

micropython-stubs

Stubs for MicroPython APIs, to assist type-checking tools
Python
1
star