Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
@tsed/engines
Advanced tools
Template engines library. Provide an API to adding custom engines from decorators.
Template engine consolidation library (inspired from consolidate) written in TypeScript.
npm install @tsed/engines
Some package has the same key name, @tsed/engines will load them according to the order number.
For example with dust, @tsed/engines will try to use in this order: dustjs-helpers
and dustjs-linkedin
.
If dustjs-helpers
is installed, dustjs-linkedin
will not be used by consolidate.
NOTE: you must still install the engines you wish to use, add them to your package.json dependencies.
You can get an engine by using engines
:
import {getEngine} from '@tsed/engines';
getEngine('swig')
.renderFile({ user: 'tobi' })
.then((html) => {
console.log(html);
})
Or render directly a template:
import {getEngine} from '@tsed/engines';
getEngine('ejs')
.render('<p><%= user.name %></p>')
.then((html) => {
console.log(html);
})
To enable caching simply pass { cache: true }
. EnginesContainer may use this option to cache things reading the file contents, compiled Function
s etc.
EnginesContainer which do not support this may simply ignore it. All engines that consolidate.js implements I/O for will cache the file contents, ideal for production environments.
When using @tsed/engines directly: getEngine('swig').renderFile('views/page.html', { user: 'tobi', cache: true });
Using supported Express versions: app.locals.cache = true
or set NODE_ENV to 'production' and Express will do this for you.
@tsed/engines
can be used with Express as following:
import express from 'express';
import {getEngine} from '@tsed/engines';
// assign the swig engine to .html files
app.engine('html', getEngine('swig'));
// set .html as the default extension
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
var users = [];
users.push({ name: 'tobi' });
users.push({ name: 'loki' });
users.push({ name: 'jane' });
app.get('/', function(req, res){
res.render('index', {
title: 'Ts.ED EnginesContainer'
});
});
app.get('/users', function(req, res){
res.render('users', {
title: 'Users',
users: users
});
});
app.listen(3000);
console.log('Express server listening on port 3000');
@tsed/engines
can be used with Koa as following:
import {getEngines} from "./getEngines";
var views = require('koa-views');
const render = views(__dirname + '/views', {
engineSource: getEngines(),
map: {
html: 'underscore'
}
})
// Must be used before any router is used
app.use(render)
// OR Expand by app.context
// No order restrictions
// app.context.render = render()
app.use(async function (ctx) {
ctx.state = {
session: this.session,
title: 'app'
};
await ctx.render('user', {
user: 'John'
});
});
Template engines are exposed via the requires
object, but they are not instantiated until you've called the getEngine(engine).render()
method.
You can instantiate them manually beforehand if you want to add filters, globals, mixins, or other engine features.
import {requires} from 'consolidate';
import nunjucks from 'nunjucks';
// add nunjucks to requires so filters can be
// added and the same instance will be used inside the render method
requires.set('nunjucks', nunjucks.configure());
requires.get('nunjucks').addFilter('foo', () => {
return 'bar';
});
@tsed/engines
let you register your own engine by using the @ViewEngine
decorator. Here an is example of
pug engine implementation:
import {Engine, ViewEngine} from "@tsed/engines";
@ViewEngine("pug", {
requires: ["pug", "then-pug"] // multiple require is possible. Ts.ED will use the first module resolved from node_modules
})
export class PugEngine extends Engine {
protected $compile(template: string, options: any) {
return this.engine.compile(template, options);
}
protected async $compileFile(file: string, options: any) {
return this.engine.compileFile(file, options);
}
}
See more examples in packages/engines/src/components
directory.
import {PugEngine} from "@tsed/engines";
@ViewEngine("pug", {
requires: ["pug", "then-pug"] // multiple require is possible. Ts.ED will use the first module resolved from node_modules
})
export class CustomePugEngine extends PugEngine {
protected $compile(template: string, options: any) {
return super.$compile(template, options);
}
protected async $compileFile(file: string, options: any) {
return super.$compileFile(file, options);
}
}
exports.nunjucks.render
function in packages/engines/src/components/NunjuncksEngine.ts
. You can pass your own engine/environment via options.nunjucksEnv
, or if you want to support Express you can pass options.settings.views
, or if you have another use case, pass options.nunjucks
(see the code for more insight).options.partials
options.loader
.React
To render content into a html base template (eg. index.html
of your React app), pass the path of the template with options.base
.Please read contributing guidelines here
Thank you to all our backers! 🙏 [Become a backer]
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]
The MIT License (MIT)
Copyright (c) 2016 - 2021 Romain Lenzotti
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.
FAQs
Template engines library. Provide an API to adding custom engines from decorators.
The npm package @tsed/engines receives a total of 8,984 weekly downloads. As such, @tsed/engines popularity was classified as popular.
We found that @tsed/engines demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 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
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
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.