How do I make a DRY template in dustjs that may or may not use a base template?

77 Views Asked by At

I got this template:

{?doesExtend}
    {>base_template/}
    {<param}
        <div id='abc'>{foo}</div>
        <div id='def'>{bar}</div>
        <div id='ghi'>{bas}</div>
    {/param}
{:else}
    <div id='abc'>{foo}</div>
    <div id='def'>{bar}</div>
    <div id='ghi'>{bas}</div>
{/doesExtend}

This works but it's not DRY...I have to put the HTML in twice. How can I make it so that if doesExtend exists, then it will extend the template to a base template, and if it doesn't exist, then it will simply not extend it and render it as is, in a way that is DRY?

1

There are 1 best solutions below

2
On

I would just move the repeated code into a separate partial.

{! "div_template"  !}
<div id='abc'>{foo}</div>
<div id='def'>{bar}</div>
<div id='ghi'>{bas}</div>

Then your original template would look like:

{?doesExtend}
    {>base_template/}
    {<param}
        {>div_template/}
    {/param}
{:else}
        {>div_template/}
{/doesExtend}