Giter Club home page Giter Club logo

Comments (4)

ltrzesniewski avatar ltrzesniewski commented on July 24, 2024 1

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

from razorblade.

ltrzesniewski avatar ltrzesniewski commented on July 24, 2024

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.

from razorblade.

naasking avatar naasking commented on July 24, 2024

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.

from razorblade.

ltrzesniewski avatar ltrzesniewski commented on July 24, 2024

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

from razorblade.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.