question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ViewEngine: referencing variables in layout does not appear to work

See original GitHub issue

I have a project that uses two files on disk to simulate an email templating engine:

  • Layout.hbs - a layout file containing common markup
  • FileReady.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:closed
  • Created 6 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
rexmcommented, May 26, 2018

This fix was released as of 1.9.3!

1reaction
rexmcommented, Jun 30, 2017

The only potential issue I see there is the order of the objects - if vm happens to have a property called body, 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found