Proposal: Razor-like templating syntax inside raw string literals #9586
Replies: 3 comments 5 replies
-
That should be implemented using Source Generator. I have implemented something similar to that with scriban: ScribanSourceGenerator. |
Beta Was this translation helpful? Give feedback.
-
Is your proposal any different from #7532? Your request is also similar to #8756. |
Beta Was this translation helpful? Give feedback.
-
What's so clunky about the first example? I have written a source generator myself and while it may be confusing at first that one has to produce strings instead of Roslyn AST. It's certainly the most flexible approach. What's so bad about multi-line/verbatim interpolated strings? If you need conditions, you can use conditional expressions inside the interpolations. If it's more complicated logic, you can use a local function. I think c# has enough flavors of string literals already:
Why add another one? Like I said, if you need conditions, you can use conditional expressions inside the interpolations. If it's more complicated logic, you can use a local function. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Introduce a new string literal form that supports embedded C# expressions and simple control flow (e.g.,
@variable
,@foreach
,@if
) in a Razor-like syntax, primarily for use in source generation and templating scenarios.Motivation
Currently, raw string literals and interpolated strings are awkward to use for generating structured content like HTML, code, or configuration files when logic is involved.
foreach
orif
.{{ }}
) in nested templating scenarios is painful.This proposal aims to bring templating support, using a Razor-like syntax, directly into the C# language for improved ergonomics in scenarios like source generators, code emitters, etc.
Examples
HTML Email Template
Current (clunky):
Proposed (templated block):
C# Source Generator Emitting Code
Current (clunky):
Proposed (templated block):
Benefits
I’m curious if this is something that could be supported by the language.
Beta Was this translation helpful? Give feedback.
All reactions