DotLiquid - Drops

使用 DotLiquid 做範本渲染時,如果需要使用到非基礎型別當作參數,我們可以為其建立對應的 Drop 型別。


該 Drop 型別繼承自 DotLiquid 的 Drop 型別,在建構子將原型別實體帶入,將原型別具有的成員屬性封裝並開出。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System.Drawing;

namespace DotLiquid.Drops.Model
{
public class PointDrop : Drop
{
private readonly Point _point;

public PointDrop(Point point)
{
_point = point;
}

public int X
{
get => _point.X;
}

public int Y
{
get => _point.Y;
}
}
}


渲染時將參數改成自建的 Drop 型別帶入即可。

1
2
3
4
5
...
var model = new {points = points.Select(item => new PointDrop(item)).ToArray()};

return Generate(templateContext, model);
...


程式寫起來會像下面這樣:

1
2
3
4
5
6
7
8
9
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
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using DotLiquid.Drops.Model;

namespace DotLiquid.Drops
{

class Program
{
static void Main(string[] args)
{
var result = GenerateHelloWorld(new Point(95,27));

Console.WriteLine(result);
}

static string Generate(string templateContext, object model)
{
var template = Template.Parse(templateContext);
return template.Render(Hash.FromAnonymousObject(model));
}

static string GenerateHelloWorld(params Point[] points)
{
var templateContext = ReadContentFromResource("DotLiquid.Drops.Template.HelloWorld.tpl");
var model = new {points = points.Select(item => new PointDrop(item)).ToArray()};

return Generate(templateContext, model);
}

private static string ReadContentFromResource(string resourceName)
{
return ReadContentFromResourceAsync(resourceName).Result;
}


private static async Task<string> ReadContentFromResourceAsync(string resourceName)
{
var assembly = Assembly.GetExecutingAssembly();
using (var stream = assembly.GetManifestResourceStream(resourceName))
using (var reader = new StreamReader(stream))
{
return await reader.ReadToEndAsync();
}
}
}
}


範本部分很單純,不需要做什麼處理。

1
2
3
{% for point in points -%}
Hello ({{point.X}}, {{point.Y}})~
{% endfor -%}


運行起來會看到渲染的結果。