-
-
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
016bf48
commit e1445d8
Showing
3 changed files
with
152 additions
and
1 deletion.
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,93 @@ | ||
'use strict' | ||
|
||
/* | ||
* 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. | ||
*/ | ||
|
||
const BaseTag = require('./BaseTag') | ||
|
||
class YieldTag extends BaseTag { | ||
/** | ||
* Tag name to be used for registering | ||
* the tag | ||
* | ||
* @method tagName | ||
* | ||
* @return {String} | ||
*/ | ||
get tagName () { | ||
return 'yield' | ||
} | ||
|
||
/** | ||
* Whether or not the tag is block level | ||
* tag. Which is yes in this case. | ||
* | ||
* @method isBlock | ||
* | ||
* @return {Boolean} | ||
*/ | ||
get isBlock () { | ||
return true | ||
} | ||
|
||
/** | ||
* Compiles the yield body | ||
* | ||
* @method compile | ||
* | ||
* @param {Object} parser | ||
* @param {Object} lexer | ||
* @param {Object} buffer | ||
* @param {String} options.body | ||
* @param {Array} options.childs | ||
* @param {Number} options.lineno | ||
* | ||
* @return {void} | ||
*/ | ||
compile (parser, lexer, buffer, { body, childs, lineno }) { | ||
const parsedStatement = lexer.parseRaw(body).toStatement() | ||
|
||
/** | ||
* If there is no fallback content, just write it | ||
*/ | ||
if (!childs.length) { | ||
buffer.writeToOutput(`$\{${parsedStatement}}`) | ||
return | ||
} | ||
|
||
/** | ||
* Otherwise wrap the value inside an if clause and | ||
* output the fallback content when actual value | ||
* does not exists. | ||
*/ | ||
buffer.writeLine(`if (${parsedStatement}) {`) | ||
buffer.indent() | ||
buffer.writeToOutput(`$\{${parsedStatement}}`) | ||
buffer.dedent() | ||
|
||
/** | ||
* Else statement for fallback content | ||
*/ | ||
buffer.writeLine('} else {') | ||
buffer.indent() | ||
childs.forEach((child) => parser.parseLine(child)) | ||
buffer.dedent() | ||
buffer.writeLine('}') | ||
} | ||
|
||
/** | ||
* Does not need anything special at runtime | ||
* | ||
* @method run | ||
*/ | ||
run () { | ||
} | ||
} | ||
|
||
module.exports = YieldTag |
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,57 @@ | ||
'use strict' | ||
|
||
/* | ||
* 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. | ||
*/ | ||
|
||
const test = require('japa') | ||
const Template = require('../../../src/Template') | ||
const dedent = require('dedent-js') | ||
|
||
test.group('Tags | Yield ', (group) => { | ||
group.before(() => { | ||
require('../../../test-helpers/transform-tags')(this, require('../../../src/Tags')) | ||
}) | ||
|
||
test('compile simple yield statement', (assert) => { | ||
const statement = dedent` | ||
@!yield(username) | ||
` | ||
const template = new Template(this.tags) | ||
const output = template.compileString(statement) | ||
assert.equal(output, dedent` | ||
return (function templateFn () { | ||
let out = new String() | ||
out += \`\${this.context.resolve('username')}\\n\` | ||
return out | ||
}).bind(this)() | ||
`) | ||
}) | ||
|
||
test('compile simple yield statement with fallback content', (assert) => { | ||
const statement = dedent` | ||
@yield(username) | ||
<h2> Hello anonymous </h2> | ||
@endyield | ||
` | ||
const template = new Template(this.tags) | ||
const output = template.compileString(statement) | ||
|
||
assert.equal(output, dedent` | ||
return (function templateFn () { | ||
let out = new String() | ||
if (this.context.resolve('username')) { | ||
out += \`\${this.context.resolve('username')}\\n\` | ||
} else { | ||
out += \` <h2> Hello anonymous </h2>\\n\` | ||
} | ||
return out | ||
}).bind(this)() | ||
`) | ||
}) | ||
}) |