From d5fbebecbd746e4dda0bcd81cb861b61afd8a309 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 10 Jan 2024 15:09:53 +0100 Subject: Add `as_ref` builtin filter --- askama_derive/src/generator.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'askama_derive') diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 0f9a2af..2040cac 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -1210,6 +1210,20 @@ impl<'a> Generator<'a> { Ok(DisplayWrap::Wrapped) } + fn _visit_as_ref_filter( + &mut self, + buf: &mut Buffer, + args: &[Expr<'_>], + ) -> Result<(), CompileError> { + let arg = match args { + [arg] => arg, + _ => return Err("unexpected argument(s) in `as_ref` filter".into()), + }; + buf.write("&"); + self.visit_expr(buf, arg)?; + Ok(()) + } + fn visit_filter( &mut self, buf: &mut Buffer, @@ -1230,6 +1244,9 @@ impl<'a> Generator<'a> { return Ok(DisplayWrap::Unwrapped); } else if name == "markdown" { return self._visit_markdown_filter(buf, args); + } else if name == "as_ref" { + self._visit_as_ref_filter(buf, args)?; + return Ok(DisplayWrap::Wrapped); } if name == "tojson" { -- cgit