Skip to content
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

Stream objects as is during template evaluation instead of stringbuilder and ToString() #562

Open
juhani-honkala opened this issue Aug 31, 2024 · 4 comments
Labels

Comments

@juhani-honkala
Copy link

Is it possible in Scriban to get a stream of objects during template evaluation instead of automatic conversion to a string through the StringBuilder? I would like to receive a stream of strings and other values without string conversion rather than having them aggregated into a single string.

thanks!

@chadcampling
Copy link

Not sure if this is what you're after but you can call PushOutput on your TemplateContext to redirect the output to your own implementation of IScriptOutput using a TextWriter or anything really. Another area to investigate on the template context there is to override the Write method which also may provide you with some hooks to achieve what your looking for.

@juhani-honkala
Copy link
Author

Thanks for the reply. I tried checking it, but for me it looks like the string conversion is done before passing outout to Output. I might have mistaken, but I couldn’t figure out how to do that without conversion to string. There seems to be some assumptions that the output is string. Write could be something to investigate.

@xoofx
Copy link
Member

xoofx commented Sep 6, 2024

You have to derive from TemplateContext and overrides the methods that fits what you are looking for. Evaluation and conversion to string is operated from there e.g:

public virtual TemplateContext Write(SourceSpan span, object textAsObject)
{
if (textAsObject != null)
{
var text = ObjectToString(textAsObject);
Write(text);
}
return this;
}

This is for example what was done in the kalk project that is using Scriban under the hood:

https://github.com/xoofx/kalk/blob/dfb1939689ed9db73f202ac43c63babff6623368/src/Kalk.Core/KalkEngine.cs#L347-L358

@xoofx xoofx added the question label Sep 6, 2024
@juhani-honkala
Copy link
Author

Thanks a lot, I'll try that one and report back!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants