diff --git a/_posts/2021-07-05-accessing-variables-in-handlebars-rust.md b/_posts/2021-07-05-accessing-variables-in-handlebars-rust.md new file mode 100644 index 0000000..bfca96c --- /dev/null +++ b/_posts/2021-07-05-accessing-variables-in-handlebars-rust.md @@ -0,0 +1,61 @@ +--- +layout: post +title: "Accessing Handlebars variables in an outer scope" +tags: +- rust +--- + +This weekend I learned some unfamiliar behaviors with the way Handlebars +handles nested variable scopes. I typically use Handlebars via the +[handlebars-rust](https://github.com/sunng87/handlebars-rust) implementation +which aims to maintain nearly one to one compatibility with the [JavaScript +implementation](https://handlebarsjs.com/). They have block scope helpers such +as `#each` and `#with`, both of which create an inner scope for variable +resolution. Unfortunately, the syntax can be quite unintuitive for accessing outer +scope once in those nested scopes. + +Handlebars is a largely declarative templating syntax which uses curlybraces +such as `{{var}}` for variable and helper substitution. The `#each` helper is +important for loops, imagine the following data structure: + +```json +{ + "repos" : [ + { + "name" : "otto" + }, + { + "name" : "l4bsd" + } + ], + "mood" : "cool" +} +``` + +This could be rendered into a list on a page via: + +```html +