Does Handlebars Have a Equivalent to Sass API Importers for Resolving Partials Imports Dynamically? #2058
Unanswered
JuanRojasC
asked this question in
Q&A
Replies: 1 comment 1 reply
-
I guess you can't use dynamic partials because packageB is not controlled by you and you can't change the "import"-statements? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Description:
I am building an application that renders both Sass and Handlebars files. In the Sass API, there's an
importers
property that allows you to define where the API should look for files to import dynamically. I'm wondering if something similarly simple exists in Handlebars for resolving partials dynamically.After some investigation, I found the
invokePartial
function in the Handlebars repository.handlebars.js/lib/handlebars/runtime.js
Line 174 in 084e8fe
I’ve partially achieved my goal by overriding it. Here’s my implementation:
This approach dynamically resolves partials based on custom paths constructed from variables. It works well when resolving the partials themselves, but fails when the partials make use of known helpers like
#each
or#with
. For instance, if a partial uses{{#each items}}
, I encounter an exception like this:I suspect this issue arises because the helpers (
knownHelpers
) are not correctly passed or recognized in this custom implementation. Despite including thehelpers
object in theoptions
, Handlebars still fails to resolve them.The goal is to enable the use of pseudo-relative paths based on the entry
.hbs
file. For example, considerpackageA
, which containsmain.hbs
andexample.hbs
. The entry point ismain.hbs
, and inside it, the partialexample.hbs
is referenced. The desired outcome is formain.hbs
to declare{{> example }}
and, usinginvokePartial
along with an injected variable, resolveexample
topackageA/example.hbs
.While a straightforward approach is to use
Handlebars.registerPartial('example', ...)
, this is not scalable because the number of partials to register is unknown. Additionally, there could also be apackageB
containing its ownexample.hbs
, which needs to be resolved correctly based on the respective package context.Question:
importers
property?#each
and#with
are properly recognized and functional in dynamically resolved partials?Beta Was this translation helpful? Give feedback.
All reactions