-
Notifications
You must be signed in to change notification settings - Fork 45
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
How do I get file path with Handlebars templates? #70
Comments
Hi simpleqcode. Handlebar-Webpack-Plugin is a simple wrapper around Handlebars. This looks like a specific Handlebars issue to me and your solution seems alright to be used in Handlebars. Additionally, correct path resolution depends on your output-structure and can be configured by webpack or other plugins. |
I've solved this by creating a custom {{webRoot}} helper:
Now I can use it like this: |
Nice. Thanks for sharing your solution! |
Perfect! This was something I was looking for. I had to modify it a bit as I have multiple levels like onBeforeSave: function (Handlebars, resultHtml, filename) {
const level = filename.split('//').pop().split('/').length;
function relPath(level) {
var res = ( level > 1 ) ? '..' : '.';
for (var i = 0; i < level; i++) {
if ( i > 0 ) {
res += '/..';
}
}
return res;
}
const finalHtml = resultHtml.split('{{wroot}}').join(relPath(level));
return finalHtml;
}, |
I am using Webpack and Handlebars templates:
index.html
head.html
Works like a charm. Now I've added a folder next to
index.html
:Both file import the same
head.html
. However, the one from the docs folder will have an error in the console because the stylehsheet is not loaded. I could have solved this by passing another variable to it, e.g.:and
and change
head.html
to:However, I hope to think Handlebars has some built-in variable for that. Is there any?
The text was updated successfully, but these errors were encountered: