merv
merv
is an opinionated wrapper around the excellent (and minimal)
mustache
templating library.
Why?
IMO, we overcomplicate things for for building simple web applications and sites
these days. Surely we can take a solid piece of tech, use some new code
idioms (such as async..await
) and get some real milege with only
a few packages.
Usage
Basically, merv
expects a certain convention. For things to work swimmingly
the following directory structure is advised:
- templates
|- a.html
|- b.html
- partials
|- x.html
|- y.html
- layout.html
(Both the layout.html
file and partials
directory are optional.)
Here's a quick look at what a template file might look like:
{{title}} {{> partial:meta }} {{> main}}
You can see we are using the standard mustache partial syntax but we are managing the partial loading ourselves. This might seem a little contrived, however, the advanced usage is coming soon.
With things set up like this you can then do something like the following:
const loadTemplates = ; // initialise merge with details of the path { const render = await ; const data = title: 'foo' ; console;} ;
What is happening here is that we have told merv
that we want to render the
a
template. So diligent merv
does that first, but then goes the extra
mile to see if we have a layout.html
file around. If so it then
uses the {{> main}}
partial to inject the output of the originally requested
template into the appropriate position.
Approach
While mustache has a limited set of features, it has enough in it's toolkit to everything you are going to need. While I'd commonly got to a more heavy weight templating engine such as PUG for layouts, includes of abritrary file content; these are all things that can be achieved with mustache.
Merv is just a collection of the general patterns that I have found to work to perform some of these tasks.
Limitations
This is designed for server side rendering only. Really it has no designs on being anything useful for the client-side. You should reach for those tools that are geared towards client rendering in those cases.
What's with the name?
See: https://en.wikipedia.org/wiki/Merv_Hughes
LICENSE
The MIT License (MIT)
Copyright (c) 2019 Damon Oehlman damon.oehlman@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.