Git Product home page Git Product logo

enlive-partials's Introduction

Enlive-partials

Adds support for including and embedding partial templates in the enlive templating library. Based on the awesome work by Brenton Ashworth, Craig Andera and others at Relevance, Inc. on the Clojurescript one project.

Build Status

Why?

One of the biggest benefits of using Enlive is the ability to have designers work with plain HTML/CSS, without having to worry about the underlying template engine. This is great, but can lead to a lot of duplication in your templates (just imagine having to include all the Twitter Bootstrap dependencies in all your template files).

A better approach is to create a layout.html file with all the necessary resources, and interpolate the html specific to each template into the body of layout.html.

Install

###Leiningen

[enlive-partials "0.2.0"]

###Maven

<dependency>
  <groupId>enlive-partials</groupId>
  <artifactId>enlive-partials</artifactId>
  <version>0.2.0</version>
</dependency>

Remember to add clojars as a repository:

 <repository>
   <id>clojars</id>
   <url>http://clojars.org/repo/</url>
 </repository>

Usage

Example taken from the Clojurescript one documentation.

Suppose we have a layout file templates/layout.html which contains the following HTML:

<html>
  <head>Example</head>
  <body>
    <_include file="menu.html"/>
    <div id="content"></div>
    <div id="footer"></div>
  </body>
</html>

Furthermore, suppose we have the following two files named templates/menu.html and templates/example.html.

<!-- templates/menu.html -->
<div>Navigation Menu</div>
<!-- templates/example.html -->
<_within file="layout.html">
  <div id="content">New Content</div>
  <div id="footer">A Footer</div>
</_within>

In order to handle the partial templates you have to enable the com.ebaxt.enlive-partials/handle-partials ring middleware.

Assumig you have the following directory structure:

resources/
  templates/
    example.html
    menu.html
    layout.html

You enable the middleware by passing in the path to the template directory, together with an optional :templateContext. If no template-context is provided, the template path will be used.

(use 'ring.middleware.file 'ring.adapter.jetty 'com.ebaxt.enlive-partials)

(defn hello [req]
  {:status 200
   :headers {"Content-Type" "text/html"}
   :body "Hello World"})

(def app (handle-partials hello "templates" {:template-context "tpl"}))

(run-jetty app {:port 8080})

If we point the browser to http://localhost:8080/tpl/example.html, the following file will be served:

<html>
  <head></head>
  <body>
    <div>Navigation Menu</div>
    <div id="content">New Content</div>
    <div id="footer">A Footer</div>
  </body>
</html>

Var support

Since version 0.2.0 enlive-partials supports using enlive ${vars}. A typical use-case for this is rewriting the context-path for resources, in order to load them from a cdn.

<html>
  <head>
      <script src="${cdn}/js/app.js"></script>
  </head>
  <body>...</body>
</html>

In order to replace the ${cdn} pass a :vars map containing the new values to the handler:

(handle-partials hello "templates" {:template-context "tpl" :vars {:cdn "http://cdn.com"}})

License

Copyright © 2013 Erik Bakstad

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

enlive-partials's People

Contributors

ebaxt avatar jonabbey avatar daemianmack avatar

Watchers

James Cloos avatar  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.