Weave by John Gietzen
Nuget / site data
Details
Info
Name: Weave
Weave is a text templating engine that is all about attention to detail. Weave handles the tricky work of making your rendered text beautiful.
Author: John Gietzen
NuGet: https://www.nuget.org/packages/Weave/
You can find more details at https://github.com/otac0n/Weave
Source : https://github.com/otac0n/Weave
Original Readme
Weave
Weave is a text templating engine for .NET that is all about attention to detail. Weave handles the tricky work of making your rendered text beautiful.
Getting Started
The easiest way to get a copy of Weave is to install the Weave NuGet package in Visual Studio.
PM> Install-Package Weave
Due to a limitation in Visual Studio, you will need to reload your project for the 'WeaveTemplate' build action to be recognized.
Once you have the package installed, files in your project marked as 'WeaveTemplate' in the properties window will be compiled to their respective .weave.cs
template classes before every build. These template classes will be automatically included in compilation.
For help with template syntax, see the Syntax Guide wiki entry
Example
@namespace MyProject
@methodname RenderFizzBuzz
@model IEnumerable<int>
{{each i in model}}
{{if i % 3 == 0 && i % 5 == 0}}
{{= i }} FizzBuzz
{{elif i % 3 == 0}}
{{= i }} Fizz
{{elif i % 5 == 0}}
{{= i }} Buzz
{{else}}
{{= i }}
{{/if}}
{{/each}}
This would generate a static (by default) method named RenderFizzBuzz
in the Templates
class (again, by default). You would use this method like so:
Templates.RenderFizzBuzz(Enumerable.Range(0, 100), Console.Out);
Any TextWriter
is supported. To get the text as a string, use a StringWriter
.
About
Scriban like generator
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- Person.cs
This is the CSharp Project that references Weave
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Remove="MyDataT.weave" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Weave" Version="2.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<WeaveTemplate Include="MyDataT.weave">
</WeaveTemplate>
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of Weave in Program.cs
using WeaveDemo;
Person p = new()
{
FirstName = "Andrei", LastName = "Ignat"
};
MyProject.Templates.RenderFizzBuzz(p, Console.Out);
StringWriter sw = new();
MyProject.Templates.RenderFizzBuzz(p, sw);
Console.WriteLine("---------------------------");
Console.WriteLine(sw.ToString());
This is the use of Weave in Person.cs
namespace WeaveDemo;
internal class Person
{
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string FullName() => $"{FirstName} {LastName}";
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- .._p5C.._p5C.._p5CMyDataT.weave.g.cs
// -----------------------------------------------------------------------
// <auto-generated>
// This code was generated by Weave 2.1.0.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
// -----------------------------------------------------------------------
namespace
#line 1 "..\..\..\MyDataT.weave"
MyProject
#line default
{
using System;
using System.IO;
partial class
Templates
{
[System.CodeDom.Compiler.GeneratedCode("Weave", "2.1.0.0")]
public
static void
#line 2 "..\..\..\MyDataT.weave"
RenderFizzBuzz
#line default
(
#line 3 "..\..\..\MyDataT.weave"
WeaveDemo.Person
#line default
model, TextWriter writer, string indentation = null)
{
var __originalIndentation = indentation = indentation ?? string.Empty;
writer.Write(indentation);
writer.Write("I will write p.FullName();");
writer.WriteLine();
writer.WriteLine();
writer.Write(indentation);
writer.Write(
#line 7 "..\..\..\MyDataT.weave"
model.FullName()
#line default
);
writer.WriteLine();
}
}
}
Usefull
Download Example (.NET C# )
Share Weave
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Weave