从html文件创建Clojurescript模板
我不熟悉clojure/clojurescript,所以我忽略了一个简单的解决方案,但我的问题是: 我有现有的html文件(只是原始html,没有变量或任何东西),我想用作clojurescript项目的部分(这些html文件是导航、页脚等)。我希望在clojurescript项目中需要这些html模板,然后将这些模板内联到已编译的js中,这样我就不必在生产环境中对模板进行任何ajax调用,也不必复制html文件以供生产环境中的客户机使用。像requirejs和browserify这样的项目都有插件,让你只需要“需要”html文件——clojurescript有等效的吗 我知道有一些库可以进行模板/dom操作,所以这不是一个问题。它只是将多个外部html文件转换为内联字符串/dom节点/生产js中包含的任何内容从html文件创建Clojurescript模板,clojurescript,Clojurescript,我不熟悉clojure/clojurescript,所以我忽略了一个简单的解决方案,但我的问题是: 我有现有的html文件(只是原始html,没有变量或任何东西),我想用作clojurescript项目的部分(这些html文件是导航、页脚等)。我希望在clojurescript项目中需要这些html模板,然后将这些模板内联到已编译的js中,这样我就不必在生产环境中对模板进行任何ajax调用,也不必复制html文件以供生产环境中的客户机使用。像requirejs和browserify这样的项目都有
谢谢,您可以使用编译时执行的宏来完成这项工作 project.clj
:dependencies [[org.clojure/clojure "1.6.0"]
[org.clojure/clojurescript "0.0-2202"]]
:source-paths ["src"]
:cljsbuild
{:builds
[{:id "main"
:source-paths ["src/cljs"]
:compiler
{
:output-to "resources/main.js"
:optimizations :whitespace
:pretty-print true}}]})
(ns cljstemplates.core
(:require [clojure.java.io :refer (resource)]))
(defmacro deftmpl
"Read template from file in resources/"
[symbol-name html-name]
(let [content (slurp (resource html-name))]
`(def ~symbol-name
~content)))
src/cljstemplates/core.clj
:dependencies [[org.clojure/clojure "1.6.0"]
[org.clojure/clojurescript "0.0-2202"]]
:source-paths ["src"]
:cljsbuild
{:builds
[{:id "main"
:source-paths ["src/cljs"]
:compiler
{
:output-to "resources/main.js"
:optimizations :whitespace
:pretty-print true}}]})
(ns cljstemplates.core
(:require [clojure.java.io :refer (resource)]))
(defmacro deftmpl
"Read template from file in resources/"
[symbol-name html-name]
(let [content (slurp (resource html-name))]
`(def ~symbol-name
~content)))
src/cljs/web.cljs
(ns cljstemplates.web
(:require-macros [cljstemplates.core :refer [deftmpl]]))
(deftmpl head "header.html")
(deftmpl nav "nav.html")
(deftmpl foot "footer.html")
这将生成varshead
,nav
,foot
,其中包含从资源/文件夹中的文件读取的字符串
resources/nav.html
<nav>
<ul>
<li>Tweets</li>
</ul>
</nav>
看看是否适合你的需要。clojurescript,编译的模板。谢谢,我会看看这是否对我们有用。不确定它是否解决了包含模板的单个js文件的问题,但对于其他文件来说似乎是可靠的-我必须尝试一下,看看。谢谢你!哇,太棒了,谢谢。作为一个clojure新手,在我得到它之前,我必须读几遍,但这非常有用。