futureaus / servue

Server-side rendering engine that renders vue files into html strings
https://futureaus.github.io/servue
59 stars 4 forks source link

Support Express set View Engine #21

Closed trekze closed 5 years ago

trekze commented 5 years ago

Hey dude.

Nice library. I've started using it as an alternative to nunjucks for templating.

One nice-to-have would be to make it work as a standard Express view engine. Then you could just call render() on .vue files instead of calling the renderer directly. Not a big deal tho. Works fine as is.

https://expressjs.com/en/guide/using-template-engines.html

AlbertMarashi commented 5 years ago

I've done something like this for my Koa app:

    var servue = new Servue()
    app.use(async (ctx, next) => {
        /**
         * render returns promise to the string which contians the rendered .vue file
         * @param {string} vueFile - path to vue single-file-component
         * @param {Object=} [data={}] - data to be inserted into .vue file when generating renderer
         * @returns {Promise<string>}
         */
        ctx.render = async(vueFile, data) => {
            ctx.body = await servue.render(vueFile, data)
        }

        await next()
    })

Be sure to instantiate Servue outside of the middleware, otherwise you'll be creating a new renderer for every single request

You can do something similar with express pretty easily

trekze commented 5 years ago

Thanks for the code sample. I'll give it a shot.