Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for writing binary content to output #99

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions ExampleAppCore/ExampleAppCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<LangVersion>latest</LangVersion>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

Expand All @@ -12,10 +13,6 @@
<ItemGroup>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\RazorEngineCore\RazorEngineCore.csproj" />
</ItemGroup>
Expand Down
151 changes: 143 additions & 8 deletions ExampleAppCore/Program.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,147 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipelines;
using System.Text;
using System.Threading.Tasks;
using RazorEngineCore;

namespace ExampleApp
{
public class TestModel : RazorEngineTemplateBase
public abstract class BinaryDataEngineTemplateBase<T> : BinaryDataEngineTemplateBase
{
public new T Model { get; set; }
}

public class BinaryDataEngineTemplateBase : IRazorEngineTemplate<byte[]>
{
readonly MemoryStream binaryWriter = new();

Encoding encoding = Encoding.UTF8;

private string attributeSuffix = null;

public dynamic Model { get; set; }

public void WriteLiteral(string literal = null)
{
WriteLiteralAsync(literal).GetAwaiter().GetResult();
}

public virtual Task WriteLiteralAsync(string literal = null)
{
Write(literal);
return Task.CompletedTask;
}

public void Write(object obj = null)
{
WriteAsync(obj).GetAwaiter().GetResult();
}

public virtual Task WriteAsync(object obj = null)
{
if (obj is byte[] bytes)
binaryWriter.Write(bytes);
else
binaryWriter.Write( encoding.GetBytes(obj.ToString()));
return Task.CompletedTask;
}

public void BeginWriteAttribute(string name, string prefix, int prefixOffset, string suffix, int suffixOffset,
int attributeValuesCount)
{
BeginWriteAttributeAsync(name, prefix, prefixOffset, suffix, suffixOffset, attributeValuesCount).GetAwaiter().GetResult();
}

public virtual Task BeginWriteAttributeAsync(string name, string prefix, int prefixOffset, string suffix, int suffixOffset, int attributeValuesCount)
{
this.attributeSuffix = suffix;
Write(prefix);
return Task.CompletedTask;
}

public void WriteAttributeValue(string prefix, int prefixOffset, object value, int valueOffset, int valueLength,
bool isLiteral)
{
WriteAttributeValueAsync(prefix, prefixOffset, value, valueOffset, valueLength, isLiteral).GetAwaiter().GetResult();
}

public virtual Task WriteAttributeValueAsync(string prefix, int prefixOffset, object value, int valueOffset, int valueLength, bool isLiteral)
{
Write(prefix);
Write(value);
return Task.CompletedTask;
}

public void EndWriteAttribute()
{
EndWriteAttributeAsync().GetAwaiter().GetResult();
}

public virtual Task EndWriteAttributeAsync()
{
Write(attributeSuffix);
this.attributeSuffix = null;
return Task.CompletedTask;
}

public void Execute()
{
ExecuteAsync().GetAwaiter().GetResult();
}

public virtual Task ExecuteAsync()
{
return Task.CompletedTask;
}

public virtual byte[] Result()
{
return ResultAsync().GetAwaiter().GetResult();
}

public virtual Task<byte[]> ResultAsync()
{
return Task.FromResult<byte[]>( binaryWriter.ToArray() );
}
}

public class TestModel : BinaryDataEngineTemplateBase<TestModel>
{
public string Name { get; set; }
public IEnumerable<int> Items { get; set; }
public IEnumerable<string> Items { get; set; }
public byte[] BinaryData { get; set; }
}



class Program
{
static string Content = @"
static readonly string Content = @"
Hello @Model.Name

@{
string testheader = "" ---------------- mytest: will show the modified items ----------- "";
List<string> myitems = new ();
foreach (var item in Model.Items) {
myitems.Add("" ---->>>> "" + item);
}
}

@foreach(var item in @Model.Items)
{
<div>- @item</div>
}
@testheader
@foreach(var myitem in @myitems)
{
<text>| @(myitem) | @(myitem) | @(myitem) |
</text>
}
Binary data below:
@Model.BinaryData
End of Binary data

<div data-name=""@Model.Name""></div>

Expand All @@ -42,19 +164,32 @@ void RecursionTest(int level){
static void Main(string[] args)
{
IRazorEngine razorEngine = new RazorEngine();
IRazorEngineCompiledTemplate template = razorEngine.Compile(Content);
var template = razorEngine.Compile<BinaryDataEngineTemplateBase<TestModel>, byte[]>(Content);

/*
StringBuilder stringBuilder = new StringBuilder();
byte[] somebytes = { 0x02, 0x04, 0x50 };
stringBuilder.Append(somebytes);

var s = stringBuilder.ToString();
*/

string result = template.Run(new
TestModel model = new TestModel()
{
Name = "Alexander",
Items = new List<string>()
{
"item 1",
"item 2"
}
});
},
BinaryData = new byte[10] { 0x41, 0x0A, 0x0D, 0x00, 0x30, 0x31, 0x00, 0x0A, 0x0D, 0x41 }
};



byte[] result = template.Run(instance => instance.Model = model);

Console.WriteLine(result);
Console.WriteLine(Encoding.UTF8.GetString(result));
Console.ReadKey();
}
}
Expand Down
2 changes: 1 addition & 1 deletion ExampleAppNET5/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void RecursionTest(int level){
static void Main(string[] args)
{
IRazorEngine razorEngine = new RazorEngine();
IRazorEngineCompiledTemplate template = razorEngine.Compile(Content);
IRazorEngineCompiledTemplate<string> template = razorEngine.Compile(Content);

string result = template.Run(new
{
Expand Down
2 changes: 1 addition & 1 deletion ExampleAppNet472/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void RecursionTest(int level){
static void Main(string[] args)
{
IRazorEngine razorEngine = new RazorEngine();
IRazorEngineCompiledTemplate template = razorEngine.Compile(Content);
IRazorEngineCompiledTemplate<string> template = razorEngine.Compile(Content);

string result = template.Run(
new
Expand Down
2 changes: 1 addition & 1 deletion RazorEngineCore.Tests/Models/TestTemplate1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace RazorEngineCore.Tests.Models
{
public class TestTemplate1 : RazorEngineTemplateBase
public class TestTemplate1 : RazorEngineTemplateBase<string>
{
public int A { get; set; }
public int B { get; set; }
Expand Down
Loading