-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eed0249
commit a8cd83b
Showing
13 changed files
with
168 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
let out = ""; | ||
let $lineNumber = 1; | ||
let $filename = "{{__dirname}}index.edge"; | ||
try { | ||
if (state.username === 'virk') { | ||
out += template.renderInline(state.partial)(template,state,ctx); | ||
} | ||
} catch (error) { | ||
ctx.reThrow(error, $filename, $lineNumber); | ||
} | ||
return out; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@includeIf(username === 'virk', partial) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"partial": "include-if-identifier/partial", | ||
"username": "virk" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello world |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello world |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
let out = ""; | ||
let $lineNumber = 1; | ||
let $filename = "{{__dirname}}index.edge"; | ||
try { | ||
if (state.username === 'virk') { | ||
out += template.renderInline("include-if-literal/partial")(template,state,ctx); | ||
} | ||
} catch (error) { | ||
ctx.reThrow(error, $filename, $lineNumber); | ||
} | ||
return out; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@includeIf(username === 'virk', "include-if-literal/partial") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello world |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* edge | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import { EdgeError } from 'edge-error' | ||
import { expressions } from 'edge-parser' | ||
|
||
import { TagContract } from '../Contracts' | ||
import { ALLOWED_EXPRESSION, getRenderExpression } from './Include' | ||
import { unallowedExpression, isSubsetOf, parseJsArg, isNotSubsetOf } from '../utils' | ||
|
||
/** | ||
* Include tag is used to include partials in the same scope of the parent | ||
* template. | ||
* | ||
* ```edge | ||
* @include('partials.header') | ||
* ``` | ||
*/ | ||
export const includeIfTag: TagContract = { | ||
block: false, | ||
seekable: true, | ||
tagName: 'includeIf', | ||
|
||
/** | ||
* Compiles else block node to Javascript else statement | ||
*/ | ||
compile (parser, buffer, token) { | ||
const parsed = parseJsArg(parser, token) | ||
|
||
/** | ||
* The include if only accepts the sequence expression | ||
*/ | ||
isSubsetOf( | ||
parsed, | ||
[expressions.SequenceExpression], | ||
() => { | ||
unallowedExpression( | ||
`"${token.properties.jsArg}" is not a valid argument type for the @includeIf tag`, | ||
parsed, | ||
token.filename, | ||
) | ||
}, | ||
) | ||
|
||
/** | ||
* Disallow more than or less than 2 values for the sequence expression | ||
*/ | ||
if (parsed.expressions.length !== 2) { | ||
throw new EdgeError('@includeIf expects a total of 2 arguments', 'E_ARGUMENTS_MIS_MATCH', { | ||
line: parsed.loc.start.line, | ||
col: parsed.loc.start.column, | ||
filename: token.filename, | ||
}) | ||
} | ||
|
||
const [ conditional, include ] = parsed.expressions | ||
|
||
isNotSubsetOf( | ||
conditional, | ||
[expressions.SequenceExpression], | ||
() => { | ||
unallowedExpression( | ||
`"${conditional.type}" is not a valid 1st argument type for the @includeIf tag`, | ||
conditional, | ||
token.filename, | ||
) | ||
}, | ||
) | ||
|
||
isSubsetOf( | ||
include, | ||
ALLOWED_EXPRESSION, | ||
() => { | ||
unallowedExpression( | ||
`"${include.type}" is not a valid 2nd argument type for the @includeIf tag`, | ||
include, | ||
token.filename, | ||
) | ||
}, | ||
) | ||
|
||
buffer.writeStatement(`if (${parser.utils.stringify(conditional)}) {`, token.filename, token.loc.start.line) | ||
buffer.outputExpression(getRenderExpression(parser, include), token.filename, token.loc.start.line, false) | ||
buffer.writeStatement('}', token.filename, -1) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters