Coder Social home page Coder Social logo

tyrrrz / minirazor Goto Github PK

View Code? Open in Web Editor NEW
221.0 8.0 24.0 168 KB

Portable Razor compiler & code generator

License: MIT License

C# 98.60% HTML 1.40%
csharp-sourcegenerator razor-templates razor roslyn compilation templates template-engine hacktoberfest

minirazor's People

Contributors

kevingliewe avatar slang25 avatar thejaymann avatar tyrrrz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

minirazor's Issues

Option to remove insignificant whitespace

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?

Add overload for RenderAsync when model is a tuple type

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));

Pass `CancellationToken` through `RenderAsync(...)` and expose it in the template

Details

Add a new property called CancellationToken to TemplateBase:

https://github.com/Tyrrrz/MiniRazor/blob/67201b654fb5e67563b98b2c6686b3d4aa07fe35/MiniRazor.Runtime/TemplateBase.cs

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(...):

/// <summary>Renders the template using the specified writer.</summary>
public static async global::System.Threading.Tasks.Task RenderAsync(global::System.IO.TextWriter output, {modelTypeName} model)
{{
var template = new {className}();
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({modelTypeName} model)
{{
using (var output = new global::System.IO.StringWriter())
{{
await RenderAsync(output, model).ConfigureAwait(false);
return output.ToString();
}}
}}
");

Support compile to dll

Details

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.

MiniRazor.CodeGen as NuGet package?

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. ๐Ÿ˜„

Render another template from inside a template

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?

Allow public access modifier for generated class

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.

Transitive assembly loading is too eager

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.

Add support for templated razor delegates

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();
    }
}

C# versioning error when using in .NET Standard 2.0 project

Version

ver 2.2.1

Details

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.

Steps to reproduce

  • Create a new .NET Standard 2.0 project.
  • Install the MiniRazor Nuget package.
  • Create a Razor template and set it be built at compile time as per documentation.
  • Observe error message on build.

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.