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

odesza

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

odesza - npm Package Compare versions

Comparing version 0.0.7 to 0.1.0

test/fixtures/answers/answer1

53

index.js

@@ -11,6 +11,6 @@ /**

var vm = require('vm');
var p = require('path');
var odesza = {};
var blocks = {};
var keywords = ['extends', 'block', 'include'];

@@ -43,3 +43,3 @@ /**

// loop over block statements, putting them into memory
s.blocks.forEach(block => {
s.block.forEach(block => {

@@ -50,22 +50,25 @@ // if the block is in memory, this means there is multiple inheritence,

let start = template.indexOf(block) + block.length;
let end = template.indexOf(block.replace('block', 'end'));
// gets the content between the block statements
let start = template.indexOf(`block ${block}`) + `block ${block}`.length;
let end = template.indexOf(`endblock`, start);
if (end == -1) {
throw new Error(`end statement required after ${block}`);
throw new Error(`'endblock' statement required after ${block}`);
}
blocks[block] = template.substr(start, end - start).trim();
});
let path = `${basePath}${s.extend[0].split('\'')[1]}`;
template = odesza.compile(path, options);
let extendPath = `${basePath}${s.extend[0]}`;
template = odesza.compile(extendPath, options);
s = statements(template);
} else {
s.blocks.forEach(block => {
s.block.forEach(block => {
if (blocks[block] != null) {
template = template.split(block).join(blocks[block]);
template = template.split(`block ${block}`).join(blocks[block]);
delete blocks[block];
} else {
// if not in memory, it is a block statement that can be ignored
template = template.split(block).join('');
template = template.split(`block ${block}`).join('');
}

@@ -76,9 +79,11 @@ });

// recursively replace each import statement with its compiled template
s.includes.forEach(statement => {
let path = `${basePath}${statement.split('\'')[1]}`;
template = template.split(statement).join(odesza.compile(path, options));
s.include.forEach(statement => {
let path = `${basePath}${statement}`;
template = template
.split(`include ${statement}`)
.join(odesza.compile(path, options));
});
try {
return vm.runInNewContext('`' + template + '`', options);
return vm.runInNewContext('`' + template + '`', options).trim();
} catch (e) {

@@ -125,13 +130,16 @@ throw new Error(e);

// matches keyword statements
const rstr = `(${keywords.join('|')})\\([\\\'\\\"]([\\w.\\/]+)[\\\'\\\"]\\)`;
const keyWordsRegex = new RegExp(rstr, 'g');
const re = /(block|extend|include) ([\/\.\w]+)/g;
// extracts statements from template
const statements = template => {
var statements = {};
var m = template.match(keyWordsRegex) || [];
statements.extend = m.filter(s => s.indexOf('extend') == 0);
statements.blocks = m.filter(s => s.indexOf('block') == 0);
statements.includes = m.filter(s => s.indexOf('include') == 0);
return statements;
var s = {
extend: [],
block: [],
include: []
};
var m;
while ((m = re.exec(template)) != null) {
s[m[1]].push(m[2]);
}
return s;
};

@@ -144,2 +152,3 @@

}
path = p.resolve(path);
if (!fs.existsSync(path)) {

@@ -146,0 +155,0 @@ if (fs.existsSync(`${path}.ode`)) {

{
"name": "odesza",
"version": "0.0.7",
"version": "0.1.0",
"description": "Write clean, expressive templates with just HTML and inline JavaScript",

@@ -5,0 +5,0 @@ "main": "index.js",

# Odesza
**Odesza** allows you to write clean, expressive templates with just HTML and inline JavaScript without any learning curve.
Odesza allows you to write clean, expressive templates with inline JavaScript. It offers the flexibility of multiple inheritance and inline programming logic with the simplicity of writing plain HTML and JS.
It offers
- multiple inheritence
**Offers**
- multiple inheritance
- variables
- fully expressive inline JavaScript
- native support for Express framework
- support for Express
- no magic, 0 dependencies, and just 150 lines of code
It does NOT offer
- HTML shorthand
- special functions
##Install
#Install
```
npm install odesza --save
```
##Render
# Variables
Variables are passed in when Odesza templates are rendered. Scope is maintained through includes and extends.
```javascript
var odesza = require('odesza');
var vars = {
name: 'world'
name = 'world'
};
odesza.render('hello ${name}!', vars); // hello world!
odesza.render('hello, ${name}', vars); // hello world
```
##Compile
Compile odesza files. Odesza first tries the literal path given, then *.ode*, then *.odesza*.
*index.odesza*
#Inline JS
Odesza makes it easy to write inline JavaScript in your templates. Under the hood, templates are evaluated as ES6 template strings, which means you have access to `${}` expressions.
**code**
```javascript
var vars = {
names: ['wells', 'joe', 'dom']
};
odesza.compile('greetings.ode', vars);
```
hello ${name}!
**greetings.ode**
```javascript
<h2>welcome ${names.join(', ')}!</h2>
${(() => {
// this is a self-executing function expression inside an ES6 template string.
// essentially that means you can write any inline js you want here. the
// following code is to demonstrate how you can programatically generate HTML.
var items = [];
names.forEach((name, index) => {
items.push(`<div>${index + 1}: ${name}</div>`)
});
return items.join('<br/>');
})()}
```
**output**
```html
<h2>welcome wells, joe, dom!</h2>
<div>1: wells</div><br/>
<div>2: joe</div><br/>
<div>3: dom</div>
```
# Partials
**welcome.ode**
```javascript
var odesza = require('odesza');
welcome, ${name}!
```
**question.ode**
```javascript
include welcome
would you like to play a game, ${name}?
```
**code**
```javascript
var vars = {
name: 'world'
name: 'foo'
};
odesza.compile('index', vars); // hello world!
odesza.compile('question', vars);
```
##Express
server.js
**output**
```
welcome, foo!
would you like to play a game, foo?
```
# Inheritance
Odesza gives you access to multiple inheritance through extending templates and block scopes.
**layout.ode**
```jade
<!doctype html>
<html>
<head>
<title>${title}</title>
block js
</head>
<body>
block content
</body>
</html>
```
**page.ode** (extends layout.ode)
```html
extend layout
block js
<script src="${base_path}/page.js"></script>
endblock
block content
<p>
Some content.
</p>
endblock
```
**extended_page.ode** (extends page.ode, overwrites 'content' block)
```html
extend page
block content
<p>
Overwritten content.
</p>
endblock
```
**code**
```javascript
app.set('view engine', 'odesza');
var vars = {
title: 'hello world',
base_path: 'public/js'
};
odesza.compile('extended_page.ode', vars);
```
**output**
```html
<!doctype html>
<html>
<head>
<title>hello world</title>
<script src="public/js/page.js"></script>
</head>
<body>
<p>
Overwritten content.
</p>
</body>
</html>
```
#Express Support
**index.js**
```javascript
app.set('view engine', 'ode');
app.engine('.ode', require('odesza').__express);
```
**controller**
```javascript
res.render('template', {
foo: 'bar'
});
```
#License
MIT

@@ -7,2 +7,3 @@ 'use strict';

const path = require('path');
const diff = require('diff');
const fs = require('fs');

@@ -26,6 +27,5 @@ const fixture = file => path.join(__dirname, 'fixtures', file);

test('file with variables', t => {
let vars = { name : 'yo' };
let vars = { name : 'world' };
let string = odesza.compile(fixture('messages/message1'), vars);
console.log(string);
t.ok(string == 'hello yo!', 'variables work');
t.ok(string == 'hello world 1!', 'variables work');
t.end();

@@ -35,5 +35,5 @@ });

test('include single file', t => {
let vars = { name: 'yo' };
let vars = { name: 'world' };
let string = odesza.compile(fixture('messages/message3'), vars);
t.ok(string == 'yo3hello yo!', 'single file was included');
t.ok(string == 'hello world 1!', 'single file was included');
t.end();

@@ -43,6 +43,6 @@ });

test('multiple recursive include statements', t => {
let vars = { name: 'yo' };
let vars = { name: 'world' };
let string = odesza.compile(fixture('includes'), vars);
console.log(string);
t.ok(string == 'hello yo!yo2yo3hello yo!', 'multiple files were included recursively');
let answer = fs.readFileSync(fixture('answers/answer1')).toString().trim();
t.ok(string == answer, 'multiple files were included recursively');
t.end();

@@ -57,3 +57,3 @@ });

};
let template = odesza.compile(fixture('content.odesza'), vars).split('\n').join('').trim();
let template = odesza.compile(fixture('content.odesza'), vars);
var answer = fs.readFileSync(fixture('answers/complex_answer1')).toString().trim();

@@ -70,3 +70,3 @@ t.ok(template == answer, 'extended template with block statement');

};
let template = odesza.compile(fixture('extend_content.odesza'), vars).split('\n').join('').trim();
let template = odesza.compile(fixture('extend_content.odesza'), vars);
var answer = fs.readFileSync(fixture('answers/complex_answer2')).toString().trim();

@@ -77,2 +77,14 @@ t.ok(template == answer, 'multiple inheritence with extends');

test('extending and including from parent directories', t => {
let vars = {
title: 'hello',
name: 'world',
basePath: 'public/js/'
};
let template = odesza.compile(fixture('messages/dir/dir/message5'), vars);
let answer = fs.readFileSync(fixture('answers/complex_answer5')).toString().trim();
t.ok(template == answer, 'files can be extended and included from parent directories');
t.end();
});
test('list content using native array methods', t => {

@@ -85,6 +97,17 @@ let names = ['wells', 'joe', 'dom'];

};
let template = odesza.compile(fixture('complex_functions'), vars).split('\n').join('').trim();
let template = odesza.compile(fixture('simple_js'), vars);
var answer = fs.readFileSync(fixture('answers/complex_answer3')).toString().trim();
t.ok(template == answer, 'complex js functions work inline')
t.ok(template == answer, 'simple js functions (map) work inline');
t.end();
});
test('complex inline js', t => {
let names = ['wells', 'joe', 'dom'];
let vars = {
names: names
};
let template = odesza.compile(fixture('inline_js'), vars);
let answer = fs.readFileSync(fixture('answers/complex_answer4')).toString().trim();
t.ok(template == answer, 'complex inline js works');
t.end();
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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