ViewEngine: referencing variables in layout does not appear to work
See original GitHub issueI have a project that uses two files on disk to simulate an email templating engine:
Layout.hbs- a layout file containing common markupFileReady.hbs- a sample file that references the layout, and contains specific messaging to let a user know that their file is ready to download.
The generated HTML for FileReady.hbs looks like it properly uses the model, but it doesn’t appear to work in Layout.hbs.
This is the C# code I’m using to render the HTML:
// Instantiate handlebars with custom configuration.
var handlebars = Handlebars.Create(new HandlebarsConfiguration
{
FileSystem = new DiskFileSystem() // Copied from the unit test project
});
// Get the render function for our FileReady view.
var fileReadyRenderer = handlebars.CompileView("Views/EmailTemplates/FileReady.hbs");
// Render the view.
var html = fileReadyRenderer(new
{
SiteUrl = "http://dev.example.com",
FileName = "SomeFile.pdf"
});
Layout.hbs contains the following markup:
<div class="footer">
Please view our <a href="{{SiteUrl}}/privacypolicy/">privacy policy</a>
</div>
And this is the output for that section:
<div class="footer">
Please view our <a href="/privacypolicy/">privacy policy</a>
</div>
Even though FileReady.hbs properly fills in {{SiteUrl}}, Layout.hbs does not.
Is there a configuration setting or special method to enable this?
I created a sample project here to reproduce the issue:
https://github.com/jonsagara/HandlebarsNetLayoutTest
Thank you.
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (7 by maintainers)
Top Results From Across the Web
View engine does not render the view when I pass a model ...
I am developing an MVC3 application using C# and Razor. I have a problem when I need to display one the Play View....
Read more >Variables in layout with Script view pages
I'm converting a small projected from netcore2.1 to netcore3, but it used Razor, which no longer picks up file changes without restarting ...
Read more >Using a view engine to render template files
When a variable has not been defined in the server code and you attempt to display its contents to the user in your...
Read more >Layout in ASP.NET Core
Learn how to use common layouts, share directives, and run common code before rendering views in an ASP.NET Core app.
Read more >Using EJS Template Engine With Express.js
Let's use the variables we just defined in index.ejs. Display a single variable in EJS: A single variable can be represented by using...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

This fix was released as of 1.9.3!
The only potential issue I see there is the order of the objects - if
vmhappens to have a property calledbody, the viewmodel would resolve to that one first and never reach the latter. If we reverse the order, that should ensure the basic behavior is protected. Feel free to go ahead and send a PR in.