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

Layout support #6

Closed
jureperak opened this issue Jan 17, 2023 · 4 comments
Closed

Layout support #6

jureperak opened this issue Jan 17, 2023 · 4 comments
Labels
enhancement New feature or request
Milestone

Comments

@jureperak
Copy link

Hi, i was playing around with this package and cannot figure out how would i make a .cshtml with Layout support? So i want to have two .cshtml and one of them would be a wrapper for the other.

e.g. in classic razor pages we are able to do in layout (lets call it SimpleLayout.cshtml)

<header>This is header</header>
@RenderBody()
<footer>This is footer</footer>

and then in view:

@{
    Layout = "SimpleLayout"
}

My content!

so the actual result would be:

This is header
My content!
This is footer

Greetings!

@ltrzesniewski
Copy link
Owner

Hello,

There is currently no layout support just like in ASP.NET, but that's a useful feature I suppose I should add eventually.

However, you can work around this limitation by writing two template files such as Header.cshtml and Footer.cshtml, then use them the following way:

@(new Header())
My content!
@(new Footer())

This doesn't handle sections, but similar workarounds should be possible for them by passing parameters to the Header/Footer templates.

@ltrzesniewski ltrzesniewski added the enhancement New feature or request label Jan 17, 2023
@naasking
Copy link

This is a neat project. I've only just found this project so I'm not as familiar with all of the details, but from the type definitions I see, you could provide a efficient version of this for simple layout templates as follows:

@{
    Layout = new SimpleLayout();
}

My content!

The base definitions are changed slightly:

public interface IEncodedContent
{
    // the layout into which this content is rendered
    IEncodedContent? Layout { get; }
    // recursive, nested rendering pipeline
    void WriteTo(TextWriter textWriter, IEncodedContent? body);
}

The render procedure for a template then executes these "inside-out":

Layout?.Render(textWriter, this);

where the body parameter bound to this is available as a parameter in the razor file to execute in the appropriate place, eg. substitute body.Render() wherever RenderBody() is invoked. This nests arbitrarily because of the recursive design, so the layout template can itself have a layout template, ad infinitum.

This only supports a single template-body distinction, but it could possibly later be extended to provide full section support by changing the body parameter a Dictionary<string, IEncodedContent>, where the key is the section name.

As a brief aside, it would be helpful to have a more detailed breakdown or comparison of RazorBlade to Razor slices which seems to have a similar goal, but slightly different design tradeoffs.

@ltrzesniewski
Copy link
Owner

Thanks! 🙂

Yes, I considered implementing something like Layout = new SimpleLayout();, but not the way you describe, as IEncodedContent is somewhat similar to IHtmlString in ASP.NET, and I'd like to keep that interface simple.

I added a writer stack on another branch (07a829a) which could be useful for layouts. I'd also like to have full section support initially. I just need to find some time to work on this.

There are many existing Razor libraries, but I wasn't aware of RazorSlices. I wrote RazorBlade because I wanted two things I couldn't find elsewhere:

  • a Razor library which does not reference anything from ASP.NET
  • for the Razor templates to be compiled at build-time

@ltrzesniewski ltrzesniewski added this to the v0.5.0 milestone Dec 2, 2023
@ltrzesniewski
Copy link
Owner

I've added full support for layouts and sections in v0.5.0, enjoy! 🙂

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

No branches or pull requests

3 participants