-
Notifications
You must be signed in to change notification settings - Fork 132
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
Set global variables using nunjucks syntax #2474
Conversation
@LamJiuFong |
Yeah I agree with @EltonGohJH. |
cb84513
to
69ec838
Compare
Hi @EltonGohJH @yucheng11122017 Thanks for your suggestions! I have updated my code accordingly
My current approach is to comment out every This method (commenting out the variables) comes with one restriction:
But the following doesn't work:
In short, {% set %} and does not work together
I think it is tricky to implement our own parser to collect all the variables because there are many use cases to consider eg. different data types, math operations, applying functions etc. Our own parser might not be able to support more complex situations though |
Testing instructions (Some sample data): Can add these variables to
and add use them anywhere, for example:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the late review @LamJiuFong! Could you add some functional test cases so we have a better idea how this should work?
@@ -406,6 +406,18 @@ <h1 id="path-within-the-boilerplate-folder-is-separately-specified">Path within | |||
</div> | |||
<p><strong>HTML include</strong></p> | |||
<div> | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is there this change in the test_site?
const variablesFileContent = fs.existsSync(this.variablesFilePath) | ||
? fs.readFileSync(this.variablesFilePath, 'utf8') : ''; | ||
|
||
return variablesFileContent.replace(VARIABLE_REGEX, NUNJUCKS_COMMENT_SYNTAX); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is there a need to do this? Isn;t the variable way of declaring already working?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LamJiuFong correct me if I am wrong.
I thinkis trying to comment out the variables so that it will not be rendered as stated earlier. As what he is trying to do is to put nunjunks variables on top of the template then render it. To load the nunjucks variables.
If variables is not commented, then it will be rendered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@EltonGohJH You are right, it is to prevent the variables from being rendered
I personally think that it is okay for {% set %} does not work with . But I feel that this solution is a bit too hacky for my liking. Moreover, the testcases changed? @LamJiuFong Do you know why did the testcase changed as pointed out by @yucheng11122017 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took a look at the PR and wanted to share a few thoughts on prepending the variables.md
content to all files before rendering with Nunjucks.
While it might work, there are a few potential downsides to keep in mind:
- It could impact performance, especially for larger files or projects with many files.
- If a file already has Nunjucks syntax or comments, I am not sure if prepending the content will lead to conflicts.
Instead of modifying individual files, I also support configuring the Nunjucks environment to load variables.md
during initialization. That way, the variables will be available to all templates without needing to modify each file.
Another option could be using Nunjucks' addGlobal
method or creating a separate config file for VariableRenderer
-specific variables.
Overall, while the proposed solution might work, it goes against the principle of separating concerns. It's generally better to keep variable declarations centralized and configure the rendering environment appropriately for cleaner code organization and maintainability.
Let me know if you have any other questions or thoughts on this!
d43bad9
to
bc86322
Compare
I agree with this, I think this is part of the tradeoff. I think we can definitely improve the current proposed solution
I couldn't think of any case where conflicts may happen, do you have any ideas?
I am not sure how to achieve this, do you have any idea in mind?
I think the issue with using
Yup, I agree on this, will work on it! |
@EltonGohJH @yucheng11122017
However, the newlines does not affect the rendering of the HTML files
We should not allow this as it affects the rendering output of the HTML files To add on, there are two ways to render the content -
|
As I could not find a way to solve this issue, I am thinking of closing this PR and summarise all the findings/alternatives that I have considered for future developers' reference, what do y'all think? |
@LamJiuFong |
What is the purpose of this pull request?
Fixes #2302
Overview of changes:
Prepend the content of
variables.md
to all files before calling nunjucks to render the filesAnything you'd like to highlight/discuss:
Nunjucks does not expose its parser as part of its public API. We can still access its internal
nunjucks.parse
function but it only allows us to parse a Nunjucks template string into an abstract syntax tree (AST), which represents the structure of the template. Hence we have to implement our own parser to parse various syntax of the different use cases of {% set %}.Another approach to this issue is to prepend the content of the
variables.md
to all the files.Compared to the parser method above, this method accommodates all use cases of {% set %}. This is because we are using nunjucks' parser while rendering the files.
We can also import files using {% ext file = "filepath" %} and use the file globally
Some restrictions have been commented below
Testing instructions:
Commented below
Proposed commit message: (wrap lines at 72 characters)
Support nunjucks syntax for global variables
Checklist: ☑️
Reviewer checklist:
Indicate the SEMVER impact of the PR:
At the end of the review, please label the PR with the appropriate label:
r.Major
,r.Minor
,r.Patch
.Breaking change release note preparation (if applicable):