Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use of "globals" in option not clear #3198

Closed
jacobbogers opened this issue Nov 17, 2019 · 1 comment
Closed

use of "globals" in option not clear #3198

jacobbogers opened this issue Nov 17, 2019 · 1 comment

Comments

@jacobbogers
Copy link

there is this in the current doc
image

but no examples whatso ever, i was thinking it was globals from the window object ,

I attempted this amongst others

const fn = pug.compileFile('./template.pug',{  self:true, debug: false, compileDebug:true, globals:['location']});

sample template

doctype html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN"
-
  var list = ["Uno", "Dos", "Tres",
          "Cuatro", "Cinco", "Seis"]
each item in list
  li= item
img(src="/this-image.jpg") 
p written with love from #{self.author} #{self.copyright} #{location.href}

but that didnt work

1.Can you explain how to use globals and what it does?
2.Can you update the documentation with an example?

Thank you

@jacobbogers
Copy link
Author

I found resolution for this code

Below is a demo that works (node, not browser)

the global is the node global if you run code in the windows client, make sure the window object is used as global

const pug = require('pug');
const p5 = require('parse5');
const stringify = require('json-stringify-safe');

global.hello='world';

const fn = pug.compileFile('./template.pug',{ doctype:'xml', self:true, debug: false, compileDebug:true, globals:['global']});
//console.log(fn);

console.log(typeof fn);

if (typeof fn === 'function'){
   // console.log(fn.toString());
    console.log(fn({ author: 'my name', copyright: '© 2019 Jacob Bogers'}));
    const ast = p5.parse(fn({x:'y'}));
    console.log(p5.serialize(ast));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant