Replies: 1 comment
-
Hey, this is not a builtin functionality in Handlebars as far as I know, but to work around that you could create a custom helper which renders the partial and pass the result of that helper to your helper, like so: // Register this as `.register_helper("eval", Box::new(EvaluatePartialHelper))`
struct EvaluatePartialHelper;
impl handlebars::HelperDef for EvaluatePartialHelper {
fn call_inner<'reg: 'rc, 'rc>(
&self,
h: &handlebars::Helper<'rc>,
reg: &'reg Handlebars<'reg>,
ctx: &'rc handlebars::Context,
rc: &mut handlebars::RenderContext<'reg, 'rc>,
) -> Result<handlebars::ScopedJson<'rc>, handlebars::RenderError> {
let Some(name) = h.param(0) else {
return Err(handlebars::RenderErrorReason::MissingVariable(
"partial name".to_string().into(),
)
.into());
};
let partial_name = name
.value()
.as_str()
.ok_or(RenderErrorReason::InvalidParamType("String"))?;
let mut str_out = StringOutput::new();
rc.get_partial(partial_name)
.ok_or_else(|| RenderErrorReason::PartialNotFound(partial_name.to_string()))?
.render(reg, ctx, rc, &mut str_out)?;
Ok(handlebars::ScopedJson::Derived(Value::String(
str_out.into_string()?,
)))
}
} Everything that happens in the partial will still affect and use the main template context as it would when it is rendered in a normal |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
How can I pass the partial defined in the template as a function argument to a helper function?
For example say I have a helper function "greeter". I want to do something like this.
Beta Was this translation helpful? Give feedback.
All reactions