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


    {%

    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



    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


    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

All RSCG

NrBlog Post
1RSCG–part 1
2RSCG- AppVersion–part 2
3http://msprogrammer.serviciipeweb.ro/2021/02/17/rsgc-enum-part-3/
4RSGC-JSON to Class- part 4
5RSGC-Constructor – Deconstructor – part 5
6RSGC – DTO Mapper – part 6
7RSGC – Skinny Controllers- part 7
8RSGC-Builder Design Pattern – part 8
9RSGC- MetadataFromObject – part 9
10RSGC- Dynamic Mock – part 10
11RSCG- Method Decorator – part 11
12RSCG – Curry – Partial function – part 12
13RSCG- part 13 – IFormattable
14RSCG- part 14 – DP_Decorator
15RSCG- part 15 – Expression Generator
16RSCG- part 16 – Many Others
17RSCG- the book
18RSCG–Template Rendering- part 17
19CI Version
20HttpClientGenerator
21Query from database
22AutoRegister
23TinyTypes
24Static2Interface
25AppSettings
26Properties
27
Roslyn Source Code Generators