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

16 Commits

Author SHA1 Message Date
SSantoro
936d5c1d94 v1.0.6 2019-04-18 16:22:44 +02:00
SSantoro
ef5fc54946 use $.extend 2019-04-18 16:06:07 +02:00
SSantoro
df262d40f4 v1.0.5 2019-04-18 15:47:37 +02:00
SSantoro
cd74e87dd3 allow options to be passed in 2019-04-18 15:47:25 +02:00
SSantoro
672b80f5a3 v1.0.4 2019-04-18 12:40:03 +02:00
SSantoro
2cde5f1226 clean up cache right after render 2019-04-18 12:39:51 +02:00
s2
37a2512f4b v1.0.3 2019-04-14 22:05:08 +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
s2
b8c9b8d180 v1.0.1 2019-04-14 18:18:58 +02:00
s2
12db14bdb8 add some stuff to package.json 2019-04-14 18:18:27 +02:00
s2
1284a11acb add install instructions 2019-04-14 18:12:08 +02:00
6 changed files with 89 additions and 29 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

33
.eslintrc.json Normal file
View File

@@ -0,0 +1,33 @@
{"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"]
}
}

View File

@@ -2,13 +2,18 @@
[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`
1. Include this script
```html
<script src="node_modulse/ejs-render-remote/ejs-render-remote.js"></script>
<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'});
@@ -27,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
@@ -33,21 +38,25 @@
var r = uuidv4();
getTemplateFn.then(function(template) {
document.getElementById(r).outerHTML = ejs.render(
var templateOptions = overwriteWithCacheOptions(options, templateUrl);
$('#' + r).replaceWith(ejs.render(
template,
data,
{
cache: true,
filename: templateUrl
}
);
templateOptions
));
//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.
@@ -56,18 +65,14 @@
} else {
$.get(templateUrl)
.then(function(template) {
var templateFn = ejs.compile(template,
{
cache: true,
filename: templateUrl
});
var templateOptions = overwriteWithCacheOptions(options, templateUrl);
var templateFn = ejs.compile(template, templateOptions);
ejs.cache.set(templateUrl, templateFn);
d.resolve();
d.resolve(templateUrl);
});
}
return d;
}
};
})(jQuery);

View File

@@ -1,11 +1,13 @@
{
"name": "ejs-render-remote",
"version": "1.0.0",
"version": "1.0.6",
"description": "",
"main": "index.js",
"homepage": "https://github.com/S2-/ejs-render-remote",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
"author": "s2 <s2@31337.it>",
"license": "ISC",
"keywords": ["ejs", "templating", "client-side"]
}