tyrrrz / minirazor Goto Github PK
View Code? Open in Web Editor NEWPortable Razor compiler & code generator
License: MIT License
Portable Razor compiler & code generator
License: MIT License
To make the output file smaller, it would be nice to be able to optionally remove insignificant whitespace. This would only work with HTML but theoretically Razor could be used for any type of output, so maybe consider some more generic solution?
If a template were to declare a tuple type model, such as when using @inherits MiniRazor.TemplateBase<(string name, int age)>
a static RenderAsync
method is generated.
/// <summary>Renders the template using the specified writer.</summary>
public static async global::System.Threading.Tasks.Task RenderAsync(global::System.IO.TextWriter output, (string name, int age) model)
{
var template = new Template();
template.Output = output;
template.Model = model;
await template.ExecuteAsync().ConfigureAwait(false);
}
/// <summary>Renders the template to a string.</summary>
public static async global::System.Threading.Tasks.Task<string> RenderAsync((string name, int age) model)
{
using (var output = new global::System.IO.StringWriter())
{
await RenderAsync(output, model).ConfigureAwait(false);
return output.ToString();
}
}
In the case that the model is detected to be a tuple type (possibly only when each tuple item has a name), I would desire that two additional overloads of RenderAsync
be generated.
public static global::System.Threading.Tasks.Task RenderAsync(global::System.IO.TextWriter output, string name, int age) => RenderAsync(output, (name, age));
public static global::System.Threading.Tasks.Task<string> RenderAsync(string name, int age) => RenderAsync((name, age));
Add a new property called CancellationToken
to TemplateBase
:
Add CancellationToken
parameter to TemplateDescriptor.RenderAsync(...)
:
https://github.com/Tyrrrz/MiniRazor/blob/master/MiniRazor.Compiler/TemplateDescriptor.cs#L25-L50
Add CancellationToken
parameter to codegened RenderAsync(...)
:
MiniRazor/MiniRazor.CodeGen/TemplateClassGenerator.cs
Lines 99 to 118 in 67201b6
Can you support compiling to DLL files? I checked the source code and found that the contents of the DLL already exist, but you got the type from the DLL and returned it.
I do not Razor as a dependency when adding the reference , right ?
But I need MiniRazor.TemplateBase as a reference
Could you put MiniRazor.TemplateBase in a separate project ?
This way I can do
to not reference Razor when dot net publish
Is there a reason why MiniRazor.CodeGen
is not published as a NuGet package?
Right now i am referencing it as a git submodule like a caveman. ๐
First and foremost, great work here! nice compact library, with a pleasant API surface area!
As I start to dig in, the first thing that presents itself is the need to support more complex layout, and most project contributors will obviously be familiar with the asp.net mvc's method for doing so.
These pieces are missing from the basic examples given so far ... is there another method built-in for templating in MiniRazor?
It appears that when a class is generated for a razor file, it is always generated as an internal class. While this makes sense as a sensible default, there are cases where a public class would be desirable, such as cases where the templates are to be shared among different projects or to allow use in projects where source generators either are not allowed or are difficult to use (such as older style projects for web applications based on System.Web). Perhaps this can be done in the <AdditionalItems>
element, with an attribute such as 'AccessModifier="public"
. Currently I have made a static class which contains one static method per template, each of which simply wraps the call to the generated RenderAsync
method, which eliminates most of the benefits of having generators to begin with.
I ran into an issue with a .Net 5 project using MiniRazor that also references ZXing.Net.
I am not really sure where the fault lies, but while loading transitive dependencies, MiniRazor is attempting to load a reference to "PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" that I found out is referenced by ZXing.Net.
Our .Net 5 project is referencing the .Net Standard 2.0 version of ZXing.Net which doesn't seems to have any "active" references to PresentationCore, or at least not for the functionality we are using. GetReferencedAssemblies as part of Compile in MiniRazor returns the PresentationCore reference though, and MiniRazor attempts to load it.
Does it make sense to include an overload or configuration option in MiniRazor that would allow a consumer to specify a list of references to ignore/exclude, to be able to work around situation like this? I could perhaps provide a PR if we can agree on a suitable solution.
I have recently had a desire to use Templated Razor Delegates in some of my razor templates recently, and found that the body ends up erased in the generated code. After looking into the diagnostics returned by the razor engine and further research, I believe this feature can be easily enabled. The feature requires adding an ITemplateTargetExtension
as a target extension, and razor provides an implementation in TemplateTargetExtension
. This requires that the template has access to a PushWriter(TextWriter)
method and a PopWriter()
method to temporarily swap the current TextWriter
being used by the template, and access to a type which will wrap the TextWriter
, has a constructor which will accept a delegate compatible with Func<TextWriter, Task>
, and can be passed to the template's Write()
method in order to render the contents of the wrapped TextWriter
.
From what I can tell, the implementation of the wrapper type can be fairly simple and still work.
public class TemplateResult {
private readonly Func<TextWriter, Task> _TemplateDelegate;
public TemplateResult(Func<TextWriter, Task> templateDelegate) => _TemplateDelegate = templateDelegate;
public override ToString() {
using var output = new StringWriter();
// Proper async code could probably be generated by implementing a GetAwaiter() method and requiring the result of
// the invocation of the razor delegate be awaited. However, all implementations I've seen, including the implementation
// built in to MVC use this method to convert the async delegate into a sync call.
_TemplateDelegate(output).GetAwaiter().GetResult();
return output.ToString();
}
}
ver 2.2.1
I'm using MiniRazor within a .NET Standard 2.0 project (as this is the maximum version allowed to reference this project from a UWP project). Using compile-time template generation yields the following error:
Feature 'nullable reference types' is not available in C# 7.3. Please use language version 8.0 or greater.
The NuGet package listing seems to indicate this package is compatible with .NET Standard 2.0.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.