Skip to content
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

Reuse RenderContext after executing partial #624

Open
gemhtcr opened this issue Dec 19, 2023 · 3 comments
Open

Reuse RenderContext after executing partial #624

gemhtcr opened this issue Dec 19, 2023 · 3 comments

Comments

@gemhtcr
Copy link

gemhtcr commented Dec 19, 2023

For now, because of the following line of code, handlebars doens't share RenderContext when rendering partial.

https://github.com/sunng87/handlebars-rust/blob/9d7d5556287e31e4148841a56e0981b64a679fb6/src/partial.rs#L61C9-L61C39

  // Rust
 let partial = find_partial(rc, r, d, tname)?; 
  
 if let Some(t) = partial { 
     // clone to avoid lifetime issue 
     // FIXME refactor this to avoid 
     let mut local_rc = rc.clone();    // <---  This line can't reuse `RenderContext` after partial
  
     // if tname == PARTIAL_BLOCK 
     let is_partial_block = tname == PARTIAL_BLOCK; 
  
     // add partial block depth there are consecutive partial 

Is it possible to fix it and have a universal RenderContext when rendering ?

@sunng87
Copy link
Owner

sunng87 commented Dec 19, 2023

Yes because partial has its own context data. I'm curious about your use case for reusing the RenderContext.

@gemhtcr
Copy link
Author

gemhtcr commented Dec 20, 2023

Hi @sunng87 , thanks for your update

My case is that I have an assign helper to assign things to some variable. See below

// _partial.hbs
...
{{#assign "foo"}}
bar
{{/assign}}
...
// parents.hbs 
...
{{> _partail}}
...
{{foo}}       <---- expected to show "bar"

After spending time tracing source, it seems to me the closest solution is to add it into RenderContext so that it can be seen by parent context
I'm happy to learn if there is other way to achieve this

@sunng87
Copy link
Owner

sunng87 commented Jan 5, 2024

I think it's not possible also it's not recommended to have data modification in templates, which has poor debug experience. I will suggest you to prepare the data ahead of rendering and feed to the template directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants