Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
eoraptor.js
Advanced tools
A mini expression javascript template engine without any dependence. Compatible with client-side and server-side.
A mini expression javascript template engine without any dependence. Compatible with client-side and server-side.
mustache
.with
statement in compiled function, recognized performance problems will be shielded.index
support when iterating an array.else if
support.mustache
.A quick glance at the unit test maybe the most direct way to dive in.
Including the eoraptor engine by script tag.
<script src="path/to/eoraptor.min.js"></script>
The classic hello world
example achieved through a variety of ways.
var hw = eoraptor.compile("Hello {{=name}}!");
hw({"name": "world"});
// "Hello world!"
Usually, this method is more suitable for compiling a pretty simple template.
text/x-eoraptor
type and an unique id
property.<script type="text/x-eoraptor" id="hw">
Hello {{=name}}!
</script>
<script>
eoraptor.extract();
eoraptor.hw({"name": "world"});
// "Hello world!"
</script>
eoraptor.compile()
or exraptor.extract()
api.<script src="path/to/eoraptor-precompiled.js"></script>
<script>
eoraptor.hw({"name": "world"});
</script>
</html>
The name of eoraptor-precompiled.js
file and the namespace of all templaltes,eoraptor
in the example above, would be any other word as you like(a declaration in options of pre-compiling tool), and the content in the file would look like this:
(function () {
// NOTE: The reality would be more complex than here
var ns = this["namespaceYouLike"] || {};
ns["hw"] = function (data){
var t__ = data, r__ = [];
r__.push("Hello ");
r__.push(t__.name);
r__.push("!");
return r__.join("");
};
// more functions
// ns["foo"] = function (data) {};
// ...
});
NOTE: The reality of namespace declaration
would be more complex than here, more details will be found in pre-compiling section.
eoraptor.compile(template)
/ eoraptor.compile(id, template)
key
for inner cache. If none, the template
itself will be used instead.In order to improve the performance of compiling, this method will save a cache for every template string, when the same template is passed in, it will skip the parsing step and return the cache directly.
The method returns a compiled renderable
function with two properties, the render
property is a reference to the function itself which takes one parameter as the context data. The source
property is only the string form of the render
, used by the pre-compile tool.
Demo:
var fooTpl = eoraptor.compile('foo','{{=foo}}');
// method 1
fooTpl.render(data);
// method 2
fooTpl(data);
// method 3
eoraptor.foo(data);
eoraptor.extract()
All script tags with a "text/x-eoraptor" type and an unique id property will be processed as individual template definitions.
Demo:
<script id="sayMorning" type="text/x-eoraptor">
Good morning, {{=name}}!
</script>
<script id="sayAfternoon" type="text/x-eoraptor">
Good afternoon, {{=name}}!
</script>
<script type="text/javascript">
eoraptor.extract();
typeof eoraptor.sayMorning; // "function"
typeof eoraptor.sayAfternoon; // "function"
</script>
After calling the extract
, the script tag will be added a compiled
attribute, so it would be ignored in next calling.
<script id="sayMorning" type="text/x-eoraptor" compiled="1">
Good morning, {{=name}}!
</script>
eoraptor.setDelimeter(start, end)
Demo:
eoraptor.setDelimiter('<%', '%>');
var tpl = eoraptor.compile('<%this.name%>');
tpl({"name": "eoraptor.js"});
// "eoraptor.js"
{{this.key}}
/ {{this["key"]}}
key
in context data.Under the hood, the function returned by
eoraptor.compile()
is builded withoutwith
statement, so the expression needs to start withthis.
prefix and it will not throw errors likeunderscore
.
Demo: output the value of the key
in context data.
var tpl = eoraptor.compile("{{this.name}}");
tpl.render({"name": "eoraptor.js"});
// "eoraptor.js"
Demo: if there is no such key
in context data.
var tpl = eoraptor.compile("{{this.name}}");
tpl.render({});
// "" empty string
{{-this.key}}
/ {{-this["key"]}}
key
in context data.demo: output the html-escaped value of the key
in context data.
var tpl = eoraptor.compile("{{@this.name}}");
tpl.render({"name": "<h1> eoraptor.js </h1>"});
// "<h1> eoraptor.js </h1>"
START with: {{#anyValue}}
/ {{#this.key}}
/ {{#this["key"]}}
/ {{#anyValue vs anyValue}}
foo
, true
, []
, {}
, etc.key
in context data.==
, ===
, !=
, !==
, >=
, <=
, >
, <
END with: {{/}}
Demo: To determine whether the if()
is like true
, comparing by ==
.
var data = {"foo": 1};
var tpl = eoraptor.compile("{{#this.foo}}like true{{/}}");
tpl.render(data);
// "like true"
Demo: To determine whether the if()
is true
, comparing by ===
.
var data = {"foo": 1};
var tpl = eoraptor.compile("{{#this.foo===true}}is true{{/}}");
tpl.render(data);
// "" empty string
START with: {{^anyValue}}
/ {{^this.key}}
/ {{^this["key"]}}
/ {{^anyValue vs anyValue}}
foo
, true
, []
, {}
, etc.key
in context data.==
, ===
, !=
, !==
, >=
, <=
, >
, <
END with: {{/}}
Demo:
var tpl = eoraptor.compile("the number is {{#this.number === 1}}"+
"one"+
"{{^this.number === 2}}"+
"two"+
"{{/}}");
tpl.render({"number": 2});
// "the number is two"
START with: {{^}}
END with: {{/}}
Demo:
var tpl = eoraptor.compile("the number is {{#this.number === 1}}"+
"one"+
"{{^}}"+
"unknown"+
"{{/}}");
tpl.render({});
// "the number is unknown"
{{#this.key currentItem[ currentKey]}}
key
in context data.k__
, assign a variable to represent the current key in an iterative process. It will be a number
value(like 0, 1, 2, etc.) or a string
value determined by the iterative object, say, array
or object
.Demo: traversal of an array
var data = {
name: "eoraptor",
features: [
"simple",
"standalone"
]
};
var tpl = eoraptor.compile("<ul>"+
"{{#this.features item key}}"+
"<li>{{key}}. {{this.name}} is {{item}}</li>"+
"{{/}}"+
"</ul>");
tpl.render(data);
// "<ul><li>0. eoraptor is simple</li><li>1. eoraptor is standalone</li></ul>"
Demo: enumerating an object
var data = {
features: {
"grammer": "simple",
"dependency": "standalone"
}
};
var tpl = eoraptor.compile("<ul>"+
"{{#this.features item key}}"+
"<li>{{key}}:{{item}}</li>"+
"{{/}}"+
"</ul>");
tpl.render(data);
// "<ul><li>grammer:simple</li><li>dependency:standalone</li></ul>"
{{!comment}}
Demo:
var tpl = eoraptor.compile("{{!hello}}eoraptor.js");
tpl.render(); // "eoraptor.js"
{{/}}
You can see it everywhere above.
{{>partialName[ partialContext]}}
Most of time, each UI compontent in the page is coded by several people, and everyone has the responsibility to keep their code clean, so the key
in the partial template may be the same as each other. As you will see in the code lower, both templates, navi
and slider
, have the list
key, so it will not work correctly with a public context data.
Unless changing
list
intonaviList
andsliderList
, but it's clearly violates the reused principle.
At this time, we can resolve the key
conflict by assignasing an independent context data to partial template when defining a combined one.
Compiling two partial templates for later use, say navi
and slider
:
eoraptor.compile('navi', '<ul>{{#this.list item}}'+
'<li>{{item.text}}</li>'+
'{{/}}</ul>');
eoraptor.compile('slider', '<ul>{{#this.list item}}'+
'<li>{{item.img}}</li>'+
'{{/}}</ul>');
Below we compile and render a combined template, including two partial templates defined above.
var tpl = eoraptor.compile(
'<p>navi:</p>'+
'{{>navi this.navi}}'+
'<p>slider:</p>'+
'{{>slider this.slider}}'
);
tpl.render({
navi: {
list: [
{text: 'foo'}, {text: 'boo'}
]
},
slider: {
list: [
{img: '1.jpg'}, {img: '2.jpg'}
]
}
});
// output:
// <p>navi:</p>
// <ul><li>foo</li><li>boo</li></ul>
// <p>slider:</p>
// <ul><li>1.jpg</li><li>2.jpg</li></ul>
The JavaScript Templates script is released under the MIT license.
@gnosaij / www.joy-studio.com
compile
method support zero parameterextract
methodeoraptor-jst
supportFAQs
A mini expression javascript template engine without any dependence. Compatible with client-side and server-side.
We found that eoraptor.js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.