- expect_nothing
Liquid create your own tag without parameters
This is probably the simplest example of extending the Liquid syntax by new tags. I am not sure how usefule is this in the real world as I think the same could be done with the include tag, but this might help understanding how to create more complex tags.
- We need both liquid and liquid-core
examples/liquid/single-tag/Cargo.toml
[package] name = "parse" version = "0.1.0" edition = "2021" [dependencies] liquid = "0.26.9" liquid-core = "0.26.9"
- We need to add the struct implementing our tag (single_tag::SingleTag) to our parser using the tag method.
- Then we can use the {% single %} tag in our template.
examples/liquid/single-tag/src/main.rs
mod single_tag; fn main() { let template = liquid::ParserBuilder::with_stdlib() .tag(single_tag::SingleTag) .build() .unwrap() .parse("Liquid: {% single %}") .unwrap(); let globals = liquid::object!({}); let output = template.render(&globals).unwrap(); assert_eq!( output, "Liquid: Single replaced by this string.".to_string() ); }
examples/liquid/single-tag/src/single_tag.rs
use std::io::Write; use liquid_core::error::ResultLiquidReplaceExt; use liquid_core::Language; use liquid_core::Renderable; use liquid_core::Result; use liquid_core::Runtime; use liquid_core::{ParseTag, TagReflection, TagTokenIter}; #[derive(Copy, Clone, Debug, Default)] pub struct SingleTag; impl TagReflection for SingleTag { fn tag(&self) -> &'static str { "single" } fn description(&self) -> &'static str { "" } } impl ParseTag for SingleTag { fn parse( &self, mut arguments: TagTokenIter<'_>, _options: &Language, ) -> Result<Box<dyn Renderable>> { arguments.expect_nothing()?; Ok(Box::new(Single)) } fn reflection(&self) -> &dyn TagReflection { self } } #[derive(Debug)] struct Single; impl Renderable for Single { fn render_to(&self, writer: &mut dyn Write, _runtime: &dyn Runtime) -> Result<()> { write!(writer, "Single replaced by this string.").replace("Failed to render")?; Ok(()) } } #[cfg(test)] mod test { use super::*; use liquid_core::parser; use liquid_core::runtime; use liquid_core::runtime::RuntimeBuilder; fn options() -> Language { let mut options = Language::default(); options .tags .register("single".to_string(), SingleTag.into()); options } #[test] fn simple() { let options = options(); let template = parser::parse("{% single %}", &options) .map(runtime::Template::new) .unwrap(); let runtime = RuntimeBuilder::new().build(); let output = template.render(&runtime).unwrap(); assert_eq!(output, "Single replaced by this string."); } }