Skip to content

Implement block-scoped inline partials #708

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde_json::value::Value as Json;

use crate::error::RenderError;
use crate::local_vars::LocalVars;
use crate::Template;

#[derive(Clone, Debug)]
pub enum BlockParamHolder {
Expand Down Expand Up @@ -64,6 +65,8 @@ pub struct BlockContext<'rc> {
base_value: Option<Json>,
/// current block context variables
block_params: BlockParams<'rc>,
/// the partials available in this block
block_partials: BTreeMap<String, &'rc Template>,
/// local variables in current context
local_variables: LocalVars,
}
Expand Down Expand Up @@ -110,6 +113,14 @@ impl<'rc> BlockContext<'rc> {
self.base_value = Some(value);
}

pub fn get_local_partial(&self, name: &str) -> Option<&'rc Template> {
self.block_partials.get(name).map(|v| &**v)
}

pub fn set_local_partial(&mut self, name: String, template: &'rc Template) {
self.block_partials.insert(name, template);
}

/// Get a block parameter from this block.
/// Block parameters needed to be supported by the block helper.
/// The typical syntax for block parameter is:
Expand Down
7 changes: 7 additions & 0 deletions src/decorators/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ impl DecoratorDef for InlineDecorator {
.template()
.ok_or(RenderErrorReason::BlockContentRequired)?;

if let Some(block) = rc.block_mut() {
block.set_local_partial(name, template);

return Ok(());
}

rc.set_partial(name, template);

Ok(())
}
}
Expand Down
22 changes: 14 additions & 8 deletions src/partial.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{HashMap, VecDeque};
use std::collections::HashMap;

use serde_json::Value as Json;

Expand Down Expand Up @@ -45,11 +45,6 @@ pub fn expand_partial<'reg: 'rc, 'rc>(
rc: &mut RenderContext<'reg, 'rc>,
out: &mut dyn Output,
) -> Result<(), RenderError> {
// try eval inline partials first
if let Some(t) = d.template() {
t.eval(r, ctx, rc)?;
}

let tname = d.name();

let current_template_before = rc.get_current_template_name();
Expand Down Expand Up @@ -85,6 +80,12 @@ pub fn expand_partial<'reg: 'rc, 'rc>(
.collect::<HashMap<&str, &Json>>();

let mut partial_include_block = BlockContext::new();

for (name, value) in &hash_ctx {
partial_include_block
.set_block_param(name, crate::BlockParamHolder::Value((*value).clone()));
}

// evaluate context for partial
let merged_context = if let Some(p) = d.param(0) {
if let Some(relative_path) = p.relative_path() {
Expand All @@ -98,12 +99,17 @@ pub fn expand_partial<'reg: 'rc, 'rc>(
// use current path
merge_json(rc.evaluate2(ctx, &Path::current())?.as_json(), &hash_ctx)
};

partial_include_block.set_base_value(merged_context);

// replace and hold blocks from current render context
let current_blocks = rc.replace_blocks(VecDeque::with_capacity(1));
rc.push_block(partial_include_block);

// try eval inline partials
if let Some(t) = d.template() {
t.eval(r, ctx, rc)?;
}

// @partial-block
if let Some(pb) = d.template() {
rc.push_partial_block(pb);
Expand All @@ -121,7 +127,7 @@ pub fn expand_partial<'reg: 'rc, 'rc>(
rc.pop_partial_block();
}

let _ = rc.replace_blocks(current_blocks);
rc.pop_block();
rc.set_trailing_newline(trailing_newline);
rc.set_current_template_name(current_template_before);
rc.set_indent_string(indent_before);
Expand Down
7 changes: 7 additions & 0 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ impl<'reg: 'rc, 'rc> RenderContext<'reg, 'rc> {
.get(self.partial_block_depth as usize)
.copied();
}

for block in &self.blocks {
if let Some(partial) = block.get_local_partial(name) {
return Some(partial);
}
}

self.partials.get(name).copied()
}

Expand Down
25 changes: 25 additions & 0 deletions tests/scoped_inline.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use handlebars::Handlebars;

#[test]
fn test_inline_scope() {
let mut hbs = Handlebars::new();
hbs.register_partial(
"test_partial",
r#"{{#>nested_partial}}Inner Content{{/nested_partial}}"#,
)
.unwrap();
let output = hbs
.render_template(
r#"{{>test_partial}}

{{#>test_partial}}
{{#*inline "nested_partial"}}Overwrite{{/inline}}
{{/test_partial}}

{{>test_partial}}"#,
&(),
)
.unwrap();

assert_eq!("Inner Content\nOverwrite\nInner Content", output);
}