玉模板作为html的预处理器

时间:2012-03-30 16:37:38

标签: templates node.js makefile pug

我不会在制作中使用node.js,但我喜欢jade的语法,因此我希望在开发时将jade模板编译为html

鉴于此文件结构:

app/
  jade_templates /
    index.jade
    subfolder /
      subpage.jade
  html_templates /
    index.html
    subfolder /
      subpage.html

我希望有一个脚本监视jade_templates目录,并在任何时候进行更改时将相应的html模板编译为html_templates

如何实现这一目标?

谢谢。

修改 Jade README有这个Sample Makefile,但我不确定如何根据我的需要调整它。

JADE = $(shell find pages/*.jade)
HTML = $(JADE:.jade=.html)

all: $(HTML)

%.html: %.jade
    jade < $< --path $< > $@

clean:
    rm -f $(HTML)

.PHONY: clean

4 个答案:

答案 0 :(得分:4)

由于我需要一个类似的脚本,我花时间尝试了一些工具和shell脚本(如forever),但找不到任何令人满意的东西。

所以我继续实施这个解决方案。你可以在github上找到它:

https://github.com/mihaifm/simplemon

看看它是否适合您。我也为玉添加了一个例子。

干杯!

答案 1 :(得分:2)

我使用Grunt。使用grunt-contrib-jadegrunt-contrib-watch,你可以相当轻松地设置一个grunt任务来监视jade文件的目录,并在它们发生变化时将它们编译到另一个目录。

Grunt有一点学习曲线,但它非常方便,让我可以随时随地在Jade(和Sass,和Coffeescript!)中进行开发 - 如果你对这种方法感兴趣,请发表评论我将添加一个可以执行您想要的示例Gruntfile。

答案 2 :(得分:0)

我建议您编写一个小节点应用程序来执行此操作。

代码如下所示:

// Watch a directory for files changes (such as here: https://github.com/Raynos/fyp/blob/master/src/build.js)
// Get the Jade code from the changed file
// Compile it
// Writes the output to a file with the same name in another directory

我说“节点应用程序”,但它应该是你喜欢的任何东西。

答案 3 :(得分:0)

您可以使用entr,如果其中一个指定的文件发生变化,它会执行一个程序:

find -name '*.jade' | entr make