Skip to content

Commit

Permalink
Move away from using @ for partials
Browse files Browse the repository at this point in the history
  • Loading branch information
nebrelbug committed Apr 10, 2020
1 parent 515256c commit 48a0c80
Show file tree
Hide file tree
Showing 20 changed files with 29 additions and 67 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,15 @@ Display this instead
### Partials

```
<% @includeFile('footer') %>
<%~ E.include('mypartial') %>
```

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

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

## ✔️ Tests
Expand Down
2 changes: 1 addition & 1 deletion browser-tests/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ <h4>Template</h4>
<% } %>
<% } %>

Partial: <% @include("mypartial") %>
Partial: <%~ E.include("mypartial") %>
</textarea
>
</div>
Expand Down
13 changes: 1 addition & 12 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: 1 addition & 12 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.

13 changes: 1 addition & 12 deletions dist/eta.es.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.es.js.map

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion dist/types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export interface EtaConfig {
interpolate: string;
raw: string;
exec: string;
special: string;
};
e: (str: string) => string;
plugins: Array<{
Expand Down
2 changes: 1 addition & 1 deletion dist/types/parse.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EtaConfig } from './config';
export declare type TagType = 'r' | 'e' | 'i' | 's' | '';
export declare type TagType = 'r' | 'e' | 'i' | '';
export interface TemplateObject {
t: TagType;
val: string;
Expand Down
2 changes: 1 addition & 1 deletion examples/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var template = `
Partial
-------
<% @include('template1', {name: 'Ben'})%>
<%~ E.include('template1', {name: 'Ben'})%>
`

Eta.templates.define('template1', Eta.compile('Hi <%=it.name%>'))
Expand Down
3 changes: 0 additions & 3 deletions src/compile-string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ function compileScope (buff: Array<AstObject>, env: EtaConfig) {
} else if (type === 'e') {
// execute
returnStr += content + '\n' // you need a \n in case you have <% } %>
} else if (type === 's') {
returnStr += 'tR+=E.' + content + ';'
// reference
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export interface EtaConfig {
interpolate: string
raw: string
exec: string
special: string
}
e: (str: string) => string
plugins: Array<{ processFnString?: Function; processAST?: Function }>
Expand Down Expand Up @@ -56,7 +55,6 @@ var defaultConfig: EtaConfig = {
parse: {
interpolate: '=',
raw: '~',
special: '@',
exec: ''
},
async: false,
Expand Down
11 changes: 2 additions & 9 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { trimWS } from './utils'

import { EtaConfig } from './config'

export type TagType = 'r' | 'e' | 'i' | 's' | ''
export type TagType = 'r' | 'e' | 'i' | ''

export interface TemplateObject {
t: TagType
Expand Down Expand Up @@ -55,12 +55,7 @@ export default function parse (str: string, env: EtaConfig): Array<AstObject> {
}
}

var prefixes = (
parseOptions.exec +
parseOptions.interpolate +
parseOptions.special +
parseOptions.raw
)
var prefixes = (parseOptions.exec + parseOptions.interpolate + parseOptions.raw)
.split('')
.join('|')

Expand Down Expand Up @@ -99,8 +94,6 @@ export default function parse (str: string, env: EtaConfig): Array<AstObject> {
currentType = 'r'
} else if (prefix === parseOptions.interpolate) {
currentType = 'i'
} else if (prefix === parseOptions.special) {
currentType = 's'
}

currentObj = { t: currentType, val: content }
Expand Down
8 changes: 4 additions & 4 deletions test/file-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ templates.define('test-template', compile('HEY <%=it.name%>'))
describe('include works', () => {
it('simple parser works with "includeFile"', async () => {
var renderedTemplate = render(
'<% @includeFile("simple", it) %>',
'<%~ E.includeFile("simple", it) %>',
{ name: 'Ben' },
{ filename: path.join(__dirname, 'templates/placeholder.eta') }
)
Expand All @@ -19,7 +19,7 @@ describe('include works', () => {

it('"includeFile" works with "views" array', async () => {
var renderedTemplate = render(
'<% @includeFile("randomtemplate", it) %>',
'<%~ E.includeFile("randomtemplate", it) %>',
{ user: 'Ben' },
{
filename: path.join(__dirname, 'templates/placeholder.eta'),
Expand All @@ -31,15 +31,15 @@ describe('include works', () => {
})

it('simple parser works with "include"', async () => {
var renderedTemplate = render('<% @include("test-template", it) %>', { name: 'Ben' })
var renderedTemplate = render('<%~ E.include("test-template", it) %>', { name: 'Ben' })

expect(renderedTemplate).toEqual('HEY Ben')
})

test('throws if helper "includeFile" cannot find template', () => {
expect(() => {
render(
'<% @includeFile("missing-template", it) %>',
'<%~ E.includeFile("missing-template", it) %>',
{},
{
filename: path.join(__dirname, 'templates/placeholder.eta'),
Expand Down
2 changes: 1 addition & 1 deletion test/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ The Daugherty's have 5 kids:

test('throws if helper "include" cannot find template', () => {
expect(() => {
render('<% @include("missing-template", it) %>', {})
render('<%~ E.include("missing-template", it) %>', {})
}).toThrow(new Error('Could not fetch template "missing-template"'))
})
})
2 changes: 1 addition & 1 deletion test/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('parse test', () => {
{ t: 'e', val: '}' },
{ t: 'e', val: '}' },
'\\nThis is a partial: ',
{ t: 's', val: 'include("mypartial")' }
{ t: 'r', val: 'E.include("mypartial")' }
])
})

Expand Down
2 changes: 1 addition & 1 deletion test/templates/complex.eta
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ Value: <%= it.obj[key] %>, Key: <%= key %>
<% } %>
<% } %>

This is a partial: <% @include("mypartial") %>
This is a partial: <%~ E.include("mypartial") %>

0 comments on commit 48a0c80

Please sign in to comment.