Read Spring ModelAndView objects in Pug

266 views Asked by At

My front-end application uses Pug to generate the index page.

Being a Vue.js application, it can be served from Java (production purpose) or Node (develop purpose)

DEVELOP

Variables in index.pug are injected by running this command:

pug -P -s -O ./variables.js -o ./ index.pug

variables.js is something like:

variables = {
  value: 'something'
}

Variables inside index.pug can be readed like:

if variables.value == 'something'
  //show some tags ecc...

All is working fine.

PRODUCTION

To render index.pug server side I use jade4j

In index controller I do something like:

@RequestMapping(value = "/index.htm")
public ModelAndView indexView(...) {
   ... other code here...
   ModelAndView mav = new ModelAndView("index");
   mav.addObject("value", "something");
   return mav;
}

but value can be readed in a script. tag by this syntax '#{value}' like in this example:

script.
   console.log('#{value}') // prints "something"

How can I read value in the DEVELOP scenario above?

1

There are 1 answers

0
Andrea Grimandi On BEST ANSWER

This has given me the answer.

It was pretty simple after all... something I've never tried cause thought it was too dummy.

Just use #{value} without '' inside the "Pug code", with it otherwise.