RSCG–Template Rendering- part 17
name | Transplator |
nuget | https://www.nuget.org/packages/Transplator/ |
link | https://github.com/atifaziz/Transplator/ |
author | Atif Aziz |
The Transplator is a small fast rendering engine to allow you to make rendering from any class instance. The code that you start with is
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | {% using System.Collections.Generic; using System.Linq; using System.Text; using Rendering; static partial class EmployeeRendering { public static string Render( params Employee[] employees) { var sb = new StringBuilder(); int i= 0; -%} Number Employees: {% employees?.Length %} {%~ foreach ( var emp in employees) { i++; ~%} {% i %}. {% emp.Name %} it is in {% emp.Department?.Name %} {%~ } ~%} {% return sb.ToString(); void WriteText( string value) => sb.Append(value); void WriteValue( object value) => sb.Append(value); } } ~%} |
The code that you will use is
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 | var IT = new Department(); IT.Name = "IT" ; var e = new Employee(); e.ID = 10; e.Name = "Andrei Ignat" ; e.Department = IT; var render = EmployeeRendering.Render(e); Console.WriteLine(render); |
The code that is generated is
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | using System.Collections.Generic; using System.Linq; using System.Text; using Rendering; static partial class EmployeeRendering { public static string Render( params Employee[] employees) { var sb = new StringBuilder(); int i= 0; WriteText( @"Number Employees: " ); WriteValue(employees?.Length); WriteText( @" " ); foreach ( var emp in employees) { i++; WriteText( @" " ); WriteValue(i); WriteText( @". " ); WriteValue(emp.Name); WriteText( @" it is in " ); WriteValue(emp.Department?.Name); WriteText( @" " ); } return sb.ToString(); void WriteText( string value) => sb.Append(value); void WriteValue( object value) => sb.Append(value); } } |
Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/TemplateRender