Created
November 22, 2017 16:29
-
-
Save morrelinko/ec81c66667de6d0b27a2150160a669d9 to your computer and use it in GitHub Desktop.
Nunjucks 'yield' Extension (One liner block expressions)
This file contains hidden or 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
'use strict' | |
class YieldExtension { | |
get tags () { | |
return ['yield'] | |
} | |
parse (parser, nodes, lexer) { | |
let tag = parser.nextToken() | |
parser.skipSymbol(tag.value) | |
let node = new nodes.Block(tag.lineno, tag.colno) | |
node.name = parser.parsePrimary() | |
node.body = new nodes.NodeList(0, 0, [new nodes.Output(0, 0, [parser.parsePrimary()])]) | |
parser.advanceAfterBlockEnd(tag.value) | |
return node | |
} | |
} | |
module.exports = YieldExtension |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use
Before (using blocks)
<!-- Child template --> {% block title %}Home Page{% endblock %}
Now (with Yield)
<!-- Child template --> {% yield title 'Home Page' %}