@@ -609,20 +609,24 @@ It's possible to interpolate special symbols like `IF/ELSE/ENDIF` or `IIF` to ad
609
609
** IF-ENDIF statements**
610
610
611
611
``` cs
612
- using CodegenCS ; // besides this...
613
- using static CodegenCS .Symbols ; // you also need this
614
-
615
- void RenderMyApiClient (bool injectHttpClient )
612
+ class MyTemplate
616
613
{
617
- w .WriteLine ($$"""
618
- public class MyApiClient
619
- {
620
- public MyApiClient({{ IF (injectHttpClient ) }}HttpClient httpClient{{ ENDIF }})
621
- { {{ IF(injectHttpClient) }}
622
- _httpClient = httpClient; {{ ENDIF }}
623
- }
624
- }
625
- """ );
614
+ void Main (ICodegenOutputFile writer )
615
+ {
616
+ RenderMyApiClient (writer , true );
617
+ }
618
+ void RenderMyApiClient (ICodegenOutputFile w , bool injectHttpClient )
619
+ {
620
+ w .WriteLine ($$"""
621
+ public class MyApiClient
622
+ {
623
+ public MyApiClient({{ IF (injectHttpClient ) }}HttpClient httpClient{{ ENDIF }})
624
+ { {{ IF(injectHttpClient) }}
625
+ _httpClient = httpClient; {{ ENDIF }}
626
+ }
627
+ }
628
+ """ );
629
+ }
626
630
}
627
631
```
628
632
@@ -641,48 +645,83 @@ public class MyApiClient
641
645
** IF-ELSE-ENDIF**
642
646
643
647
``` cs
644
- w .WriteLine ($$"""
645
- public class MyApiClient
646
- {
647
- public void InvokeApi()
648
+ class MyTemplate
649
+ {
650
+ void Main (ICodegenOutputFile writer )
651
+ {
652
+ var settings = new MySettings () { SwallowExceptions = true };
653
+ RenderMyApiClient (writer , settings );
654
+ }
655
+ class MySettings
656
+ {
657
+ public bool SwallowExceptions ;
658
+ }
659
+ void RenderMyApiClient (ICodegenOutputFile w , MySettings settings )
660
+ {
661
+ w .WriteLine ($$"""
662
+ public class MyApiClient
648
663
{
649
- try
664
+ public void InvokeApi()
650
665
{
651
- restApi.Invoke();
652
- }
653
- catch (Exception ex)
654
- { {{IF(settings.swallowExceptions) }}
655
- Log.Error(ex); {{ ELSE }}
656
- throw; {{ ENDIF }}
666
+ try
667
+ {
668
+ restApi.Invoke();
669
+ }
670
+ catch (Exception ex)
671
+ { {{IF(settings.SwallowExceptions) }}
672
+ Log.Error(ex); {{ ELSE }}
673
+ throw; {{ ENDIF }}
674
+ }
657
675
}
658
676
}
659
- }
660
- """ );
677
+ """ );
678
+ }
679
+ }
661
680
```
662
681
663
682
** Nested IF statements**
664
683
``` cs
665
- w .WriteLine ($$"""
666
- {{ IF (generateConstructor ) }}public class MyApiClient
667
- {
668
- public MyApiClient({{ IF (injectHttpClient ) }}HttpClient httpClient{{ ENDIF }})
669
- { {{IF(injectHttpClient) }}
670
- _httpClient = httpClient; {{ ENDIF }}
671
- }}
672
- } {{ ENDIF }}
673
- """ );
684
+ class MyTemplate
685
+ {
686
+ void Main (ICodegenOutputFile writer )
687
+ {
688
+ RenderMyApiClient (writer , true , true );
689
+ }
690
+ void RenderMyApiClient (ICodegenOutputFile w , bool generateConstructor , bool injectHttpClient )
691
+ {
692
+ w .WriteLine ($$"""
693
+ {{ IF (generateConstructor ) }}public class MyApiClient
694
+ {
695
+ public MyApiClient({{ IF (injectHttpClient ) }}HttpClient httpClient{{ ENDIF }})
696
+ { {{IF(injectHttpClient) }}
697
+ _httpClient = httpClient; {{ ENDIF }}
698
+ }}
699
+ } {{ ENDIF }}
700
+ """ );
701
+ }
702
+ }
674
703
```
675
704
676
705
** IIF (Immediate IF):**
677
706
678
707
``` cs
679
- w .WriteLine ($$"""
680
- public class User
681
- {
682
- {{ IIF (isVisibilityPublic , $" public " ) }}string FirstName { get; set; }
683
- {{ IIF (isVisibilityPublic , $" public " , $" protected " ) }}string FirstName { get; set; }
684
- }
685
- """ );
708
+ class MyTemplate
709
+ {
710
+ void Main (ICodegenOutputFile writer )
711
+ {
712
+ RenderMyApiClient (writer , true );
713
+ }
714
+ void RenderMyApiClient (ICodegenOutputFile w , bool isVisibilityPublic )
715
+ {
716
+ w .WriteLine ($$"""
717
+ public class User
718
+ {
719
+ {{ IIF (isVisibilityPublic , $" public " ) }}string FirstName { get; set; }
720
+ {{ IIF (isVisibilityPublic , $" public " , $" protected " ) }}string FirstName { get; set; }
721
+ }
722
+ """ );
723
+ }
724
+ }
686
725
```
687
726
688
727
@@ -998,7 +1037,21 @@ Generated 1 file: 'C:\Users\drizin\MyTemplate.generated.cs'
998
1037
Successfully executed template ' MyTemplate.cs' .
999
1038
` ` `
1000
1039
1040
+ # # Async Support
1001
1041
1042
+ Templates can also be ` async Task` , ` async Task< FormattableString> ` or ` async Task< int> ` :
1043
+
1044
+ ` ` ` cs
1045
+ class MyTemplate
1046
+ {
1047
+ //Task< FormattableString> Main () => Task.FromResult(( FormattableString)$"My first template");
1048
+ async Task<FormattableString> Main(ILogger logger)
1049
+ {
1050
+ await logger.WriteLineAsync($"Generating MyTemplate...");
1051
+ return $"My first template";
1052
+ }
1053
+ }
1054
+ ```
1002
1055
1003
1056
# Learn More
1004
1057
0 commit comments