P粉3099896732023-09-03 15:46:09
I don't understand why you stopped using Bootstrap when you tried to implement the 3columnLayout
flag. Using Bootstrap, we will create a 3 column layout using class col-lg-4
on each column. Therefore, the conditional expression we need for each column is: {{#if ../3columnLayout}}col-lg-4{{else}}col-lg-3{{/if}}
.
const template = Handlebars.compile(document.getElementById('Template').innerHTML); const data = { "3columnLayout": true, "cards": [{ "title": "Card 1", }, { "title": "Card 2", }, { "title": "Card 3", }, { "title": "Card 4", }, { "title": "Card 5", }, { "title": "Card 6", }, { "title": "Card 7", }, { "title": "Card 8", } ] } const output = template(data); document.getElementById('Output').innerHTML = output;
.image-card { height: 432px; background-color: lightblue; border: 1px solid; margin-bottom: 16px; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.7.7/handlebars.min.js"></script> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"/> <script id="Template" type="text/template"> <div class="container"> <div class="row"> {{#each this.cards}} <div class="col-md-6 {{#if ../3columnLayout}}col-lg-4{{else}}col-lg-3{{/if}}"> <div class="image-card"> <div class="image-content"> <p class="title">{{title}}</p> </div> </div> </div> {{/each}} </div> </div> </script> <div id="Output"></div>