Skip to content

Commit

Permalink
Add magical variable that dumps current context
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Prouillet committed Dec 9, 2016
1 parent c84ef5d commit 1dbfe14
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/render.rs
@@ -1,5 +1,6 @@
use std::collections::{LinkedList, HashMap};

use serde_json::to_string_pretty;
use serde_json::value::{Value, to_value};

use context::{ValueRender, ValueNumber, ValueTruthy, get_json_pointer};
Expand Down Expand Up @@ -90,6 +91,13 @@ impl<'a> Renderer<'a> {
None => &self.context
};

// Magical variable that just dumps the context
if key == "__tera_context" {
return Ok(to_value(
to_string_pretty(context).expect("Couldn't serialize context for `__tera_context`")
));
}

// small helper fn to reduce duplication code in the 3 spots in `lookup_variable` where we
// need to actually do the variable lookup
fn find_variable(context: &Value, key: &str, tpl_name: &str) -> Result<Value> {
Expand Down Expand Up @@ -391,7 +399,9 @@ impl<'a> Renderer<'a> {
let active_namespace = match namespace.as_ref() {
"self" => {
// TODO: handle error if we don't have a namespace
// This can only happen when calling {{ self:: }} outside of a macro afaik
// This can (maybe) happen when calling {{ self:: }} outside of a macro
// This happens when calling a macro defined in the file itself without imports
// that means macros need to be put in another file to work, which seems ok
self.macro_namespaces
.last()
.expect("Open an issue with a template sample please (mention `self namespace macro`)!")
Expand Down
55 changes: 55 additions & 0 deletions tests/expected/magical_variable.html
@@ -0,0 +1,55 @@


{
"a_tuple": [
1,
2,
3
],
"an_array_of_tuple": [
[
1,
2,
3
],
[
1,
2,
3
]
],
"empty": [],
"friend_reviewed": true,
"number_reviews": 2,
"product": {
"manufacturer": "Motorala",
"name": "Moto G",
"price": 100,
"summary": "A phone"
},
"reviews": [
{
"paragraphs": [
"A",
"B",
"C"
],
"title": "My review"
},
{
"paragraphs": [
"A",
"B",
"C"
],
"title": "My review"
}
],
"show_more": true,
"username": "bob"
}


{
"name": "bob"
}
6 changes: 6 additions & 0 deletions tests/render_ok.rs
Expand Up @@ -138,3 +138,9 @@ fn test_ok_macros() {
vec!["tests/templates/macros.html", "tests/templates/macro_included.html"]
);
}


#[test]
fn test_magical_variable_dumps_context() {
assert_template_ok("tests/templates/magical_variable.html", vec!["tests/templates/macros.html"]);
}
4 changes: 4 additions & 0 deletions tests/templates/macros.html
Expand Up @@ -16,3 +16,7 @@
{% macro include(greeting) %}
{% include "macro_included.html" %}
{% endmacro include %}

{% macro dump(name) %}
{{ __tera_context }}
{% endmacro dump %}
6 changes: 6 additions & 0 deletions tests/templates/magical_variable.html
@@ -0,0 +1,6 @@
{% import "macros.html" as macros %}

{{ __tera_context }}


{{ macros::dump(name="bob") }}

0 comments on commit 1dbfe14

Please sign in to comment.