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

Added option to customize the wrapper for precompiled templates #302

Merged
merged 2 commits into from
Apr 27, 2015

Conversation

rhengles
Copy link
Contributor

Users can use this options to store templates in modules, for example.

@jlongster
Copy link
Contributor

That's what asFunction is for. Probably not the best name for it though.

If you are calling this API directly, you can use asFunction and the string returned from precompile represents an expression that is a function called with the context (and optional callback if using async stuff). You can do anything you want with that expression. Like put it in a module. So you would call that API directly and just append code before and after it that you need.

This isn't much different though, and maybe easier to use. This will be backwards-incompatible though. Not sure how much is built off the current API. That's my only major concern with changing it. Might need to add a deprecation noticed for the next version and merge this in 2 versions if we want to do this.

@rhengles
Copy link
Contributor Author

Thanks @jlongster for reviewing this.

I didn't understand this very well: isn't _precompile a private function, and as such is not part of the API? So the BW-incompatible change is the new property "wrapper" on the "opts" object of "function precompile(input, opts)"?

The thing I'm trying to prevent with this is the global variable. I want to store the actual precompiled template, and not a empty shell which just call the global variable. The only way this is possible with the current API is if I replace with strings the beginning and end of the precompiled string, but it is very error-prone.

[edit] Now I see, I can just use the compiler directly. But I'll have to reimplement the file and directory loading routines :/

@jlongster
Copy link
Contributor

Now I see, I can just use the compiler directly. But I'll have to reimplement the file and directory loading routines :/

That's not what I meant, you definitely shouldn't have to do that.

precompile takes an options object that the user can pass asFunction on. It's part the public precompile API.

I don't know what you mean by empty shell. When a file is precompiled you get the whole JS code for it back, and if you pass asFunction you get back all the code as an expression that you can stick anywhere - inside a module for example.

Yes, it will always also put it on nunjucksPrecompiled regardless. That's a bit of a hack, but it's needed for template loading. If you ever use include, import, etc. the system needs a registry of all the templates. So you could use this and put each template in a module, and load those modules separately, but you would also need to load any templates that you include in another template manually, then you could use include.

The story for "code splitting" or lazily loading templates isn't the best. The primary use case is to load everything at once, but this could certainly be improved.

@rhengles
Copy link
Contributor Author

Thanks for the clarifications, and sorry for my stubbornness :o)

When I said empty shell, I meant the function returned by asFunction. It returns a function to render the template, and that's great - nothing wrong with that. It's just not what I'm trying to do.

The code returned by precompile does contain the entire precompiled template - but when executed, the code doesn't return the template. It may return a function to render it if I pass asFunction.

These are sensible defaults, I'm not arguing with that. But if I could get only the compiled template, without the global variable, I would be happy :)

I know nunjucks need all the templates I call with include, import, etc, and I pretend to pass all of them with a custom loader - I commented this on line 35 of src/precompile.js of this PR.

Thanks again! I'm just too excited with this lib because it's the best template language I ever found for JS!

@jlongster
Copy link
Contributor

Ah, I see you have a custom loader. Neat. I'm open to refactoring this since the asFunction opt is kind of weird. I'll think about this a bit more and we can do a backwards incompatible change with a bigger version bump.

@ulion
Copy link

ulion commented Dec 13, 2014

This is very useful for the Node.js env, because in some env the fs module can not be used to load template, the only way is to precompile the templates into js file and use require to load it as module.

@rhengles
Copy link
Contributor Author

Can you give me an idea of what you're considering refactor here? Can this be considered for 2.0 (#347)?

@jlongster
Copy link
Contributor

@rhengles I'm taking a look at this again, and I think we could do this with a minor bump (1.2 -> 1.3). I'd like to review this a little more before accepting it though.

@jlongster
Copy link
Contributor

Ok I'm definitely looking at this again. If we break the precompile API it may be reason enough for 2.0. No need to worry about bumping the major version.

@jlongster
Copy link
Contributor

You've convinced me, I'll merge this in and play with it locally to make sure everything works.

jlongster added a commit that referenced this pull request Apr 27, 2015
Added option to customize the wrapper for precompiled templates
@jlongster jlongster merged commit 33c2dfa into mozilla:master Apr 27, 2015
@jlongster
Copy link
Contributor

Actually, it looks like this is backwards-compatible. I thought you replaced the asFunction option but it's still there. this should be fine.

@rhengles
Copy link
Contributor Author

👍 💯 Thanks man!

@rhengles rhengles deleted the precompile-custom branch April 28, 2015 00:50
@rhengles rhengles mentioned this pull request Dec 14, 2015
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

Successfully merging this pull request may close these issues.

3 participants