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

主宰稳场 提交于 2019-12-02 19:13:04

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.

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.

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

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