Pass viewData to the template, initialize the viewData of the response with the contents of a basket
Hello,
I am doing an exercise where I need to pass data to the response (setViewdata) but when trying to do so, I get an error:
Error: Cannot render template without name or data in controller Basket
What am I doing wrong ?
Thanks for your help.
isml template:
<isloop items="${pdict.Basket.getProductLineItems()}" var="productLineItem" status="loopstate">
${productLineItem.productName}<br/>
Count ${loopstate.count} Index ${loopstate.index} ${productLineItem.product.name}<br/>
</isloop>
controller js:
"use strict";
var server = require("server");
var BasketMgr = require("dw/order/BasketMgr");
server.get("Show", function (req, res, next) {
// Pass viewData to the template
var basket = BasketMgr.getCurrentBasket();
res.setViewData({ Basket: basket });
next();
});
module.exports = server.exports();
You have a endpoint without any rendering, so there is no link to the isml you are mentioning above. You need:
res.render('/your/template', {
Basket: basket
});
And the object you add into the rendering method is the viewData object for the response. So you can skip using "res.setViewData({ Basket: basket })" explicitly. This call typically becomes relevant if you use the append/prepend mechanisms or other situations where you want to add something from another place other than the rendering controller.