-
-
Notifications
You must be signed in to change notification settings - Fork 139
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
Local variable API refinement #371
Comments
I just noticed this and a couple of times would have liked to set custom local variables using the If this is already possible would love to know how to do it, thanks! |
This is possible. The local variable is associated with current block (for example, Check the source of |
Ah, thanks! My mistake was not creating a block for my helper iterator. If i create the block i can use |
Yes. I suggest you to check the implementation of The handlebars book #368 was in my backlog. I will cover more detail of helper development in the doc. |
Thanks so much for the advice @sunng87, after some study I found |
I have tried understanding and reproducing in part the implementations for both |
@cryarchy Do you have a link for your current implementation? |
I have tried the use handlebars::{
BlockContext, Context, Handlebars, Helper, HelperResult, Output, RenderContext, RenderError,
Renderable,
};
pub fn handler<'reg, 'rc>(
h: &Helper<'reg, 'rc>,
handle: &'reg Handlebars,
ctx: &'rc Context,
render_ctx: &mut RenderContext<'reg, 'rc>,
out: &mut dyn Output,
) -> HelperResult {
let lhs = h
.param(0)
.map(|v| v.value())
.ok_or(RenderError::new("expected a left hand side operand"))?
.as_str()
.ok_or(RenderError::new(
"expected the left hand side operand to be a string",
))?;
let rhs = h
.hash()
.get("map")
.map(|v| v.value())
.ok_or(RenderError::new("expected a 'map' hash argument"))?;
if let Some(val) = rhs.get(lhs) {
if let Some(template) = h.template() {
let mut new_block = BlockContext::new();
new_block.set_base_value(val.to_owned());
render_ctx.push_block(new_block);
template.render(handle, ctx, render_ctx, out)?;
render_ctx.pop_block();
}
}
Ok(())
} Sample usage: {{#populate str_key map=exampleMap}}
{{exampleMapProperty}}
{{@../parentVariable}}
{{/map}} where: exampleMap = |
@sunng87 I was investigating this earlier today and I realized that it now works. I think it is possible that the latest version fixed this. If that is not the case... dunno! Anyways, thank you. For this great project and for the quick and helpful responses. |
@
prefix&str
where possibleThe text was updated successfully, but these errors were encountered: