forked from dotliquid/dotliquid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtending.cshtml
More file actions
164 lines (122 loc) · 3.89 KB
/
Extending.cshtml
File metadata and controls
164 lines (122 loc) · 3.89 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
@{
ViewBag.IncludePrism = true;
}
<div id="docs-content">
<markdown>
# Extending DotLiquid
Extending DotLiquid is very easy. If you do create useful filters or tags, please consider creating a pull request.
## Create your own filters
Creating filters is very easy. Filters are just methods which take one parameter and return a modified string.
You can use your own filters by passing an array of filter types to the `Render` call like this:
``` csharp
template.Render(new RenderParameters(CultureInfo.CurrentCulture)
{
Filters = new[] { typeof(MyTextFilters), typeof(MyDateFilters) }
});
```
Example:
``` csharp
public static class TextFilter
{
public static string Textilize(string input)
{
return TextileFormatter.FormatString(input);
}
}
Template template = Template.Parse(" {{ '*hi*' | textilize }} ");
template.Render(new RenderParameters(CultureInfo.CurrentCulture)
{
Filters = new[] { typeof(TextFilter) }
});
```
Alternatively, you can register your filters globally:
``` csharp
public static class TextFilter
{
public static string Textilize(string input)
{
return TextileFormatter.FormatString(input);
}
}
Template.RegisterFilter(typeof(TextFilter));
```
Once the filter is globally registered, you can simply use it. Filter names in liquid markup are lower case.
``` csharp
Template template = Template.Parse(" {{ '*hi*' | textilize }} ");
template.Render(); // => "<b>*hi*</b>"
```
A filter can access the current context if you add a Context object as the first argument to your filter method. DotLiquid will automatically pass the current context to your filter:
``` csharp
public static String MyFilter(Context context, string input)
{
//...
}
```
Filters also work from F#:
``` csharp
open DotLiquid
type TextFilter() =
static member Textilize (input : string) =
"<b>" + input + "</b>"
Template.RegisterFilter(TextFilter().GetType());
let template = Template.Parse(" {{ '*hi*' | textilize }} ");
printfn "%s" (template.Render()) // => "<b>*hi*</b>"
```
## Create your own tags
To create a new tag, simply inherit from `DotLiquid.Tag` and register your tag with `DotLiquid.Template`.
``` csharp
public class Random : DotLiquid.Tag
{
private int _max;
public override void Initialize(string tagName, string markup, List<string> tokens)
{
base.Initialize(tagName, markup, tokens);
_max = Convert.ToInt32(markup);
}
public override void Render(Context context, TextWriter result)
{
result.Write(new Random().Next(_max).ToString());
}
}
Template.RegisterTag<Random>("random");
Template template = Template.Parse(" {% random 5 %}");
template.Render(); // => "3"
```
### Create your own tag blocks
All tag blocks are parsed by DotLiquid. To create a new block,
you just have to inherit from `DotLiquid.Block` and register your block with `DotLiquid.Template`.
``` csharp
public class Random : DotLiquid.Block
{
private int _max;
public override void Initialize(string tagName, string markup, List<string> tokens)
{
base.Initialize(tagName, markup, tokens);
_max = Convert.ToInt32(markup);
}
public override void Render(Context context, StreamWriter result)
{
if (new System.Random().Next(_max) == 0)
base.Render(context, result);
}
}
Template.RegisterTag<Random>("random");
string text = " {% random 5 %} wanna hear a joke? {% endrandom %} ";
Template template = Template.Parse(text);
template.Render(); // => In 20% of the cases, this will output "wanna hear a joke?"
```
### Adding custom operators
It is possible to add your own custom operators like this:
``` csharp
Condition.Operators["IsMultipleOf"] = (left, right) => (int)left % (int)right == 0;
```
And use it like this in ruby casing:
``` csharp
{% if 16 is_multiple_of 4 %} TRUE {% endif %}
```
Or like this in C# casing:
``` csharp
{% if 16 IsMultipleOf 4 %} TRUE {% endif %}
```
</markdown>
</div>