Comments (4)
I've added full support for layouts and sections in v0.5.0, enjoy! 🙂
from razorblade.
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.
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.
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)
- `CancellationToken` in templates HOT 3
- Make the generator self-contained (no runtime dependency) HOT 9
- Can't use template with a model when RazorBladeEmbeddedLibrary is true HOT 4
- DiagnosticId is not available in netstandard2.0 HOT 2
- Cannot use RazorBlade in a roslyn SourceGenerator, because it requires C# 10, and source generators need to target netstandard2.0 which only has c# 7.3 HOT 2
- Tag helpers don't work HOT 5
- Forward base class constructors by default HOT 16
- When trying the example with a model, I get a template generated which do not accept a model arg in the ctor ? HOT 1
- Streaming HTML HOT 2
- Getting IDE error HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from razorblade.