I'm currently working on a web application using Node.js and Express, and I'm using the Handlebars template engine to render my views. I have a route that displays a list of students with pagination, and I'm encountering a parse error in my Handlebars template.
Here's my Express route:
app.get('/student-list', async (req, res) => {
// Retrieve a list of all students from your data source
let allStudents = await business.getAllStudents();
// Implement pagination logic to group students into pages of 6
const page = parseInt(req.query.page) || 1; // Get the requested page number from the query parameter
const perPage = 6;
const startIndex = (page - 1) * perPage;
const endIndex = startIndex + perPage;
const studentsToDisplay = allStudents.slice(startIndex, endIndex);
// Calculate the total number of pages
const totalPages = Math.ceil(allStudents.length / perPage);
res.render('student_list', {
studentsList: studentsToDisplay,
currentPage: page,
totalPages: totalPages
});
});
And here's my student_list.hbs template:
<body>
<h1>Student List</h1>
{{#each studentsList}}
<div>
<p>{{this.name}}</p>
<!-- Display other student details here -->
</div>
{{/each}}
<!-- Pagination Links -->
<div class="pagination">
{{#if currentPage > 1}}
<a href="/student-list?page={{currentPage-1}}">Previous</a>
{{/if}}
{{#if currentPage < totalPages}}
<a href="/student-list?page={{currentPage+1}}">Next</a>
{{/if}}
</div>
</body>
I'm getting the following error when rendering this template:
Error: Parse error on line 13:
... {{#if currentPage > 1}}
<
-----------------------^
Expecting 'CLOSE_RAW_BLOCK', 'CLOSE', 'CLOSE_UNESCAPED', 'OPEN_SEXPR', 'CLOSE_SEXPR', 'ID', 'EQUALS', 'OPEN_BLOCK_PARAMS', 'STRING', 'NUMBER', 'BOOLEAN',