From 3c9a65f0a576397024c76bcc33950796ec3297ec Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 12 Jun 2026 14:33:34 -0700 Subject: if/elif/else and loop work fully. Complex identifiers also work now. --- src/lib.rs | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 2b53828..84a5a43 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -171,6 +171,7 @@ impl Crimtag { pub fn render(&self, view: &str, input: &impl MappedStructure ) -> CrimResult { if let Some(view) = self.views.get( view ) { + //println!("::Token tree::\n{:?}", view ); let mut output = view.process( input )?; if let Some(l) = &view.layout { self.render( l, &output ) @@ -203,12 +204,14 @@ fn exec(input: &impl MappedStructure, tokens: &Vec, buf: &mut String) -> } } } - Token::If(ident,code) => { + Token::If(ident,code,other) => { if let Some(value) = input.get_value( &ident ) && let Value::Bool(b) = value && b { exec(input, code, buf)? + } else { + exec(input, other, buf)? } } Token::Show(ident,p) => { @@ -322,16 +325,13 @@ Now with explicit outputs: } #[test] - fn loops() { + fn loops() -> Result<(),CrimError> { let mut ct = Crimtag::new(); - if let Err(e) = ct.load_static(r#"Looping code: + ct.load_static(r#"Looping code: [|view "index"|>We will enumerate people here:[|loop people|> - - [|show last_name|], [|show first_name|][|if show_title |>: [|show title|] <|if|] [|loop tags|> [|show tag|]<|loop|] <|loop|] + - [|show last_name|], [|show first_name|][|if show_title |>: [|show title|] <|else|> **title redacted** <|if|] [|loop tags|> [|show tag|]<|loop|] <|loop|] <|view|] -"#) { - println!("Error: {:?}", e ); - return; - } +"#)?; let ctx = Context::from([ ("people".to_string(), vec![ @@ -361,9 +361,28 @@ Now with explicit outputs: ].into(), ].into()), ]); - match ct.render("index", &ctx) { - Ok(s) => println!("View result: {}", s ), - Err(e) => println!("Error: {:?}", e ), - } + + println!("View result: {}", ct.render("index", &ctx)? ); + + Ok(()) + } + + #[test] + fn conditional() -> Result<(),CrimError> { + let mut ct = Crimtag::new(); + ct.load_static(r#"Hi there +[|view "index"|> + Color: [|if is_red|> red <|elif is_blue |> blue <|else|> green <|if|] +<|view|]"#)?; + + let ctx = Context::from([ + ("is_red".into(), false.into()), + ("is_blue".into(), false.into()), + ("color".into(), "purple".into()), + ]); + + println!("View result: {}", ct.render("index", &ctx)? ); + + Ok(()) } } -- cgit v1.2.3