mozilla / nunjucks

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)
https://mozilla.github.io/nunjucks/
BSD 2-Clause "Simplified" License
8.54k stars 638 forks source link

Error while calling macro in nunjucks #924

Open sudhirbeldar opened 7 years ago

sudhirbeldar commented 7 years ago

I am having /template/_modules/top-category.njk in which I am calling a below macro {{ plpGrocery() }}

This is defined in /template/_modules/product-listing-item-grocery.njk {% macro plpGrocery(outofstock) %} ............ {% endmacro %}

While compiling I am getting error Template render error: (D:\POC_nunjucks\template_modules\top-category-offers.njk) Template render error: (D:\POC_nunjucks\template_modules\product-listing-item-grocery.njk) Error: Unable to call plpGrocery, which is undefined or falsey

Can someone please help?

harikm-dmart commented 7 years ago

Are you importing the macro as follows: {% from "top-category.njk" import plpGrocery %}.

Also you might need the 'outofstock' variable value to be passed to it.

revelt commented 7 years ago

@sudhirbeldar how did it go, did you solved this since?

sudhirbeldar commented 7 years ago

@revelt , Yes I got succeeded in cracking this out like as below.

  1. We need to import the njk file in which macro is written {% import '_modules/product-listing-item-grocery.njk' as groceryOffer %}

  2. Now we are free to call macro with it name using above instance. Here groceryOffer is an instance of njk file and plpGrocery is macro name {{ groceryOffer.plpGrocery(baseURL, wcsProductURL, cr) }}