Skip to content

Commit

Permalink
Add support for calling debug() with args
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Oct 8, 2023
1 parent af3dc50 commit d5cb1ab
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
18 changes: 13 additions & 5 deletions minijinja/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ mod builtins {
use std::collections::BTreeMap;

use crate::error::ErrorKind;
use crate::value::{MapType, ValueRepr};
use crate::value::{MapType, Rest, ValueRepr};

/// Returns a range.
///
Expand Down Expand Up @@ -304,19 +304,27 @@ mod builtins {
}
}

/// Outputs the current context stringified.
/// Outputs the current context or the arguments stringified.
///
/// This is a useful function to quickly figure out the state of affairs
/// in a template. It emits a stringified debug dump of the current
/// engine state including the layers of the context, the current block
/// and auto escaping setting.
/// and auto escaping setting. The exact output is not defined and might
/// change from one version of Jinja2 to the next.
///
/// ```jinja
/// <pre>{{ debug() }}</pre>
/// <pre>{{ debug(variable1, variable2) }}</pre>
/// ```
#[cfg_attr(docsrs, doc(cfg(feature = "builtins")))]
pub fn debug(state: &State) -> String {
format!("{state:#?}")
pub fn debug(state: &State, args: Rest<Value>) -> String {
if args.is_empty() {
format!("{state:#?}")
} else if args.len() == 1 {
format!("{:#?}", args.0[0])
} else {
format!("{:#?}", &args.0[..])
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions minijinja/tests/inputs/debug.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@
}
---
{% with f = range %}{% for x in f(upper) %}{{ debug() }}{% endfor %}{% endwith %}
---
{{ debug(none) }}
---
{{ debug(true, false, 42) }}
---
{{ debug([debug, 42]) }}
15 changes: 14 additions & 1 deletion minijinja/tests/snapshots/test_templates__vm@debug.txt.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: minijinja/tests/test_templates.rs
description: "{% with f = range %}{% for x in f(upper) %}{{ debug() }}{% endfor %}{% endwith %}"
description: "{% with f = range %}{% for x in f(upper) %}{{ debug() }}{% endfor %}{% endwith %}\n---\n{{ debug(none) }}\n---\n{{ debug(true, false, 42) }}\n---\n{{ debug([debug, 42]) }}"
info:
upper: 1
input_file: minijinja/tests/inputs/debug.txt
Expand Down Expand Up @@ -128,4 +128,17 @@ State {
],
},
}
---
none
---
[
true,
false,
42,
]
---
[
minijinja::functions::builtins::debug,
42,
]

0 comments on commit d5cb1ab

Please sign in to comment.