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

Expose the fact that templates implement Display #654

Merged
merged 2 commits into from
Mar 31, 2022

Conversation

Kijewski
Copy link
Collaborator

Rebased on #653.

This PR consists of two parts:

  • Expose the fact that templates implement Display

    This is a quite useful feature, because you can use templates in format!(), format_args!(), etc.

  • Add io::writer helper methods

    It might not be immediately obvious to everyone how easy it is to use Askama template with std::io (e.g. files) instead of std::fmt, so this PR adds a few helper methods to make this more obvious to novice users.

(Nb. the "novice user" is me in this case :) )

/// Renders the template to the given `writer` buffer
/// Helper method which allocates a new `Vec<u8>` and renders into it
#[inline]
fn render_bytes(&self) -> Result<Vec<u8>> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't meet my cost/benefit bar, conversions with String::into_bytes() should be easy.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

fn render_into(&self, writer: &mut (impl std::fmt::Write + ?Sized)) -> Result<()>;

/// Renders the template to the given `writer` io buffer
#[inline]
fn render_into_io(&self, writer: &mut (impl std::io::Write + ?Sized)) -> std::io::Result<()> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one seems reasonable, though the name seems a little unwieldy. Maybe write_into()? That's technically somewhat ambitious between impl std::fmt::Write and impl std::io::Write but that doesn't seem like a big deal.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed.

This is a quite useful feature, because you can use templates in
format!(), format_args!(), etc.
It might not be immediately obvious to everyone how easy it is to use
Askama template with std::io (e.g. files) instead of std::fmt, so this
PR adds a few helper methods to make this more obvious to novice users.
@djc djc merged commit 8ee5eee into djc:main Mar 31, 2022
@Kijewski Kijewski deleted the pr-write-into-any branch March 31, 2022 19:14
@djc djc mentioned this pull request Feb 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants