-
-
Notifications
You must be signed in to change notification settings - Fork 171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recusive variable resolving #600
Comments
If you want this behavior globally - I would probably use the same approach you mentioned. We had a similar requirement but the re-rendering was limited to a few variables. It is not recursive but is already sufficient for many use cases like the example you mentioned. public string Render<TModel>(string template, TModel model)
{
var parser = new FluidParser();
if (!parser.TryParse(template, out var compiledTemplate, out var error))
{
throw new TemplateParseException(error, template);
}
var templateOptions = new TemplateOptions
{
MemberAccessStrategy = new UnsafeMemberAccessStrategy(),
};
templateOptions.Filters.AddFilter("template", TemplateFilter);
var context = new TemplateContext(model, templateOptions);
var result = compiledTemplate.Render(context);
return result;
}
private ValueTask<FluidValue> TemplateFilter(
FluidValue input,
FilterArguments arguments,
TemplateContext context)
{
var template = input.ToStringValue();
if (!fluidParser.TryParse(template, out var compiledTemplate, out var error))
{
throw new TemplateParseException(error, template);
}
var result = compiledTemplate.Render(context);
return new ValueTask<FluidValue>(new StringValue(result));
}
public class TemplateParseException : Exception
{
public TemplateParseException(string message, string template)
: base(message)
{
Template = template;
}
public string Template { get; set; }
} Usage: var model = new { Format = "Hello, {{ Name }}", Name = "John" };
const string template = "{{ Format | template }}!";
// Returns "Hello, John!"
var result = Render(template, model); |
Hi!
Is there some easy way to recursively resolve a variable?
E. g. when template
{{ variable1 }}
with variable1="{{ variable2 }}" and variable2="value" resolves tovalue
.For now, the only way I see is doing parse-render in a cycle.
The text was updated successfully, but these errors were encountered: