Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Modern Template Consolidation Engine for EJS, Markdown, Pug, Nunjucks, and Handlebars
render
and renderFromFile
functions for ES6 and Typescript.Follow these steps to add Ecto to a new or existing JavaScript project:
brew install node
The Node.js package manager documentation provides the commands needed to complete the install on Windows and other operating systems.
npm install
to ensure all project dependencies are correctly installed.npm install
yarn
is a package manager you can learn about here.yarn add ecto
const Ecto = require("ecto").Ecto;
let ecto = new Ecto();
OR
const ecto = require("ecto").create();
let source = "<h1>Hello <%= firstName%> <%= lastName %>!</h1>";
let data = {firstName: "John", lastName: "Doe"}
ecto.render(source, data).then((output) => {
console.log(output);
});
After running your program you should be greeted by the following output:
<h1>Hello John Doe!</h1>
You can easily set a different defaultEngine, here we use Handlebars.
let ecto = require("ecto").create({defaultEngine: "handlebars"});
let source = "<h1>Hello {{ firstName }} {{ lastName }}!</h1>";
let data = {firstName: "John", lastName: "Doe"};
ecto.render(source, data).then((output) => {
console.log(output);
});
To render from a template file, Ecto uses the renderFromFile
function. This performs an automatic selection of the engine based on the file extension.
let ecto = require("ecto").create();
let data = { firstName: "John", lastName: "Doe"};
//async renderFromFile(filePath:string, data?:object, rootTemplatePath?:string, filePathOutput?:string, engineName?:string): Promise<string>
ecto.renderFromFile("./path/to/template.ejs", data).then((output) => {
console.log(output)
});
Next Steps:
We decided to focus on the most popular and well-maintained consolidation engines. Unfortunately other engines suffered from packages that were unsupported, making it difficult to validate them as working fully. Some engines also had limited types and lacked ease of use.
Our goal is to support the top engines, handling the vast majority of use cases. Here are the top engines that we support:
Engine | Monthly Downloads | Extensions |
---|---|---|
EJS | .ejs | |
Markdown | .markdown, .md | |
Pug | .pug, .jade | |
Nunjucks | .njk | |
Mustache | .mustache | |
Handlebars | .handlebars, .hbs, .hjs | |
Liquid | .liquid |
The Extensions
are listed above for when we Render from File.
As we have shown in Getting Started -- It's that Easy! You can render in only a couple of lines of code:
let ecto = require("ecto").create();
let source = "<h1>Hello <%= firstName%> <%= lastName %>!</h1>";
let data = {firstName: "John", lastName: "Doe"};
ecto.render(source, data).then((output) => {
console.log(output);
});
Now let's say your desired engine is not EJS, so you want to specify it explicitly. You can either set the defaultEngine parameter, or simply pass it in the render
function. In this case with the popular engine, Handlebars:
let ecto = require("ecto").create();
let source = "<h1>Hello {{firstName}} {{lastName}}!</h1>";
let data = {firstName: "John", lastName: "Doe"}
ecto.render(source, data, "handlebars").then((output) => {
console.log(output);
});
The render
function also can handle partial files for standard engines (markdown excluded) by simply adding the rootTemplatePath
:
let ecto = require("ecto").create();
let source = "<h1>Hello <%= firstName%> <%= lastName %>!</h1><%- include('/relative/path/to/partial'); %>";
let data = {firstName: "John", lastName: "Doe"};
ecto.render(source, data, undefined, "./path/to/templates").then((output) => {
console.log(output);
});
With render
you can also write to a file. This is accomplished by specifying the filePathOutput
parameter as below. It will still return the output as a string
:
let ecto = require("ecto").create();
let source = "<h1>Hello <%= firstName%> <%= lastName %>!</h1>";
let data = {firstName: "John", lastName: "Doe"};
ecto.render(source, data, undefined, undefined, "./path/to/output/file.html").then((output) => {
console.log(output);
});
Notice the undefined
value passed into the engineName
parameter. This is done because we already have the defaultEngine set to EJS. If you want you can easily add it in here too.
To render via a template file, it is as simple as calling the renderFromFile
function with a couple of simple parameters passed in. In this example, we are passing in the template and it will return a string
.
One of the main benefits is that it will automatically select the correct engine based on the file extension.
let ecto = require("ecto").create();
let data = { firstName: "John", lastName: "Doe"};
ecto.renderFromFile("./path/to/template.ejs", data).then((output) => {console.log(output)});
In this example, we are writing the output to a HTML file:
let ecto = require("ecto").create();
let data = { firstName: "John", lastName: "Doe"};
ecto.renderFromFile("./path/to/template.ejs", data, undefined, "./path/to/output/yourname.html")
Notice that in these examples it is using the ./path/to/template.ejs
to specify EJS for the rendering.
You can override the auto-selected engine by passing in the string value of a template engine as a parameter into the renderFromFile
function. We pass in pug
, which states we want to render the template using the Pug engine.
let ecto = require("ecto").create();
let data = { firstName: "John", lastName: "Doe"};
ecto.renderFromFile("./path/to/template.ejs", data, undefined,
"./path/to/output/yourname.html", "pug")
There are several ways to set the default engine to make it flexible. Ecto.defaultEngine
is set by default to ejs
, so if you are using ejs
no need to change anything.
Here is how you set liquid
as the default engine while initializing your class:
let ecto = require("ecto").create({defaultEngine: "liquid"});
Or you can set the default engine as a parameter like so:
const Ecto = require("ecto").Ecto;
let ecto = new Ecto();
ecto.defaultEngine = "mustache";
You can also set the engine as a parameter on the render
function, which will override the Ecto.defaultEngine
parameter:
let ecto = require("ecto").create();
let source = "<h1>Hello {{firstName}} {{lastName}}!</h1>";
let data = {firstName: "John", lastName: "Doe"};
ecto.render(source, data, "handlebars").then((output) => {
console.log(output);
});
You can also override the auto selection on renderFromFile
like so:
let ecto = new Ecto();
let data = { firstName: "John", lastName: "Doe"};
ecto.renderFromFile("./path/to/template.ejs", data, undefined,
"./path/to/output/yourname.html", "pug").then((output) => {
console.log(output)
});
Markdown does not contain complexities such as data objects, or partials and layouts. To render markdown its as simple as:
let ecto = Ecto();
let source = "# markdown rulezz!";
ecto.render(source, undefined, "markdown").then((output) => {
console.log(output) //should be <h1 id="markdown-rulezz">markdown rulezz!</h1>
});
Render by Markdown file:
let ecto = Ecto();
ecto.renderByFile("/path/to/file.md").then((output) => {
console.log(output)
});
With Markdown we have added the following options as they are the most common:
{
pedantic: false,
gfm: true,
breaks: false,
sanitize: false,
smartLists: true,
smartypants: false,
xhtml: false
}
You can read more about them here.
In Ecto we use the handlebars engine to render mustache
related templates. This is because handlebars is based on mustache with just more additional features.
Handlebars is a fantastic template engine, and we've incorporated helpers to make it even better. We added in handlebars-helpers so you can format dates, and more. Here is an example using Handlebars Helpers
in your template:
let ecto = Ecto();
let source = "{{year}}";
ecto.render(source, undefined, "handlebars").then((output) => {
console.log(output)
});
The API is focused on using the main Ecto
class:
const Ecto = require("Ecto").Ecto;
let ecto = new Ecto();
//ecto.<API> -- functions and parameters
async
) - Render from a string.
Ecto.defaultEngine
parameterpartials
and layouts
async
) - Renders from a file path and will auto-select what engine to use based on the file extension. It will return a Promise<string>
of the rendered output.
partials
and layouts
ejs
To make it easier to access and change between engines, all supported engines are provided as parameters on the Ecto
class as Ecto.<EngineFullName>
let ecto = Ecto();
console.log(ecto.Handlebars.name); // will return "handlebars"
console.log(ecto.Handlebars.opts); // will return "handlebars" options object
To access a specific engine you can do so by going to ecto.<engine_name>.engine
and setting the SafeString:
let ecto = Ecto();
ecto.Handlebars.engine.SafeString("<div>HTML Content!</div>");
This is an open-source project under MIT License. If you would like to get involved and contribute to this project, simply follow these steps:
Pull requests are used in open-source projects or in some corporate workflows to manage changes from contributors and to initiate code review before such changes are merged.
By creating a pull request, you tell others about the changes you've pushed to your fork of a GitHub repository, so that the maintainers of the original repository can review your changes, discuss them, and integrate them into the base branch.
Issues can be used to keep track of bugs, enhancements, or other requests. Issues can be created based on code from pull requests, comments, or created from the main repository page.
FAQs
Modern Template Consolidation Engine for EJS, Markdown, Pug, Nunjucks, Liquid, and Handlebars
The npm package ecto receives a total of 426 weekly downloads. As such, ecto popularity was classified as not popular.
We found that ecto 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
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
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.