Static HTML compilation with partials using Grunt.js [closed]

送分小仙女□ 提交于 2019-12-03 05:51:37

问题


I've been looking all over for something that will let me precompile static websites using Grunt. It needs to have partials, so I can include things like a common header/footer across the pages.

So far, I've only really found Jade, which has a grunt plugin, and this plugin for Grunt that compiles Dust.js templates to static HTML. I don't really like Jade's syntax, and the Dust plugin for Grunt is less than ideal.

Are there any static HTML templating languages with Grunt/Gulp support that don't deviate too much from regular HTML, and are still active?


回答1:


I found this plugin named grunt-includes. I've been looking for an answer to your question forever. This is the first one I've seen that seems easy to use. All the others seem to maybe have this feature but do 20x other things so they seem like the wrong tool for the job.

The one thing this is missing for me is the ability to prefix relative paths. I'm talking to the developer of modest which is becoming a better option.

UPDATE: There's a grunt-includes-replace that is almost as simple and can prefix relative paths. IE: it lets you pass in variables.




回答2:


I've been having some success doing just that with grunt plugin assemble. I made a couple of vids when I first started using it:

http://www.youtube.com/watch?v=oRwL5Y7K0CM (5:43)

http://www.youtube.com/watch?v=R9Jj9ciA2wM (16:44)

Here's the official site:

https://github.com/assemble/assemble

From that site you can see how partials can be used; for example:

assemble: {
  options: {
    assets: 'assets',
    partials: ['docs/includes/**/*.hbs'],
    data: ['docs/data/**/*.{json,yml}']
  },
  pages: {
    src: ['docs/*.hbs'],
    dest: './'
  }
}

Then essentially you're able to run something like:

grunt assemble

or for more fine grained control you can execute a task of the assemble target like:

grunt assemble:your_target

It's working well for me. It does require a bit of a learning curve and the docs will likely improve as they continue to work on it.




回答3:


I use https://npmjs.org/package/grunt-dust for pre-compiling dust templates with partials.

The relevant part of Gruntfile.js can look something like this:

    dust: {
        defaults: {
            files: {
                'views/index.js': 'views/**/*.dust'
            },  
            options: {
                wrapper: 'commonjs',
                runtime: false,
                wrapperOptions: {
                    returning: 'dust',
                    deps: {
                        dust: 'dustjs-linkedin',
                        dustHelpers: 'dustjs-helpers'
                    }   
                }   

            }   
        }   
    },

This will put all of the compiled dust templates in a views/index.js.

There are more examples and more detailed docs at https://github.com/vtsvang/grunt-dust



来源:https://stackoverflow.com/questions/18263750/static-html-compilation-with-partials-using-grunt-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!