I’m converting a small projected from netcore2.1 to netcore3, but it used Razor, which no longer picks up file changes without restarting the application.
Essentially, any variables inserted before {{ page }} is used are ignored.
Is this how it works?
I can’t find the documentation for “page” but from the few examples I could find online, it looks like it is a replacement for RenderBody(). Is this the right way?
Yes, this is documented in Arguments, only static page arguments <!-- --> or arguments defined in PageResult and ScriptContext are available before the page is evaluated.
Yes, this is documented in Arguments, only static page arguments <!-- --> or arguments defined in PageResult and ScriptContext are available before the page is evaluated.
Thanks. I guess I’m asking about #raw appendTo variables, is that the same? Aren’t they set by the page that would then be available in the _layout?
Sorry, I just not understanding, as this seems it would make the concept of layouts impossible, as you can’t reference a section until after the page has been parsed…but you would expect the page to get parsed first and then the layout.
Do you have any links to more sophisticated examples that show replicating Razor? That might help me understand.
Parsed isn’t the issue, all pages are parsed into a cached AST before they’re run.
The page needs to be evaluated which it isn’t done until {{ page }}, exactly the same as it is for partials (or any other code), i.e. you cannot access modified values from code that hasn’t been run yet. When it’s evaluated it’s written directly to the output stream after where any of its side-effects (i.e. modified values) can be accessed within _layout or any other subsequent partials.
You wont be able to access variables from the page in your layout before it’s rendered. You’d need to either pass it in as arguments from the Service or your layout page can embed a separate partial or static file. If it’s static you can utilize page arguments like:
Your script references should ideally be at the bottom of the page and you can use {{#raw apppendTo scripts}} to append any scripts after the layout script references. Your style includes don’t need to be in the head section as long as they’re at the start of your page it will minimize the number of redraws browsers needs to do.
No it’s not possible to access any values within the page before it’s rendered, the options I’ve mentioned in my previous comment are still the only ones.
Ok, thanks. Style, scripts and so on, I was just trying to give examples of a mix of logic and static content that needs to be near the top. It’s a shame {#raw appendTo …} can’t append to the head section, or anything before the {{ page }} tag appears.
As both includeFile and partial allow you to specify relative references, you can include relative references to the static file (includeFile) or partial next to the page you want to dynamically render, e.g: