Code Monkey home page Code Monkey logo

hbs's Introduction

hbs

Real-world Clojure templating, seriously. Don't talk about enlive or hiccup on a clojure web development book any more.

  • Never ruin your Clojure code with HTML
  • Never ruin your HTML code with Clojure
  • Templating without reuse mechanism is shit
  • Templating without customization is nothing but shit

What handlebars has?

  • Separate your Clojure and HTML, calm down both
  • Reuse your handlebars template with include, partial and block
  • Create your own helpers for your infinite customization needs

This library is based on handlebars.java.

Build Status

Usage

Leiningen

https://clojars.org/link

Using hbs.core

(require '[hbs.core :as hbs])

;; render template string
(render "Hello {{person.name}}" {:person {:name "World"}})

;; render template file with a registry
(def reg (registry (classpath-loader "/templates" ".tpl")))
(render-file reg "hello" {:name "World"})

Using hbs in Ring application

hbs library provides a default ring middleware hbs.core/wrap-handlebars-template to generate pages from standard response: {:hbs {:template ... :context ...}}

;; ring handler function that generate handlebars response
(defn some-handler [req]
  ;;...
  {:hbs {:template "index"
         :context {:a "A" :b "B"}}})

By default, hbs middleware use text/html; charset=utf-8 as content type. You can override it by setting {:headers {"Content-Type" ...}} explicitly in your response.

Extending hbs.helper

Handlebars is nothing without helpers.

(use '[hbs core helper])

;; def a helper
(defhelper mytag [ctx options]
  (safe-str "HelloWorld, " (clojure.string/upper ctx)))

(def reg (registry (classpath-loader "/templates" ".tpl")))

;; register the helper into registry
(register-helper! registry "mytag" mytag)

(render "{{mytag foo}}" {:foo "bar"})

Using javascript helpers

Helpers can also be defined using javascript. Javascript helpers are registered using register-js-helpers!-function

(register-js-helpers! registry "path/to/file.js")

Helpers defined by me

I have some predefined helpers used in my projects. And I decide to ship it in the release.

To use these helpers, be sure eval hbs.ext. You can just add a (:require [hbs.ext]) on you core namespace.

Available helpers:

  • ifequals
  • ifgreater
  • ifless
  • ifcontains
  • uppercase
  • lowercase
  • or
  • count
  • format-date
  • format
  • ifempty
  • max
  • min

You can find usage examples of these helpers in the test case test/hbs/ext_test.clj.

See also

Handlebars implemented for Rust language: handlebars-rust.

License

Copyright © 2013-2018 Sun Ning

Distributed under the Eclipse Public License, the same as Clojure.

hbs's People

Contributors

cap10morgan avatar okorz001 avatar simoraman avatar sunng87 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.