Code Monkey home page Code Monkey logo

pejs's Introduction

PEJS

PEJS is pre-compiled EJS with a inheritance, blocks and file support that works both in the client and on the server. It's available through npm:

npm install pejs

Usage

PEJS is easy to use:

var pejs = require('pejs');
var views = pejs(); // pass in {compress:true} to get compressed output

views.render('./example.ejs', function(err, result) {
	// renders example.ejs into a string
	console.log(result);
});
views.parse('./example.ejs', function(err, src) {
	// parses the template and compiles it down to portable js
	// this means it works in the client!
	console.log(src);
});

PEJS has an internal cache of parsed templates which means that when you render a template twice it will only parse it once.

It also makes sure to clear this cache if the template has changed in anyway on the disk

Options

The pejs() init function supports the following options:

  • compress: Compress the template output. Default: false
  • basedir: Base directory for template resolution. Default: cwd
  • watch: Watch templates for changes. Default: true

Path resolution

PEJS uses a similar file/module resolution as node.js.

  • views.render('./file'): pejs will look for file.pejs, file.ejs, file.html, file/index.pejs, file/index.ejs or file/index.html.
  • views.render('template'): pejs will look for for template in in the nearest views folder using the same scheme as above.

This is almost exactly the same as node does with it's node_modules resolution.

Classic EJS

PEJS templates has your usual EJS syntax with <% and %>. Read more about EJS here

  • inline code: <% var a = 42; %>
  • insert: <%- data %>
  • insert and html escape: <%= data %>

Blocks

PEJS expands the original EJS syntax by letting you declare blocks using the <%{ syntax. A block is basically a partial template that optionally can be loaded from a file.

  • declare block: <%{{ blockName }}%>
  • declare file block: <%{ './filename.html' }%>
  • override block: <%{ blockName %>hello block<%} %>

In general all block can be loaded from a file instead of being defined inline by providing a filename:

  • declare block: <%{{ myBlock './example.ejs' }}%>
  • override block: <%{ myOverrideBlock 'example.ejs' }%>

If you want include a block using a different set of locals than in you current scope you pass these as the last argument to the block.

  • declare block: <%{{ myBlock {newLocalsArg:oldLocalsArg} }}%>
  • override block: <%{ './example.ejs' {newLocalsHere:oldLocalsArg} }%>

All filepaths above are subject to the same path resolution as decribed in the previous section.

Inheritance

Using blocks it's easy to implement template inheritance. Just declare a base.html with some anchored blocks:

<body>
	Hello i am base
	<%{{ content }}%>
</body>

Then a child.html that renders base.html

<%{ './base.html' }%>
<%{ content %>
	i am inserted in base
<%} %>

To render the example just render child.html

pejs.render('./child.html', function(err, result) {
	console.log(result);
});

The above outputs:

<body>
	Hello i am base
	i am inserted in base
</body>

License

MIT

pejs's People

Contributors

jmosbech avatar mafintosh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pejs's Issues

problem resolving paths

my file structure is

root
    - server.js
    - web/
        - templates/
           - index.ejs

i configured pejs with

{
    basedir: 'web/templates'
}

then when i do pejs.render('index') i always get Error: Cannot find module 'index'

i've tried to set the path to

web/templates
/web/templates
./web/templates

and also with trailing slash, nothing helps

where's the problem?

Locals aren't inherited when using file blocks with new locals

When doing the following in a base file.

<%{ './template.html' { newLocal: 1 } }%>

The only accessible local variable in template.html is newLocal, even though there are more locals defined in the base file.

Expected to have all locals from base with newLocal in template.html.

Error thrown when closing blocks in diffrent ways

/node_modules/pejs/codegen.js:38
        tree.forEach(function(node) {
             ^
TypeError: Cannot call method 'forEach' of undefined
    at stringify (/node_modules/pejs/codegen.js:38:8)
    at /node_modules/pejs/codegen.js:59:54
    at Array.forEach (native)
    at stringify (/node_modules/pejs/codegen.js:38:8)
    at /node_modules/pejs/codegen.js:59:54
    at Array.forEach (native)
    at stringify (/node_modules/pejs/codegen.js:38:8)
    at codegen (node_modules/pejs/codegen.js:67:12)
    at /node_modules/pejs/index.js:93:48
    at /node_modules/pejs/sequence.js:11:12

The above error is thrown when trying to render the following template.

<%{ header %>
<% }%>

<%{ body %>
<%} %>

It somehow related to how the blocks are closed. The below template, for example, doesn't throw (notice how the blocks are closed).

<%{ header %>
<%} %>

<%{ body %>
<% }%>

Multiple levels of inheritance aren't supported

I've been keeping my main site header/footer in a single file which all other files inherit from. However, one section of the site I'm working on has multiple pages that require the same sub-header. When I try to make that sub-header a file that inherits from my main file and then make a page that inherits from the subheader, it won't render.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.