Mal.OnyxTemplate
1.2.1
See the version list below for details.
dotnet add package Mal.OnyxTemplate --version 1.2.1
NuGet\Install-Package Mal.OnyxTemplate -Version 1.2.1
<PackageReference Include="Mal.OnyxTemplate" Version="1.2.1" />
paket add Mal.OnyxTemplate --version 1.2.1
#r "nuget: Mal.OnyxTemplate, 1.2.1"
// Install Mal.OnyxTemplate as a Cake Addin #addin nuget:?package=Mal.OnyxTemplate&version=1.2.1 // Install Mal.OnyxTemplate as a Cake Tool #tool nuget:?package=Mal.OnyxTemplate&version=1.2.1
Onyx Template
Onyx Template is a very simple Source Generator powered runtime text template system.
- Note: Only really tested with modern project format + modern nuget. No guarantees of
proper function in old .NET Framework projects etc.
Usage:
- Add the nuget package Mal.OnyxTemplate.
- Add a file with an
.onyx
extension.
(It should be added automatically as AdditionalFiles. If not - change it to that).
The macros are fairly simple.
{{ YourMacroName }}
Let's create our first .onyx file. Let's call it WelcomeText.onyx
.
Hello, {{ UserName }}, welcome to the show.
The source generator will have generated a class for you. Every macro you make in your .onyx file will get its own property string (or boolean) you can set for that macro.
... But I need it to be public / $template
There is a special configuration macro you can add to the very top of your .onyx file
to configure it. Let's mke our WelcomeText macro generate a public
base class
instead of the default internal
.
{{ $template public }}
Hello, {{ UserName }}, welcome to the show.
That's all it takes.
Repeating data / $foreach
To generate lists of data you need to utilize the special $foreach
macro:
Animals:
{{ $foreach animal in animals }}
{{ animal }}
{{ $next }}
Providing we give it the following data provider:
var template = new Template();
template.Animals = new[] {
"Dog",
"Cat",
"Tiger",
"Bear"
}
The result will be
Animals:
Dog
Cat
Tiger
Bear
See also item state conditionals
Dealing with multiline macros
But what if the animal
macro returned multiple lines? Let's try it:
var template = new Template();
template.Animals = new[] {
"Dog\n- Canidae",
"Cat\n- Felidae",
"Tiger\n- Felidae",
"Bear\n- Ursidae"
}
Result:
Animals:
Dog
- Canidae
Cat
- Felidae
Tiger
- Felidae
Bear
- Ursidae
So yes, that worked, but... it's not exactly what we're after, is it. We'd prefer it if the new lines were aligned with the first macro.
There's two ways we can solve this problem. We can set an indented
flag
in the template header
{{ $template indented }}
or we can tell the generator that only this particular use of the macro needs to be indented:
{{ animal:indent }}
Result:
Animals:
Dog
- Canidae
Cat
- Felidae
Tiger
- Felidae
Bear
- Ursidae
That's better!
Complex macros
We could make it even better though. Rather than formatting the whole thing in the data provider itself we could do this:
Animals:
{{ $foreach animal in animals }}
Animal Name: {{ name }}
Animal Family: {{ type }}
{{ $next }}
Now this is detected to be a complex macro. This means we'll have to change our data provider again:
var template = new Template();
template.Animals = new[]
{
new Template.Animal { Name = "Dog", Type = "Canidae" },
new Template.Animal { Name = "Cat", Type = "Felidae" },
new Template.Animal { Name = "Tiger", Type = "Felidae" },
new Template.Animal { Name = "Bear", Type = "Ursidae" }
}
The generator has produced an item class we can use to produce data for the complex macro.
Now, our results are:
Animals:
Animal Name: Dog
Animal Family: Canidae
Animal Name: Cat
Animal Family: Felidae
Animal Name: Tiger
Animal Family: Felidae
Animal Name: Bear
Animal Family: Ursidae
Conditionals
Sometimes you want templates that may or may not generate parts of itself
based on a condition. We can do that by using the {{ $if }}
, {{ $elseif }}
,
{{ $else }}
macros.
Mr. Jenkins, you're {{ $if isfired }}fired!{{ $else }}hired.{{ $end }}
You can invert a condition by including the not
modifier
Mr. Jenkins, you're {{ $if not isfired }}hired.{{ $else }}fired!{{ $end }}
Item state conditionals
You can query a set of states about items in a $foreach
by using a specialized set of fields:
$first
The item is the first item of the list.$last
The item is the last item of the list.$middle
The item is neither the first nor the last item in the list.$odd
The item is of odd numbered index.$even
The item is of even numbered index.
{{ $foreach item in list }}
item.name{{ $if not $last }},{{ $end }}
{{ $next}}
And that's it
This library was primarily designed 1. to help me get better at making source generators and 2. because I needed something simple to make my other source generators cleaner, and without all the StringBuilder shenanigans all over the place.
Known Issue with Jetbrains Rider
There seems to be some kind of caching going on with Rider which prevents it from detecting changes in the .onyx files now and again. I have yet to find a solution for it. A Rebuild All forces the issue.
Watch your whitespace
If you have whitespace before or after the {{ $template }}
,
{{ $foreach item in source }}
and {{ $next }}
macros, you might get
newlines you don't want.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated | |
---|---|---|---|
1.3.6-alpha | 629 | 8/5/2024 | |
1.3.5-alpha | 65 | 8/5/2024 | |
1.3.4-alpha | 72 | 8/5/2024 | |
1.3.3-alpha | 73 | 8/4/2024 | |
1.3.1-alpha | 78 | 8/4/2024 | |
1.3.0-alpha | 68 | 7/31/2024 | |
1.2.1 | 4,013 | 11/19/2023 | |
1.2.1-alpha | 1,108 | 10/10/2023 | |
1.2.0-alpha | 100 | 10/10/2023 | |
1.1.7 | 145 | 11/5/2023 | |
1.1.7-alpha | 111 | 10/9/2023 | |
1.1.6-alpha | 100 | 10/9/2023 | |
1.1.5-alpha | 110 | 10/9/2023 | |
1.1.3-alpha | 114 | 10/9/2023 | |
1.1.2-alpha | 99 | 10/9/2023 | |
1.1.1-alpha | 122 | 9/17/2023 | |
1.1.0-alpha | 102 | 9/17/2023 | |
1.0.0-alpha | 113 | 9/17/2023 |
v.1.2.1-alpha
- Missing a using...
v.1.2.0-alpha
- BREAKING CHANGE: Completely changed the way the template is generated and used code wise, to simplify even further.
- Should now support environments with nullable support a little better.
v.1.1.7-alpha
- Learned how to include release notes!
v.1.1.6-alpha
- Added "not" support to conditionals
- Now supports item states {{ $if <state> }} where <state> can be $first $last $middle $odd $even
- Fixed bad end check, fixed bad location of Else macro type check
- Fixed a rather ugly infinite loop