Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow simplified usage of include, includeFile #17

Merged
merged 3 commits into from Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -171,15 +171,15 @@ Display this instead
### Partials

```ejs
<%~ E.include('mypartial') %>
<%~ include('mypartial') %>
```

```ejs
<%~ E.includeFile('./footer') %>
<%~ includeFile('./footer') %>
```

```ejs
<%~ E.include('users', {users: it.users}) %>
<%~ include('users', {users: it.users}) %>
```

## ✔️ Tests
Expand Down
14 changes: 7 additions & 7 deletions browser-tests/demo.html
Expand Up @@ -154,7 +154,7 @@ <h2>
<input id="strip" type="checkbox" checked="checked" />
<label for="strip">Strip whitespaces</label>
</div> -->
<div style="clear:both;height:10px"></div>
<div style="clear: both; height: 10px"></div>
<div class="templategroup">
<h4>Template</h4>
<textarea autocomplete="off" id="template">
Expand All @@ -173,15 +173,15 @@ <h4>Template</h4>
<% } %>
<% } %>

Partial: <%~ E.include("mypartial") %>
Partial: <%~ include("mypartial") %>
</textarea
>
</div>
<div class="functiongroup">
<h4>Eta.compile()</h4>
<div id="function"></div>
</div>
<div style="clear:both"></div>
<div style="clear: both"></div>
<div class="datagroup">
<h4>Data</h4>
<textarea autocomplete="off" id="data">
Expand All @@ -204,18 +204,18 @@ <h4>Result</h4>
window.onload = function () {
Eta.templates.define('mypartial', Eta.compile('This is a partial'))

function escape (str) {
function escape(str) {
// To handle escaping for the function result
var escMap = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#39;',
'/': '&#x2F;'
'/': '&#x2F;',
}

function replaceChar (s) {
function replaceChar(s) {
return escMap[s]
}
var newStr = String(str)
Expand All @@ -226,7 +226,7 @@ <h4>Result</h4>
}
}

function render () {
function render() {
console.clear()
var options = JSON.parse('{' + document.getElementById('data').value + '}')
console.log(JSON.stringify(options))
Expand Down
6 changes: 3 additions & 3 deletions deno_dist/README.md
Expand Up @@ -171,15 +171,15 @@ Display this instead
### Partials

```ejs
<%~ E.include('mypartial') %>
<%~ include('mypartial') %>
```

```ejs
<%~ E.includeFile('./footer') %>
<%~ includeFile('./footer') %>
```

```ejs
<%~ E.include('users', {users: it.users}) %>
<%~ include('users', {users: it.users}) %>
```

## ✔️ Tests
Expand Down
7 changes: 5 additions & 2 deletions deno_dist/compile-string.ts
Expand Up @@ -14,14 +14,17 @@ import { AstObject } from "./parse.ts";
*
* ```js
* compileToString("Hi <%= it.user %>", eta.config)
* // "var tR='';tR+='Hi ';tR+=E.e(it.user);if(cb){cb(null,tR)} return tR"
* // "var tR='',include=E.include.bind(E),includeFile=E.includeFile.bind(E);tR+='Hi ';tR+=E.e(it.user);if(cb){cb(null,tR)} return tR"
* ```
*/

export default function compileToString(str: string, config: EtaConfig) {
var buffer: Array<AstObject> = Parse(str, config);

var res = "var tR=''\n" +
var res = "var tR=''" +
(config.include ? ",include=E.include.bind(E)" : "") +
(config.includeFile ? ",includeFile=E.includeFile.bind(E)" : "") +
"\n" +
(config.useWith ? "with(" + config.varName + "||{}){" : "") +
compileScope(buffer, config) +
"if(cb){cb(null,tR)} return tR" +
Expand Down
2 changes: 1 addition & 1 deletion deno_dist/compile.ts
Expand Up @@ -27,7 +27,7 @@ export type TemplateFunction = (
* let compiledFn = eta.compile("Hi <%= it.user %>")
* // function anonymous()
* let compiledFnStr = compiledFn.toString()
* // "function anonymous(it,E,cb\n) {\nvar tR='';tR+='Hi ';tR+=E.e(it.user);if(cb){cb(null,tR)} return tR\n}"
* // "function anonymous(it,E,cb\n) {\nvar tR='',include=E.include.bind(E),includeFile=E.includeFile.bind(E);tR+='Hi ';tR+=E.e(it.user);if(cb){cb(null,tR)} return tR\n}"
* ```
*/

Expand Down
6 changes: 5 additions & 1 deletion deno_dist/config.ts
Expand Up @@ -50,6 +50,10 @@ export interface EtaConfig {
"view cache"?: boolean;
/** Make data available on the global object instead of varName */
useWith?: boolean;
/** Function to include templates by name */
include?: Function;
/** Function to include templates by filepath */
includeFile?: Function;
[index: string]: any; // eslint-disable-line @typescript-eslint/no-explicit-any
}

Expand All @@ -64,7 +68,7 @@ export type PartialConfig = Partial<EtaConfig>;
/**
* Include a template based on its name (or filepath, if it's already been cached).
*
* Called like `E.include(templateNameOrPath, data)`
* Called like `include(templateNameOrPath, data)`
*/

function includeHelper(
Expand Down
2 changes: 1 addition & 1 deletion deno_dist/file-helpers.ts
Expand Up @@ -11,7 +11,7 @@ interface GenericData {
/* END TYPES */

/**
* Called with `E.includeFile(path, data)`
* Called with `includeFile(path, data)`
*/

export function includeFileHelper(
Expand Down
11 changes: 7 additions & 4 deletions dist/browser/eta.dev.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/browser/eta.dev.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/browser/eta.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/browser/eta.min.js.map

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions dist/eta.cjs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/eta.cjs.js.map

Large diffs are not rendered by default.