Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
templatizer
Advanced tools
Simple solution for compiling jade templates into vanilla JS functions for blazin' fast client-side use.
Simple solution for compiling jade templates into vanilla JS functions for blazin' fast client-side use.
v2 has been released. See the changelog for breaking changes.
Client-side templating is overly complicated, ultimately what you actually want is a function you can call from your JS that puts your data in a template. Why should I have to send a bunch of strings with Mustaches {{}}
or other silly stuff for the client to parse? Ultimately, all I want is a function that I can call with some variable to render the string I want.
So, the question is, what's a sane way to get to that point? Enter jade. Simple, intuitive templating, and happens to be what I use on the server anyway. So... Jade has some awesome stuff for compiling templates into functions. I just built templatizer to make it easy to turn a folder full of jade templates into a CommonJS module that exports all the template functions by whatever their file name.
From my tests it's 6 to 10 times faster than mustache.js with ICanHaz.
npm install templatizer
var templatizer = require('templatizer');
// pass in the template directory and what you want to save the output file as
templatizer(
__dirname + '/templates',
__dirname + '/output.js',
options, // Optional
function (err, templates) { console.log(err || 'Success!') }
);
So a folder like this
/clienttemplates
user.jade
app.jade
/myfolder
nestedTemplate.jade
Compiles down to a JS file that looks something like this:
var jade = require('jade-runtime') // This is a peerDependency
var templates = {};
// a function built from the `user.jade` file
// that takes your data and returns a string.
templates.user = function () {}
// built from the `app.jade` file
templates.app = function () {} // the function
// folders become nested objects so
// myfolder/nestedTemplate.jade becomes
templates.myFolder = {};
templates.myfolder.nestedTemplate = function () {} // the template function
module.exports = templates;
The awesome thing is... they're just functions at this point. Crazy fast, SO MUCH WIN!!!!
templatizer
has jade-runtime
as a peerDependency
. In npm 3.x.x peerDependencies will no longer be installed by default.
When this happens, you'll want to run the following npm install jade-runtime
to install it yourself.
Note: the currently published jade-runtime
only works with the upcoming jade@2.x.y
release. For now templatizer
uses an npm publically scoped module that is a copy of the current runtime @lukekarrys/jade-runtime
. This will be changed once jade@2.x.y is released.
templatizer(
templatesDirectory,
outputFile?,
options?,
function (err, templates) {}
)
A string or an array of paths to look for templates.
The path can also be a glob instead that can be used to match *.jade
files across multiple directories. For example:
templatizer(__dirname + '/app/**/*.jade', ...);
Optionally build the compiled templates to a file. The output will be a CommonJS module. If you don't build to a file, you'll want to supply a callback to do something else with the compiled templates.
jade
(object, default {}
)jade
is an object which will be passed directly to jade.compile()
. See the Jade API documentation for what options are available.
Here's an example where we set the Jade compileDebug
option to true
.
templatizer(templatesDir, outputFile, {
// Options
jade: {
compileDebug: true
}
});
globOptions
(object, default {}
)globOptions
will be passed directly to node-glob
. See the API docs for available options.
transformMixins
(boolean, default false)Set this to true
to turn on mixin
AST transformations.
Jade has a feature called mixins
which when compiled get treated as function declarations within the compiled function. Templatizer can pull these out of the compiled function and place them on the namespace of the parent function. For example:
// users.jade
ul
each user in users
mixin user(user)
mixin user(user)
// Jade mixin content
Templatizer will compile this as
// Compiled fn from file
exports.users = function () {}
// Compiled mixin fn
exports.users.user = function (user) {}
This is helpful as it allows you to call users()
to create your list and then users.user()
to render just a single item in the list.
If the last parameter is a function, it will be treated as a callback. The callback will always have the signature function (err, templates) {}
. Use this to respond to errors or to do something else with the source of the compiled templates file.
This can be helpful if you don't want to write the compiled templates directly to a file, and you want to make modifications first.
Both the outputFile
string and options
object are optional.
// Use just the callback to do something else with your templates
// besides write them to a file
templatizer(templatesDir, function (err, templates) { });
// Build to a file and do something in the callback
templatizer(templatesDir, outputFile, function (err, templates) { });
// Use only with options
templatizer(templatesDir, { /* options */ }, function (err, templates) { });
// Use with options and outputFile
templatizer(templatesDir, outputFile, { /* options */ }, function (err, templates) { });
Simply pass in data objects to make those variables available within the template:
templatizer.Template({ title: ..., description: ...});
Using jade's &attributes(attributes)
syntax:
templatizer.Template.call({ attributes:{ class: ..., value: ...}} , data);
templatizer.Template.apply({ attributes:{ class: ..., value: ...}} , [data]);
Templatizer comes with a bin script to use from makefiles/package.json scripts/etc, it works like this:
$ templatizer -d path/to/templates -o /path/to/output/templates.js
Run npm test
to run the tests (you'll need phantomjs installed). You can also run the tests in your browser with npm start
and going to http://localhost:3003.
2.0.3
2.0.2
@lukekarrys/jade-runtime
2.0.0 Breaking Changes:
function (err, templates) {}
to know when compilation is complete.require.js
conversion tool or amd-wrap
for AMD compatibility or creating a standalone build with browserify
for global builds.jade-runtime
is no longer inlined. jade-runtime
is now installed as a peerDependency
and required from the compiled templates file.namespace
options have been removed. Since the compiled templates no longer have the option to attach to a global variable, the namespace
options are no longer relevant.transformMixins: true
. Also, the dynamic mixin compiler is no automatically turned on if opting-in to mixin transformation.MIT
If you think this is cool, you should follow me on twitter: @HenrikJoreteg
FAQs
Simple solution for compiling jade templates into vanilla JS functions for blazin' fast client-side use.
The npm package templatizer receives a total of 17 weekly downloads. As such, templatizer popularity was classified as not popular.
We found that templatizer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.