diff options
| author | Mike Buland <mike@xagasoft.com> | 2026-06-12 14:33:34 -0700 |
|---|---|---|
| committer | Mike Buland <mike@xagasoft.com> | 2026-06-12 14:33:34 -0700 |
| commit | 3c9a65f0a576397024c76bcc33950796ec3297ec (patch) | |
| tree | 3b6e7d10f7a5507dbe7736e443ad85bcdf78a1fe /src/lib.rs | |
| parent | 9083d5843f5a39fafacde81242b817021f9cb818 (diff) | |
| download | crimtag-3c9a65f0a576397024c76bcc33950796ec3297ec.tar.gz crimtag-3c9a65f0a576397024c76bcc33950796ec3297ec.tar.bz2 crimtag-3c9a65f0a576397024c76bcc33950796ec3297ec.tar.xz crimtag-3c9a65f0a576397024c76bcc33950796ec3297ec.zip | |
if/elif/else and loop work fully.
Complex identifiers also work now.
Diffstat (limited to 'src/lib.rs')
| -rw-r--r-- | src/lib.rs | 43 |
1 files changed, 31 insertions, 12 deletions
| @@ -171,6 +171,7 @@ impl Crimtag { | |||
| 171 | 171 | ||
| 172 | pub fn render(&self, view: &str, input: &impl MappedStructure ) -> CrimResult<String> { | 172 | pub fn render(&self, view: &str, input: &impl MappedStructure ) -> CrimResult<String> { |
| 173 | if let Some(view) = self.views.get( view ) { | 173 | if let Some(view) = self.views.get( view ) { |
| 174 | //println!("::Token tree::\n{:?}", view ); | ||
| 174 | let mut output = view.process( input )?; | 175 | let mut output = view.process( input )?; |
| 175 | if let Some(l) = &view.layout { | 176 | if let Some(l) = &view.layout { |
| 176 | self.render( l, &output ) | 177 | self.render( l, &output ) |
| @@ -203,12 +204,14 @@ fn exec(input: &impl MappedStructure, tokens: &Vec<Token>, buf: &mut String) -> | |||
| 203 | } | 204 | } |
| 204 | } | 205 | } |
| 205 | } | 206 | } |
| 206 | Token::If(ident,code) => { | 207 | Token::If(ident,code,other) => { |
| 207 | if let Some(value) = input.get_value( &ident ) && | 208 | if let Some(value) = input.get_value( &ident ) && |
| 208 | let Value::Bool(b) = value && | 209 | let Value::Bool(b) = value && |
| 209 | b { | 210 | b { |
| 210 | 211 | ||
| 211 | exec(input, code, buf)? | 212 | exec(input, code, buf)? |
| 213 | } else { | ||
| 214 | exec(input, other, buf)? | ||
| 212 | } | 215 | } |
| 213 | } | 216 | } |
| 214 | Token::Show(ident,p) => { | 217 | Token::Show(ident,p) => { |
| @@ -322,16 +325,13 @@ Now with explicit outputs: | |||
| 322 | } | 325 | } |
| 323 | 326 | ||
| 324 | #[test] | 327 | #[test] |
| 325 | fn loops() { | 328 | fn loops() -> Result<(),CrimError> { |
| 326 | let mut ct = Crimtag::new(); | 329 | let mut ct = Crimtag::new(); |
| 327 | if let Err(e) = ct.load_static(r#"Looping code: | 330 | ct.load_static(r#"Looping code: |
| 328 | [|view "index"|>We will enumerate people here:[|loop people|> | 331 | [|view "index"|>We will enumerate people here:[|loop people|> |
| 329 | - [|show last_name|], [|show first_name|][|if show_title |>: [|show title|] <|if|] [|loop tags|> [|show tag|]<|loop|] <|loop|] | 332 | - [|show last_name|], [|show first_name|][|if show_title |>: [|show title|] <|else|> **title redacted** <|if|] [|loop tags|> [|show tag|]<|loop|] <|loop|] |
| 330 | <|view|] | 333 | <|view|] |
| 331 | "#) { | 334 | "#)?; |
| 332 | println!("Error: {:?}", e ); | ||
| 333 | return; | ||
| 334 | } | ||
| 335 | 335 | ||
| 336 | let ctx = Context::from([ | 336 | let ctx = Context::from([ |
| 337 | ("people".to_string(), vec![ | 337 | ("people".to_string(), vec![ |
| @@ -361,9 +361,28 @@ Now with explicit outputs: | |||
| 361 | ].into(), | 361 | ].into(), |
| 362 | ].into()), | 362 | ].into()), |
| 363 | ]); | 363 | ]); |
| 364 | match ct.render("index", &ctx) { | 364 | |
| 365 | Ok(s) => println!("View result: {}", s ), | 365 | println!("View result: {}", ct.render("index", &ctx)? ); |
| 366 | Err(e) => println!("Error: {:?}", e ), | 366 | |
| 367 | } | 367 | Ok(()) |
| 368 | } | ||
| 369 | |||
| 370 | #[test] | ||
| 371 | fn conditional() -> Result<(),CrimError> { | ||
| 372 | let mut ct = Crimtag::new(); | ||
| 373 | ct.load_static(r#"Hi there | ||
| 374 | [|view "index"|> | ||
| 375 | Color: [|if is_red|> red <|elif is_blue |> blue <|else|> green <|if|] | ||
| 376 | <|view|]"#)?; | ||
| 377 | |||
| 378 | let ctx = Context::from([ | ||
| 379 | ("is_red".into(), false.into()), | ||
| 380 | ("is_blue".into(), false.into()), | ||
| 381 | ("color".into(), "purple".into()), | ||
| 382 | ]); | ||
| 383 | |||
| 384 | println!("View result: {}", ct.render("index", &ctx)? ); | ||
| 385 | |||
| 386 | Ok(()) | ||
| 368 | } | 387 | } |
| 369 | } | 388 | } |
