Recent searches
No recent searches
How to pass variables from handlebar file to javascript file?
Answered
Posted Nov 21, 2023
Hello Team,
I want to access variables like sections, categories etc in script.js file. How can we achieve this?
2
6
Recent searches
No recent searches
Posted Nov 21, 2023
Hello Team,
I want to access variables like sections, categories etc in script.js file. How can we achieve this?
2
6 comments
Nicolas P.
We have built-in helper handlebars.
See more in our doc: https://developer.zendesk.com/api-reference/help_center/help-center-templates/helpers/
Now, if I look at the handlebars documentation (https://handlebarsjs.com/guide/#what-is-handlebars), I can see that you can import handlebar.js and use it in your theme.
You will have to customize your theme.
More info about theme customization in this article: https://support.zendesk.com/hc/en-us/articles/4408839332250
I hope it helps.
2
Zain Khan
To pass variables from a Handlebars file to a JavaScript file (like script.js), you can follow these steps:
1. Declare the Variables in the Handlebars Template: In your Handlebars template file (let's say it's template. has), declare the variables you want to pass to script.js. For example:
Here, sections and categories are assumed to be JavaScript objects or arrays.
2. Render the Template with Data: When rendering your Handlebars template, pass the necessary data:
3. Access the Variables in script.js: Now, in your script.js file, you can access these variables:
By following these steps, you'll be able to pass variables from a Handlebars template to a JavaScript file. Remember, it's important to ensure that the variables are properly formatted and passed as needed, especially if they contain complex data structures like arrays or objects.
Here are the best Javascript online learning platforms:
1. W3School 2. Iqra Technology 3. JavatPoint
1
Mario Hondermann
Hello, the answer is very interesting, I am just starting out in the world of programming. This way of passing variables with handlebar can be used to pass data from mongodb to chratjs. thank you for your time.
0
Mario Hondermann
sorry this is my question: This way of passing variables with handlebar can be used to pass data from mongodb to chratjs.??
0
Erica Girges
Also, without seeing what your data looks its hard to confirm but, if there is any nested objects to be sure you're accessing them appropriately before attempting to render the handlebars template with Chart.js.
1
Mario Hondermann
Thank you very much for your response, I am going to try it following your recommendations.
1