Coder Social home page Coder Social logo

vivid-inc / ash-ra-template Goto Github PK

View Code? Open in Web Editor NEW
23.0 23.0 1.0 1.1 MB

Expressive & customizable template system featuring Clojure language processing

Home Page: https://vivid-inc.net

License: Apache License 2.0

Clojure 97.39% Shell 2.61%
art clojure erb erb-syntax lisp template template-engine template-language template-library templates templating

ash-ra-template's People

Contributors

enspritz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

respatialized

ash-ra-template's Issues

Classpath issues with local code for template evaluation

Hi! Thanks for this great library - I'm extremely excited about its capabilities, as it essentially brings the power and flexibility of the Clojure ecosystem to a concept very similar to Matthew Butterick's pollen library for Racket. However, I'm having some trouble getting my code across the edge of the sandbox that you're using ShimDandy to create.

The workflow I'm trying to support is:

  1. create helper functions around the capabilities of hiccup.
  2. call those functions when rendering the templates into HTML files.

I'm encountering a lot of errors, depending on how I try the invocation - not least because I'm probably not super solid in my understanding of vars, symbols, and namespaces. Say I have some fns defined like the following:

(ns user
  (:require [hiccup.core :refer [html]]
                [vivid.art :as art]))

(defn my-header [text] (html [:h1 text]))
(defn span [text] (html [:span text]))
(defn span2 [text] (str "<span>" text "</span>"))

(def sample-template
  "test 1: <%= (my-header \"sample text!\") %> 
   test 2: <%= (span \"sample span!\") %>
   test 3: <%= (span2 \"sample span 2\") %>")

Here are the various ways I've tried to get my functions into the runtime environment:

Passing the function name directly to art/render throws a IllegalStateException:

(art/render sample-template
                 {:bindings '{my-header my-header span span span2 span2}})
> Execution error (IllegalStateException) at user/eval158 (REPL:11).
> Attempting to call unbound fn: #'user/my-header

Quoting the function names in the :bindings map returns null for each invocation:

(art/render sample-template
                 {:bindings '{my-header 'my-header span 'span span2 'span2}})
> "test 1: null \n   test 2: null\n   test 3: null"

Attempting to pass the fully-qualified function name (using var or resolve) causes a StackOverflowError

(art/render sample-template
                  '{:bindings {my-header (var my-header) span  (var span) span2  (var span2) }})
> Execution error (StackOverflowError) at (REPL:1).
> null

I tried as best I could to replicate the example in bindings-test.clj - I'm not sure if I did it correctly, because I don't know which of these methods are supposed to be the canonical way of passing in these functions.

When that failed, I decided to try a different tack: factor out my rendering code into a library and add it as a local dependency using the capabilities of tools.deps.

;; deps.edn:
{:paths ["build"]
 :deps {
  org.clojure/java.classpath {:mvn/version "1.0.0"}
  vivid/ash-ra-template {:mvn/version "0.4.0"}
  my-lib {:local/root  "/path/to/my-lib"}}}
;; /path/to/my-lib/src/my-lib/render.clj
(ns my-lib.render
  (:require [hiccup.core :refer [html]]))

(defn em [text] (html [:em text]))
(ns build
  (:require [vivid.art :as art]
            [clojure.java.io :as io]
            [my-lib.render :as render])

(def art-config
  {:dependencies
   {'hiccup {:mvn/version "2.0.0-alpha2"}
    'org.clojure/clojure {:mvn/version "1.10.0"}
    'my-lib {:local/root  "/path/to/my-lib"}
    'org.clojure/java.classpath {:mvn/version "1.0.0"}}})

Local deps added with :dependencies throw a classpath exception when (:require )'d

I know I'm referring to the classpath correctly because I can invoke the following shell command:

$ clj -Sdeps '{:deps {my-lib {:local/root "./my-lib"}}}' -e '(require `[my-lib.render :as render]) (my-lib.render/em "test")'
> "<em>help</em>"

I can also invoke the functions from my local library from the repl if I (require) them. However, here's what happens when I try to invoke them from within the ash-ra-template environment:

(art/render "<% (require '[my-lib.render :refer [em]]) %><%= (em \"text\")"
                  {:dependencies {'my-lib {:local/root "./my-lib"}}})
>Execution error (FileNotFoundException) at user/eval156 (REPL:7).
>Could not locate Clojure resource on classpath: my-lib/render.clj

I did a sanity check by invoking some hiccup functions directly:

(art/render "<% (require '[hiccup.core :refer [html]]) %><%= (html [:span \"text\"])"
                  {:dependencies {'hiccup {:mvn/version "2.0.0-alpha2"}}})
> "<span>text</span>"

So the hiccup fns work, but mine don't, even though the directories containing render.clj are on the classpath (I checked by using clojure.java.classpath within the template).

Any idea on how I should proceed here? I could simply define all my rendering functions inline in my page templates, but that seems like it would rapidly become unmaintainable. I'm happy to provide additional info for debugging or clarify these examples - I tried to make them as reproducible as possible.

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.