javascript - Recursive mixin in Pug -
we use pug produce style guide, , have mixin generate header , description reusable elements. simplified example this:
mixin sgitem(name) if !isnested - isnested = true h3=name block else block - isnested = false
that means if write this:
+sgitem('foo') include partial sgitem +sgitem('baz') div example here
, nested item should return block , not name. well, works, not if there's second or third nested sgitem:
+sgitem('foo') include partial sgitem include partial sgitem +sgitem('baz') div example here
that generate this:
<h3>foo</h3> <div>nested example</div> <h3>nested title</h3> <div>nested example</div> <h3>baz</h3> <div>example here</div>
, while this:
<h3>foo</h3> <div>nested example</div> <div>nested example</div> <h3>baz</h3> <div>example here</div>
it issue not pug per se, setting variables , on. how can mixin correctly know if nested or not?
i feel you're trying overly complicated. not create following?
mixin sgitem(title, description) if title h3=title if description div=description
then call mixins:
+sgitem('foo', 'nested example') +sgitem('', 'nested example') +sgitem('baz', 'example here')
which render into:
<h3>foo</h3> <div>nested example</div> <div>nested example</div> <h3>baz</h3> <div>example here</div>
Comments
Post a Comment