Git Product home page Git Product logo

dima1034 / razortemplating Goto Github PK

View Code? Open in Web Editor NEW

This project forked from soundaranbu/razor.templating.core

0.0 2.0 0.0 899 KB

Razor Templating Engine to render Razor Views(.cshtml files) to String in Console, Web, Service, Desktop workloads in .NET Core 3+

Home Page: https://soundaranbu.medium.com/render-razor-view-cshtml-to-string-in-net-core-7d125f32c79

License: Apache License 2.0

C# 100.00%

razortemplating's Introduction

Razor Templating

Build+Test Nuget Downloads

Using Razor for HTML templating was never been so easy like this. Render your .cshtml files into string easily using this library.

This project makes use of Razor SDK for precompiling the views.

Supported Application Types

.NET Core 3.0 .NET Core 3.1 .NET 5
Console
Api
Mvc
Worker Service
WPF
WinForms

Applications

  • Email Templating
  • Report Generation & so on

Installing Nuget Package

This library is available as Nuget package

Using .NET CLI
dotnet add package Razor.Templating.Core
Using Package Reference .csproj
<PackageReference Include="Razor.Templating.Core" Version="1.4.0" />

Simple Usage:

using Razor.Templating.Core;

var model = new ExampleModel()
{
    PlainText = "This text is rendered from Razor Views using Razor.Templating.Core",
    HtmlContent = "<em>You can use it to generate email content, report generation and so on</em>"
};

var viewData = new Dictionary<string, object>();
viewData["Value1"] = "1";
viewData["Value2"] = "2";

var html = await RazorTemplateEngine.RenderAsync("/Views/ExampleView.cshtml", model, viewData);

Before applying this code, follow this article for working implementation: https://medium.com/@soundaranbu/render-razor-view-cshtml-to-string-in-net-core-7d125f32c79

Dependency Injection [Since v1.4.0]

Dependencies can be injected directly into views using @inject in .csthml file. Refer sample application here

In ASP.NET Core, add dependency like below in Startup.cs -> ConfigureServices

...
services.AddTransient<ExampleService>();
//add after registering all the dependencies
services.AddRazorTemplating();

or in console or other applications, add as below

// Add dependencies to the service collection
var services = new ServiceCollection();
services.AddTransient<ExampleService>();
// Add RazorTemplating after registering all dependencies
// this is important for the razor template engine to find the injected services
services.AddRazorTemplating(); 
var html = await RazorTemplateEngine.RenderAsync("~/Views/ExampleViewServiceInjection.cshtml");

Razor Views in Library

Razor view files(.cshtml) can be organized in a separate shared Razor Class Libary(RCL). Sample library can be found here

Sample Applications

Please find the sample applications here

Note:

  • Please ensure that the views path is always unique among all the shared template projects.

References:

Thanks to all the great articles and projects which helped to bring this library out!

razortemplating's People

Contributors

soundaranbu avatar litan1106 avatar soundarrbt avatar

Watchers

James Cloos avatar  avatar

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.