1 | initial version |
To present data in a jade template using information from a SQLite database, follow these steps:
Install and configure the sqlite3 module in your Node.js project.
Connect to the SQLite database using the sqlite3 module.
Write a SQL query to retrieve the required data from the database.
Pass the data obtained from the query as an object to the jade template.
In the jade template, use jade syntax to iterate over the data and display it as desired.
For example, let's say we have a table named "students" in our SQLite database with columns named "name", "age", and "gender". We want to display the data of all students in a table on our web page. We can achieve this as follows:
npm install sqlite3
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('path/to/database');
var sql = "SELECT name, age, gender FROM students";
db.all(sql, [], function(err, rows) {
if(err) {
console.log(err);
} else {
res.render('students', {students: rows});
}
});
res.render('students', {students: rows});
table
tr
th Name
th Age
th Gender
each student in students
tr
td= student.name
td= student.age
td= student.gender
The above jade code will generate an HTML table displaying the data of all students retrieved from the SQLite database.