diff --git a/fixtures/yield-fallback/compiled.js b/fixtures/yield-fallback/compiled.js new file mode 100644 index 0000000..27bc42f --- /dev/null +++ b/fixtures/yield-fallback/compiled.js @@ -0,0 +1,10 @@ +(function (template, ctx) { + let out = '' + if(ctx.resolve('username')) { + out += ctx.resolve('username') + } else { + out += ' Hello guest' + out += '\n' + } + return out +})(template, ctx) \ No newline at end of file diff --git a/fixtures/yield-fallback/index.edge b/fixtures/yield-fallback/index.edge new file mode 100644 index 0000000..26f0788 --- /dev/null +++ b/fixtures/yield-fallback/index.edge @@ -0,0 +1,3 @@ +@yield(username) + Hello guest +@endyield \ No newline at end of file diff --git a/fixtures/yield-fallback/index.json b/fixtures/yield-fallback/index.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/fixtures/yield-fallback/index.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/fixtures/yield-fallback/index.txt b/fixtures/yield-fallback/index.txt new file mode 100644 index 0000000..2021617 --- /dev/null +++ b/fixtures/yield-fallback/index.txt @@ -0,0 +1 @@ +Hello guest \ No newline at end of file diff --git a/fixtures/yield-html/compiled.js b/fixtures/yield-html/compiled.js new file mode 100644 index 0000000..23a5321 --- /dev/null +++ b/fixtures/yield-html/compiled.js @@ -0,0 +1,8 @@ +(function (template, ctx) { + let out = '' + if(ctx.resolve('greeting')) { + out += ctx.resolve('greeting') + } else { + } + return out +})(template, ctx) \ No newline at end of file diff --git a/fixtures/yield-html/index.edge b/fixtures/yield-html/index.edge new file mode 100644 index 0000000..741717e --- /dev/null +++ b/fixtures/yield-html/index.edge @@ -0,0 +1 @@ +@!yield(greeting) \ No newline at end of file diff --git a/fixtures/yield-html/index.json b/fixtures/yield-html/index.json new file mode 100644 index 0000000..7a6a639 --- /dev/null +++ b/fixtures/yield-html/index.json @@ -0,0 +1,3 @@ +{ + "greeting": "Hello virk" +} \ No newline at end of file diff --git a/fixtures/yield-html/index.txt b/fixtures/yield-html/index.txt new file mode 100644 index 0000000..a1ab2a4 --- /dev/null +++ b/fixtures/yield-html/index.txt @@ -0,0 +1 @@ +Hello virk \ No newline at end of file diff --git a/fixtures/yield-tag/compiled.js b/fixtures/yield-tag/compiled.js new file mode 100644 index 0000000..1a7a28c --- /dev/null +++ b/fixtures/yield-tag/compiled.js @@ -0,0 +1,8 @@ +(function (template, ctx) { + let out = '' + if(ctx.resolve('username')) { + out += ctx.resolve('username') + } else { + } + return out +})(template, ctx) \ No newline at end of file diff --git a/fixtures/yield-tag/index.edge b/fixtures/yield-tag/index.edge new file mode 100644 index 0000000..d5db150 --- /dev/null +++ b/fixtures/yield-tag/index.edge @@ -0,0 +1 @@ +@!yield(username) \ No newline at end of file diff --git a/fixtures/yield-tag/index.json b/fixtures/yield-tag/index.json new file mode 100644 index 0000000..d5f5540 --- /dev/null +++ b/fixtures/yield-tag/index.json @@ -0,0 +1,3 @@ +{ + "username": "virk" +} \ No newline at end of file diff --git a/fixtures/yield-tag/index.txt b/fixtures/yield-tag/index.txt new file mode 100644 index 0000000..b7acc9a --- /dev/null +++ b/fixtures/yield-tag/index.txt @@ -0,0 +1 @@ +virk \ No newline at end of file diff --git a/src/Tags/Yield.ts b/src/Tags/Yield.ts new file mode 100644 index 0000000..8597625 --- /dev/null +++ b/src/Tags/Yield.ts @@ -0,0 +1,58 @@ +/* +* edge +* +* (c) Harminder Virk +* +* For the full copyright and license information, please view the LICENSE +* file that was distributed with this source code. +*/ + +import { Parser } from 'edge-parser' +import { EdgeBuffer } from 'edge-parser/build/src/EdgeBuffer' +import { IBlockNode } from 'edge-lexer/build/src/Contracts' +import { allowExpressions } from '../utils' + +export class YieldTag { + public static block = true + public static seekable = true + public static selfclosed = true + public static tagName = 'if' + + /** + * Expressions which are not allowed by the sequence + * expression + * + * @type {Array} + */ + private static allowedExpressions = ['Identifier', 'Literal', 'MemberExpression', 'CallExpression'] + + /** + * Compiles the if block node to a Javascript if statement + */ + public static compile (parser: Parser, buffer: EdgeBuffer, token: IBlockNode) { + const parsed = parser.parseJsArg(token.properties.jsArg, token.lineno) + allowExpressions('yield', parsed, this.allowedExpressions) + + /** + * Start if block + */ + const parsedString = parser.statementToString(parsed) + + /** + * If main content is truthy + */ + buffer.writeStatement(`if(${parsedString}) {`) + buffer.indent() + buffer.writeLine(parsedString) + buffer.dedent() + buffer.writeStatement('} else {') + + /** + * Else write fallback + */ + buffer.indent() + token.children.forEach((child, index) => (parser.processToken(child, buffer))) + buffer.dedent() + buffer.writeStatement('}') + } +} diff --git a/src/Tags/index.ts b/src/Tags/index.ts index 24391d3..e900f25 100644 --- a/src/Tags/index.ts +++ b/src/Tags/index.ts @@ -17,3 +17,4 @@ export { SlotTag as slot } from './Slot' export { DebuggerTag as debugger } from './Debugger' export { SetTag as set } from './Set' export { UnlessTag as unless } from './Unless' +export { YieldTag as yield } from './Yield'