1
0
mirror of https://github.com/S2-/ejs-render-remote synced 2025-08-02 20:50:04 +02:00

30 Commits

Author SHA1 Message Date
s2
2701eb3060 1.0.13 2020-02-12 19:37:44 +01:00
s2
ad9d3b9be9 just throw when called sync 2020-02-12 19:37:36 +01:00
s2
21d9319f97 1.0.12 2020-02-08 22:38:28 +01:00
s2
371d16bc2e s/then/done/ 2020-02-08 22:37:54 +01:00
s2
d53c239f90 no-console 2020-02-08 18:24:09 +01:00
s2
6635965706 1.0.11 2020-02-08 18:22:48 +01:00
s2
dfcf1a4b6f use $.readyException 2020-02-08 18:22:16 +01:00
s2
4b3df71c6b 1.0.10 2020-02-08 17:57:16 +01:00
s2
af63b77c9b add more logs 2020-02-08 17:57:05 +01:00
s2
9aba0176f4 add an example 2020-02-08 17:05:22 +01:00
s2
75401b90dd update ejs 2020-02-08 16:58:53 +01:00
s2
47459bd322 1.0.9 2020-02-07 20:50:08 +01:00
s2
6ed2b48d36 log async ejs compile error to console 2020-02-07 20:50:01 +01:00
s2
23d46035e8 1.0.8 2020-02-07 17:29:59 +01:00
s2
1c720ce9f3 when compile throws an exception, log it to the console 2020-02-07 17:26:54 +01:00
s2
8c9f23ce3f v1.0.7 2019-05-18 15:30:59 +02:00
s2
94d5afe1a0 resolve with templateUrl even when we have it already in cache 2019-05-18 15:30:59 +02:00
s2
f5a44438d1 v1.0.6 2019-05-18 15:30:59 +02:00
s2
06b2289724 use $.extend 2019-05-18 15:30:59 +02:00
s2
9e36c0e82b v1.0.5 2019-05-18 15:30:59 +02:00
s2
6047c6b084 allow options to be passed in 2019-05-18 15:30:59 +02:00
s2
4dd2bfb8a9 v1.0.4 2019-05-18 15:30:59 +02:00
s2
b6e19487b7 clean up cache right after render 2019-05-18 15:30:59 +02:00
s2
6d6def3374 v1.0.3 2019-05-18 15:30:59 +02:00
s2
326e760c78 resolve with the templateUrl on preload 2019-04-14 22:04:52 +02:00
s2
95a1708280 v1.0.2 2019-04-14 21:55:06 +02:00
s2
c4fc133475 outerHTML vs replaceWith 2019-04-14 21:54:34 +02:00
s2
c1f83b7468 add an example 2019-04-14 18:55:14 +02:00
s2
a2bd63ae60 some boilerplate stuff 2019-04-14 18:47:34 +02:00
s2
39238a44d6 typo 2019-04-14 18:38:32 +02:00
10 changed files with 116 additions and 36 deletions

14
.editorconfig Normal file
View File

@@ -0,0 +1,14 @@
# Editor configuration, see http://editorconfig.org
root = true
[*]
charset = utf-8
indent_style = tab
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true
end_of_line = lf
[*.md]
max_line_length = off
trim_trailing_whitespace = false

1
.eslintignore Normal file
View File

@@ -0,0 +1 @@
node_modules

34
.eslintrc.json Normal file
View File

@@ -0,0 +1,34 @@
{"parserOptions":
{"ecmaVersion": 6},
"rules": {
"quotes": [2, "single", {"allowTemplateLiterals": true}],
"curly": [2, "all"],
"keyword-spacing": [2, {"overrides": {"else": {"before": true}, "catch": {"before": true, "after": false}}}],
"space-before-blocks": [2, "always"],
"wrap-iife": [2, "inside"],
"space-before-function-paren": [2, "never"],
"one-var": [2, "never"],
"vars-on-top": 0, "no-empty": [2, {"allowEmptyCatch": true}],
"array-bracket-spacing": [2, "never"],
"space-in-parens": [2, "never"],
"no-underscore-dangle": 0,
"comma-style": [2, "last"],
"comma-spacing": [2, {"before": false, "after": true}],
"space-unary-ops": [2, {"words": false, "nonwords": false}],
"no-multi-spaces": 2,
"space-infix-ops": 2,
"no-with": 2,
"indent": [2, "tab", {"SwitchCase": 1, "FunctionExpression": {"body": 1, "parameters": 1}, "MemberExpression": 0}],
"no-mixed-spaces-and-tabs": 2,
"no-trailing-spaces": 2,
"comma-dangle": [2, "never"],
"semi": [2, "always"],
"brace-style": [2, "1tbs", {"allowSingleLine": true}],
"eol-last": 2,
"dot-notation": 0,
"no-multi-str": 2,
"key-spacing": [2, {"afterColon": true}],
"func-call-spacing": [2, "never"],
"no-console": "warn"
}
}

View File

@@ -2,6 +2,10 @@
[ejs](https://ejs.co/) remote client side includes.
```js
html = ejs.rr('sayhello.ejs', {name: 'Simon'});
```
## Quick start
0. Install with `npm i ejs-render-remote`
@@ -9,7 +13,7 @@
```html
<script src="node_modules/ejs-render-remote/ejs-render-remote.js"></script>
```
2. Creare a file with your template, for example `templates/hello-world.ejs` containing `hello <%= name %>!`
2. Create a file with your template, for example `templates/hello-world.ejs` containing `hello <%= name %>!`
3. Render the remote template:
```js
someDomelement.outerHTML = ejs.rr('templates/hello-world.ejs', {name: 'Simon'});
@@ -28,5 +32,5 @@ The resulting ejs template function is cached, so the second time this function
### ejs.preloadTemplate(templateUrl)
Since `ejs.rr` is async, you can call `ejs.preloadTemplate` before invoking `ejs.rr` to warm the template chace up for that `templateUrl`.
Since `ejs.rr` is async, you can call `ejs.preloadTemplate` before invoking `ejs.rr` to warm the template cache up for that `templateUrl`.
By doing so the call to `ejs.rr` will return the rendered template string right away.

View File

@@ -1,4 +1,4 @@
//this uses jQuery for now because ie11 support is needed (promises and fetch)
//this uses jQuery for now because ie11 support is needed (promises, fetch, Object.assign)
(function($) {
var uuidv4 = function() {
@@ -9,17 +9,22 @@
});
};
ejs.rr = function(templateUrl, data) {
var overwriteWithCacheOptions = function(options, cacheName) {
var cacheOptions = {
cache: true,
filename: cacheName
};
var templateOptions = options || {};
return $.extend(templateOptions, cacheOptions);
};
ejs.rr = function(templateUrl, data, options) {
var templateFn = ejs.cache.get(templateUrl);
//if the template is already cached, return it and we are done
if (templateFn) {
//but first check if there is still a getter function for this template in the cache
//if yes, remove it so we clean up a bit
if (ejs.cache.remove && ejs.cache.get('getFnFor' + templateUrl)) {
ejs.cache.remove('getFnFor' + templateUrl);
}
return templateFn(data);
} else { //if the template is not cached, we need to get it and render it later once we have it. remember: this happens only if the template is not already cached
@@ -32,42 +37,50 @@
}
var r = uuidv4();
getTemplateFn.then(function(template) {
document.getElementById(r).outerHTML = ejs.render(
template,
data,
{
cache: true,
filename: templateUrl
}
);
getTemplateFn.done(function(template) {
var templateOptions = overwriteWithCacheOptions(options, templateUrl);
try {
$('#' + r).replaceWith(ejs.render(
template,
data,
templateOptions
));
} catch(ex) {
$.readyException(ex);
}
//clean up the getFnFor
if (ejs.cache.remove && ejs.cache.get('getFnFor' + templateUrl)) {
ejs.cache.remove('getFnFor' + templateUrl);
}
});
return '<span class="ejs-templateplaceholder" style="display: none;" id="' + r + '"></span>';
}
};
ejs.preloadTemplate = function(templateUrl) {
ejs.preloadTemplate = function(templateUrl, options) {
var d = $.Deferred();
//if the template is already cached, just return.
if (ejs.cache.get(templateUrl)) {
d.resolve();
d.resolve(templateUrl);
} else {
$.get(templateUrl)
.then(function(template) {
var templateFn = ejs.compile(template,
{
cache: true,
filename: templateUrl
});
ejs.cache.set(templateUrl, templateFn);
d.resolve();
.done(function(template) {
try {
var templateOptions = overwriteWithCacheOptions(options, templateUrl);
var templateFn = ejs.compile(template, templateOptions);
ejs.cache.set(templateUrl, templateFn);
d.resolve(templateUrl);
} catch(ex) {
$.readyException(ex);
d.reject(ex);
}
});
}
return d;
}
};
})(jQuery);

2
examples/ejs.min.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -9,6 +9,7 @@
<body>
<h1>ejs-render-remote example</h1>
<p class="hello"></p>
<div class="sometext"></div>
<script src="jquery.min.js"></script>
<script src="ejs.min.js"></script>
@@ -18,6 +19,14 @@
<script>
//render the template
$('.hello').html(ejs.rr('templates/hello.ejs', {name: 'Simon'}));
//a more elaborate example: preload the template to be sure the dom is
//ready when manipulated
ejs.preloadTemplate('templates/somestuff.ejs')
.then(function(t) {
$('.sometext').html(ejs.rr(t)); //this is sync now
$('.stuff').html('hi!');
});
</script>
</body>
</html>

View File

@@ -1 +1 @@
Hello <%= name%>!
Hello <%= name %>!

View File

@@ -0,0 +1 @@
<p class="stuff"></p>

View File

@@ -1,6 +1,6 @@
{
"name": "ejs-render-remote",
"version": "1.0.1",
"version": "1.0.13",
"description": "",
"main": "index.js",
"homepage": "https://github.com/S2-/ejs-render-remote",
@@ -9,5 +9,9 @@
},
"author": "s2 <s2@31337.it>",
"license": "ISC",
"keywords": ["ejs", "templating", "client-side"]
"keywords": [
"ejs",
"templating",
"client-side"
]
}