Skip to content

Commit

Permalink
feat(tags): implement yield tag
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Mar 21, 2017
1 parent 016bf48 commit e1445d8
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 1 deletion.
93 changes: 93 additions & 0 deletions src/Tags/YieldTag.js
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
3 changes: 2 additions & 1 deletion src/Tags/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ module.exports = {
includeTag: new (require('./IncludeTag'))(),
componentTag: new (require('./ComponentTag'))(),
slotTag: new (require('./SlotTag'))(),
sectionTag: new (require('./SectionTag'))()
sectionTag: new (require('./SectionTag'))(),
yieldTag: new (require('./YieldTag'))()
}
57 changes: 57 additions & 0 deletions test/unit/tags/yield.spec.js
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)()
`)
})
})

0 comments on commit e1445d8

Please sign in to comment.