Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pbrisbin/liquid
Haskell implementation of Liquid templating by Shopify
https://github.com/pbrisbin/liquid
Last synced: 2 months ago
JSON representation
Haskell implementation of Liquid templating by Shopify
- Host: GitHub
- URL: https://github.com/pbrisbin/liquid
- Owner: pbrisbin
- Created: 2013-10-12T20:31:03.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2017-05-30T18:35:33.000Z (over 7 years ago)
- Last Synced: 2024-10-10T20:16:40.943Z (3 months ago)
- Language: Haskell
- Homepage:
- Size: 41 KB
- Stars: 6
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Liquid Haskell
Haskell implementation of Liquid templating by Shopify.
**NOTE**: This project is dead. Please try https://github.com/projectorhq/haskell-liquid
# Installation
```
$ git clone https://github.com/pbrisbin/liquid && cd liquid
$ cabal sandbox init
$ cabal install --dependencies-only -- enable-tests
```# Usage
Generally speaking, the function `liquid` takes an object with a valid
`ToJSON` instance and template content as `Text`. It returns either
`Left error-message` or `Right processed-template`.How you come about this jsonify-able object or the textual template is
your business, but here is an example of how I might do things:```haskell
{-# LANGUAGE OverloadedStrings #-}import Data.Aeson
import Data.Text (Text)
import Text.Liquidimport qualified Data.Text as T
data Post = Post { postTitle :: Text }
data User = User
{ userName :: Text
, userAge :: Int
, userPosts :: [Post]
}instance ToJSON Post where
toJSON (Post title) = object ["title" .= title]instance ToJSON User where
toJSON (User name age posts) =
object [ "name" .= name
, "age" .= age
, "posts" .= map toJSON posts
]liquid (User "Pat" 28 [Post "Post one", Post "Post two"]) $
T.unlines [ "Name: {{name}}"
, "Age: {{age}}"
, "Posts:"
, "{% for post in posts %}"
, " * {{post.title}}"
, "{% endfor %}"
]
```This may seem verbose as a standalone example, but in a framework-using
web application (like Yesod), you'll likely already have models with
`ToJSON` instances.A more realistic use case may simply be:
```haskell
myHandler :: UserId -> Handler Html
myHandler userId = do
user <- get404 userId
template <- T.readFile "templates/user.html"return . preEscapedToMarkup
$ either errorHandler id
$ liquid user template
```## Testing
Running all specs:
```
$ cabal test
```Running one spec:
```
$ cabal exec -- ghc -isrc -itest -e main test/Text/Liquid/RenderSpec.hs
```Continuously run specs as files are edited:
```
$ gem install bundler
$ bundle
$ bundle exec guard
```