npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

extended-ejs

v1.0.1

Published

The extended EJS template

Downloads

13

Readme

Extended EJS

npm version license dependencies Status Known Vulnerabilities

Extended EJS works as a wrapper and pre-compiler for the well-known EJS template, which aims to provide the missing support for blocks and the inheritance in EJS.

It's quite simple and won't place side effects on your original EJS templates.

Install

Run npm install extended-ejs or yarn add extended-ejs.

Examples

Here, assume we got two files.

<!-- skeleton.ejs -->

<h1>[% @block heading %]</h1>

[% @block content %]
<!-- index.ejs -->

[% @extends "./skeleton.ejs" %]

[% @impl heading %]
Hello <span style="color: red">world</span>
[% @end %]

[% @block content %]
<h3>My name is <%= name %>.</h3>
[% @end %]

Use renderFile to render a file and write the rendered result to an output file.

Remember that Extended EJS is only works as a pre-compiler – that is, you can pass local variables to your template and Extended EJS renders your template exactly like what EJS does.

const { renderFile } = require('extended-ejs');

renderFile('index.ejs', { name: 'Makito' }, 'index.html');

The final index.html should be like...

<!-- index.html -->

<h1>Hello <span style="color: red">world</span></h1>

<h3>My name is Makito.</h3>

Statements

A valid statement looks like [% @ACTION PARAMETER %].

Extended EJS uses [% and %] to wrap a statement.

An action is always prefixed with an @.

Every statement will only have one or zero parameters.

The parameter can be wrapped with '' or "". But you can also omit the quotation marks as long as the parameter does not contain any whitespaces.

Extension

With @extends, you can extend a skeleton in your template.

Usage:

[% @extend ./layout_to_extend.ejs %]

One template can only have one or zero @extends statement

Block

With @block, you can define blocks in your template.

Usage:

  • [% @block aliasForBlock %]

  • [% @block "wrap this if you want to play with spaces" %]

Every block definition must have a corresponding implementation, otherwise it will be considered to be an error. If a block in the extended template has no implementation and the strict mode is enabled, the pre-compiler will also throw an error.

Implementation

With @impl, you can implement blocks that you've already defined in your template.

Usage:

Use [% @impl aliasForBlock %] to begin an implementation for a block.

Don't forget to end your implementation with a [% @end %].

Implementing the same block alias twice will be regarded as an error. An error will also be thrown if the extended template has already implemented the block alias that the one who extends attempts to implement when the strict mode is enabled.

Compile

Usage:

compile(input, workDir?, fileRef?, strict? = false)

Pre-compile the string presented template and make it reusable.

Example:

const template = `<h1>[% @block content %]</h1>[% @impl content %]Hello <%= name %>[% @end %]`;
const compiled = compile(template, null, null);

console.log(compiled.render({ name: 'Makito' }));
// outputs: <h1>Hello Makito</h1>

console.log(compiled.render({ name: 'Makino' }));
// outputs: <h1>Hello Makino</h1>

Render

Strings

Usage:

render(input, localVars, outputFile?, strict? = false)

Files

Usage:

renderFile(inputFile, localVars, outputFile?, strict? = false)

Both methods above return the rendered template as a string, with local variables rendered.

Rendered result will be written to outputFile if it is not null.

License

2018 © Makito Yu

Licensed under the beloved MIT License.