Skip to content

Commit 66076b1

Browse files
committed
Added basic DIY templating support
and removed the ill-conceived LazyNode
1 parent 2813138 commit 66076b1

File tree

5 files changed

+109
-29
lines changed

5 files changed

+109
-29
lines changed

src/Cubist.Helium.Tests/HeTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,59 @@ public void CanRenderJsonNode()
3737
_output.WriteLine(html);
3838
Assert.Equal("{\"text\":\"some text\",\"bool\":true}", html);
3939
}
40+
41+
[Fact]
42+
public void CanRenderTemplateNode()
43+
{
44+
var tt = new TextTemplate();
45+
var link = A("#text", tt);
46+
47+
48+
var html = link.ToString();
49+
_output.WriteLine(html);
50+
Assert.Equal("<a href=\"#text\"></a>", html);
51+
52+
tt.Text = "Some Text";
53+
var html2 = link.ToString();
54+
_output.WriteLine(html2);
55+
Assert.Equal("<a href=\"#text\">Some Text</a>", html2);
56+
}
57+
58+
[Fact]
59+
public void CanRenderTemplateNodeAsAttributeValue()
60+
{
61+
var tt = new TextTemplate();
62+
var link = Div(("class", tt));
63+
64+
65+
var html = link.ToString();
66+
_output.WriteLine(html);
67+
Assert.Equal("<div class=\"\"></div>", html);
68+
69+
tt.Text = "myClass";
70+
var html2 = link.ToString();
71+
_output.WriteLine(html2);
72+
Assert.Equal("<div class=\"myClass\"></div>", html2);
73+
}
74+
75+
private class TextTemplate : ITemplate
76+
{
77+
private string _text = "";
78+
private Text _cachedNode = Text("");
79+
80+
public string Text
81+
{
82+
get => _text;
83+
set
84+
{
85+
_text = value;
86+
_cachedNode = Text(value);
87+
}
88+
}
89+
90+
public Node Render() => _cachedNode;
91+
}
92+
4093
[Fact]
4194
public void CanRenderImportMap()
4295
{

src/Cubist.Helium/He.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ public He Attr(string name, object? value = null)
5252
throw new ArgumentException("name cannot be null or empty.", nameof(name));
5353

5454
_attrs ??= new();
55-
_attrs.Add((name, value));
55+
_attrs.Add((name, value switch
56+
{
57+
ITemplate t => new Template(t),
58+
_ => value
59+
}));
5660
return this;
5761
}
5862

@@ -165,6 +169,7 @@ public He Add(IEnumerable content)
165169
Node n => Add(n),
166170
string s => Add(s),
167171
ITuple { Length: 2 } tuple => Attr(tuple[0] as string ?? $"{tuple[0]}", tuple[1]),
172+
ITemplate t => Add(new Template(t)),
168173
JsonNode json => Add(Json(json)),
169174
IEnumerable nodes => AddRange(nodes),
170175
bool boolValue => Add(boolValue.ToString()),

src/Cubist.Helium/ITemplate.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Cubist.Helium
8+
{
9+
/// <summary>
10+
/// You can implement the <see cref="ITemplate"/> interface to implement your own template engine.
11+
/// <see cref="ITemplate"/> instances will be wrapped by a <see cref="Template"/> instance,
12+
/// which will invoke <see cref="ITemplate.Render"/> every time <see cref="Node.WriteTo"/> or <see cref="Node.PrettyPrintTo"/> is called.
13+
/// </summary>
14+
public interface ITemplate
15+
{
16+
/// <summary> Renders the template to a <see cref="Node"/> </summary>
17+
Node Render();
18+
}
19+
}

src/Cubist.Helium/LazyNode.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/Cubist.Helium/Template.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace Cubist.Helium;
2+
3+
/// <summary> Node that renders the supplied <see cref="ITemplate"/> implementation </summary>
4+
public class Template : Node
5+
{
6+
private readonly ITemplate _template;
7+
8+
/// <summary> Creates a new <see cref="Template"/> instance </summary>
9+
public Template(ITemplate template)
10+
{
11+
_template = template;
12+
}
13+
14+
/// <inheritdoc cref="Node.WriteTo"/>>
15+
public override void WriteTo(TextWriter w)
16+
{
17+
_template.Render().WriteTo(w);
18+
}
19+
20+
/// <inheritdoc cref="Node.PrettyPrintTo"/>>
21+
public override void PrettyPrintTo(IndentWriter w)
22+
{
23+
_template.Render().PrettyPrintTo(w);
24+
}
25+
26+
/// <inheritdoc cref="Node.ToString"/>>
27+
public override string ToString()
28+
{
29+
return _template.Render().ToString();
30+
}
31+
}

0 commit comments

Comments
 (0)