Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

razorleaf

Package Overview
Dependencies
Maintainers
1
Versions
96
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

razorleaf - npm Package Compare versions

Comparing version 0.13.4 to 0.14.0-beta

compiler.js

21

package.json
{
"name": "razorleaf",
"version": "0.13.4",
"version": "0.14.0-beta",
"main": "razorleaf.js",
"files": [
"razorleaf.js",
"lib/compiler.js",
"lib/parser.js",
"lib/template-utilities.js",
"bin/leaf"
"compiler.js",
"parser.js"
],

@@ -15,3 +13,3 @@ "description": "A template engine for HTML",

"bin": {
"leaf": "bin/leaf"
"leaf": "cli.js"
},

@@ -25,13 +23,2 @@ "repository": {

},
"dependencies": {
"index-format": "~1.0.0"
},
"devDependencies": {
"async": "~0.2.7",
"diff": "~1.0.4",
"cli-color": "~0.2.2"
},
"scripts": {
"test": "node test/test.js"
},
"licenses": [

@@ -38,0 +25,0 @@ {

"use strict";
var compiler = require("./lib/compiler");
var parser = require("./lib/parser");
var __utilities = require("./lib/template-utilities");
var parser = require("./parser");
var compiler = require("./compiler");
var defaults = {
load: function() {
throw new Error("load callback must be specified to include subtemplates.");
}
};
function loadIncludes(tree, visited, options) {
tree.includes.forEach(function(include) {
if(visited.indexOf(include.template) !== -1) {
throw new Error("Circular inclusion: ⤷ " + visited.slice(visited.indexOf(include.template)).join(" → ") + " ⤴");
}
function combine() {
var combined = {};
var includeTree = parser.parse(options.include(include.template));
for(var i = 0; i < arguments.length; i++) {
var obj = arguments[i];
visited.push(include.template);
loadIncludes(includeTree, visited, options);
visited.pop();
for(var k in obj) {
if(obj.hasOwnProperty(k)) {
combined[k] = obj[k];
}
}
}
return combined;
include.children = includeTree.children;
});
}
function compile(template, options) {
options = combine(defaults, options);
var tree = parser.parse(template);
var tree = parser.parse(template, options);
var code = compiler.compile(tree);
loadIncludes(tree, [], options);
return eval("(function(data) {\n" + code + "\n})");
return compiler.compile(tree);
}
module.exports.compile = compile;
module.exports.defaults = defaults;
module.exports.utilties = compiler.utilties;

@@ -1,110 +0,146 @@

[![Build Status](https://travis-ci.org/campersander/razorleaf.png)](https://travis-ci.org/campersander/razorleaf)
Razor Leaf is a template engine for JavaScript with a convenient
indentation-based syntax. It aims, like [Jade], to reduce the redundancy
inherent in HTML — but with simpler rules, a sparser syntax, and a few further
features not found in larger libraries.
Razor Leaf is a template engine for HTML. It is indentation-based and vaguely
resembles [Jade] \(among others).
## Syntax
## Example
### Elements
Elements are defined by their names only; no other special character is
necessary.
```
doctype
p
```
html
head
meta charset: 'utf-8'
```html
<p></p>
```
title 'Hello, world!'
Void elements are recognized automatically.
for script in data.scripts
script type: 'text/javascript' src: '#{script.url}'
if script.async
async:
```
meta
```
for stylesheet in data.stylesheets
link
rel: 'stylesheet'
type: 'text/css'
href: '#{stylesheet.url}'
```html
<meta>
```
body
h1 id: 'title'
'An example'
### Strings
p id: 'content' 'This template demonstrates ' em 'most'
' of Razor Leaf’s features.'
Strings are double-quoted and escaped for use in HTML as needed. Backslash
escape codes can be used as in JavaScript. No whitespace is added
around strings.
!'Literal <abbr title="HyperText Markup Language">HTML</abbr> content can be written using a string with a leading exclamation mark.'
```
"--> A string <--\n" "A string containing \"double-quotes\""
```
```javascript
var fs = require("fs");
var razorleaf = require("razorleaf");
var template = razorleaf.compile(fs.readFileSync("views/template.leaf", "utf8"));
```html
--&gt;A string &lt;--
A string containing "double-quotes"
```
console.log(template(data));
Strings can also contain interpolated sections, delimited by `#{` and `}`.
Both delimiters can be escaped with a backslash.
```
"#{6 * 7}"
```
## API
```html
42
```
### `razorleaf.compile(template, [options])`
If an exclamation mark precedes the string, it and any of its interpolated
sections will not be escaped.
Compiles a template and returns a function that renders the template
and returns the result, taking an optional `data` argument usable inside
the template.
```
!"<!-- A significant comment -->"
```
**`options`** is an object containing options for compilation:
```html
<!-- A significant comment -->
```
- `load`: A function that will be called to load parent and included
templates. It is required if either feature is used.
### Attributes
### `razorleaf.defaults`
Attributes are marked up using the syntax <code><i>name</i>:</code>.
An attribute name can, optionally, be followed by a string to be used as
its value; if a value isn’t provided, the attribute is assumed to be boolean
(and present). Note that a string used as an attributes value cannot be “raw”
— that is, cannot be preceded by an exclamation mark.
An object that contains the default options for use with `razorleaf.compile()`.
These are combined with any options passed to `compile`.
```
meta charset: "utf-8"
```
## Syntax
```html
<meta charset="utf-8">
```
There are four types of “items”.
### Hierarchy
### Elements
Hierarchy in Razor Leaf is defined using indentation. Indentation *must* use
tabs, and not spaces. For example:
An element is defined by a name and may be followed on the same line by any
number of attributes and strings, and up to one element. If the element is not
inline (that is, on the same line as another element), it may followed by an
indented block containing any number of elements, attributes, strings, and
special blocks.
```
html
head
meta charset: "utf-8"
### Attributes
title "Example"
Attributes use the syntax `name: 'value'`. The value is optional, and must be a
string if provided. If a value is not provided, the attribute is assumed to be
boolean. Whitespace between the colon and value is required, as both attribute
and element names may contain colons.
link
rel: "stylesheet"
type: "text/css"
href: "stylesheets/example.css"
### Strings
body
p id: "introduction"
"This template is a brief example of hierarchy."
```
Strings may be delimited by either single or double quotes. Any expression
between `#{` and `}` is interpolated. When interpolating, quotes do not need to
be escaped, but a closing brace (`}`) does. Strings’ contents are escaped as
appropriate. Unescaped strings are marked up with a `!` before the opening
delimiter.
```html
<html><head><meta charset="utf-8"><title>Example</title><link rel="stylesheet"
type="text/css" href="stylesheets/example.css"></head><body><p
id="introduction">This template is a brief example of hierarchy.</p></body></html>
```
Content found after an element on the same line will also be considered that
element’s content.
### Special blocks
- `for (identifier) in (expression)` will evaluate `(expression)` (the
remainder of the line) as JavaScript and iterate over the result. `for`
blocks cannot directly contain attributes.
Some names define special blocks. These are:
- `if (expression)` will evaluate `(expression)` (the remainder of the line) as
JavaScript and include the block if the result is truthy (by the same rules
as JavaScript’s `if`). It may be followed by an `else` block.
- **`doctype`**: Inserts `<!DOCTYPE html>`.
- **`if (condition)`**: Includes its content only if *`condition`* is met.
- **`else`**: Can immediately follow an `if`.
- **`for (identifier) in (collection)`**: Includes its content for each element
in the array or array-like object *`collection`*.
- **`include (name)`**: Loads and includes another template.
- `include (name)` will call `options.load(name)` to retrieve a subtemplate and
insert its contents as if to replace the `include` block.
## API
- `doctype` will insert the string `<!DOCTYPE html>`.
### `razorleaf.compile(template, [options])`
## Upcoming features
Compiles a template string into a function. The compiled function takes
one argument, `data`, which can be used (under that name) in the template.
- Replaceable blocks and template extension
### Options
- **`include(name)`**: A function that should return the template represented
by `name`, as given by any `include` statements in a template. This is
optional if template inclusion is not used.
## leaf
`leaf` is a utility to compile static template files to HTML. It can currently
be passed any number of paths to compile, and will write the result to an HTML
file of the same name. (If the path ends in `.leaf`, it is replaced
with `.html`.)
[Jade]: http://jade-lang.com/
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc