Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialCarlos Sanchez
8,900 PointsHow to send values to templates with {{ variable }} tags?
I am implementing my own version of this project, and part of my own implementation is that I added a database to which i can connect and perform CRUD operations. Right now I am trying to print in one of the templates the data I am retrieving from the DB, however it does not seem to work as the page does not render anything, The data is good and not null since it prints the values when I do the print statement. Therefore I figure that the template is never receiving the data, what am I ding wrong? Its so much easier to do this part in NodeJS, why is the Handlebars iteration in java so much harder? :(
main.java
public class Main {
public static void main(String[] args) {
get("/", (req, res) -> "Hello");
get("/hello", (req, res) -> {
BookDAO bookTransfer = new BookDAO();
List<BookDTO> bookList = bookTransfer.readAll();
bookList.stream().forEach(System.out::println);
return new ModelAndView(bookList, "list.hbs");
}, new HandlebarsTemplateEngine());
}
}
list.hbs
1 Answer
Carlos Sanchez
8,900 PointsOk so I went back and rewatched the video again and came up with this fix:
public class Main {
public static void main(String[] args) {
get("/", (req, res) -> {
BookDAO bookTransfer = new BookDAO();
List<BookDTO> bookList = bookTransfer.readAll();
bookList.stream().forEach(System.out::println);
Map<String, Object> model = new HashMap<>();
model.put("books", bookList);
return new ModelAndView(model, "list.hbs");
}, new HandlebarsTemplateEngine());
}
}
base.hbs
list.hbs