Reputation: 8379
I have a template like below
<div>
<a href="{{href}}" class="{{class}}">{{linkText}}</a>
same duplicate link below
<a href="{{href}}" class="{{class}}">{{linkText}}</a>
</div>
As you can see above <a href="{{href}}" class="{{class}}">{{linkText}}</a>
is being reused two times. Is there any possible solutions to define it once and use it as many times as I need.
Upvotes: 0
Views: 172
Reputation: 11256
You could either use Partials
or Components
to achieve this. Here is an example using Partials
<script id='template' type='text/ractive'>
<div>
{{>link}}
same duplicate link below
{{>link}}
</div>
<!-- {{>link}} -->
<a href="{{href}}" class="{{class}}">{{linkText}}</a>
<!-- {{/link}} -->
</script>
Upvotes: 3
Reputation: 1251
You could leverage mustache iteration.
<div>
{{#links}}
<a href="{{href}}" class="{{class}}">{{linkText}}</a>
{{/links}}
</div>
But you need your data to repeat.
{
"links" : [{
"href" : 1,
"class" : 1,
"linkText" : 1,
},{
"href" : 1,
"class" : 1,
"linkText" : 1,
}]
}
Upvotes: 1