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

Expose the require definition to pug renderer #71

Closed
catc opened this issue May 28, 2018 · 9 comments
Closed

Expose the require definition to pug renderer #71

catc opened this issue May 28, 2018 · 9 comments

Comments

@catc
Copy link

catc commented May 28, 2018

Is it possible to import javascript (or json) files into .pug documents to use as content?

Something like:

// content.js
module.exports = 'some content'

// doc.pug
- var someVal = require('./content.js')
h1 hello world #{someVal}

I believe that pug supports it (pugjs/pug#2604 (comment)) if you define require when passing in variables to pug.

@Zulko
Copy link
Member

Zulko commented May 28, 2018

I'd be ok for it (others, any opinion ?). Exposing require should make it easy for users to extend the framework, but when you multiply the "require" it will make rendering slower (since it requires at every run).

Out of curiosity what is your use-case ?

I think I will also make yaml import easy. I like yaml.

@catc
Copy link
Author

catc commented May 28, 2018

My current use case is:

  • i run a script, generate some data - let's say it's json
  • my doc.pug imports that data, consumes it, builds the pdf
  • i run the script again to generate other data, build the pdf with new data
  • rinse and repeat

The main benefit would be that you wouldn't have to store your data structures in the pug file, and pug would only contain rendering logic.

@Zulko
Copy link
Member

Zulko commented May 29, 2018

This much is clear, the question was more what kind of data, how does it get rendered (table ? graph ?) what kind of document, is it for "interactive" use or use on a server, etc. I'm trying to get ideas of how it could get use, it can influence directions of the software.

@catc
Copy link
Author

catc commented May 29, 2018

It doesn't have to be rendered in any specific way. The whole point is that users could keep their external data in a separate file, import the raw js/json file and render it. This would help keep your pug file clean of any data/variables.

Another huge benefit of adding exposing the require definition to the pug renderer is that you'd have access to other npm modules in the renderer. You could do something like:

- var escape = require('lodash/escape')

p An escaped ampersand looks like: #{escape('&')}

@catc catc changed the title Import js/json files Expose the require definition to pug renderer May 29, 2018
@Zulko
Copy link
Member

Zulko commented May 29, 2018

Done. See this test example. Note that ReLaXed also exposes path and basedir (the directory where the master pug document is). In the example these two constants are used to resolve the path of the local data.json.

@catc
Copy link
Author

catc commented May 30, 2018

Looks good! Can you get a new build/version out on npm?

@Zulko
Copy link
Member

Zulko commented May 30, 2018

possibly this evening

@catc
Copy link
Author

catc commented May 30, 2018

Thanks!

@catc catc closed this as completed May 30, 2018
@Zulko
Copy link
Member

Zulko commented May 30, 2018

now in version 0.1.6 on npm

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

2 participants