Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 2.2.0 (unreleased)

- Do not reserialize existing Value when serializing to a Value
- Allow trimming start/end of component definition with metadata `{"trim": true}`

## 2.1.1 (2026-08-11)

Expand Down
37 changes: 37 additions & 0 deletions tera/src/parsing/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,43 @@ impl ComponentDefinition {
self.kwargs.keys().map(|k| k.as_str()).collect()
}

/// Trim body unless the `trim` metadata field is set to `false` or unset
pub fn maybe_trim_body(&mut self) {
let needs_trimming = self
.metadata
.get("trim")
.and_then(|v| v.as_bool())
.unwrap_or(false);
if !needs_trimming {
return;
}

// We might have multiple Node::Content at start/end if there were comment nodes that got dropped
while let Some(Node::Content(c)) = self.body.first_mut() {
let t = c.trim_start();
if t.is_empty() {
self.body.remove(0);
} else {
if t.len() != c.len() {
*c = t.to_string();
}
break;
}
}

while let Some(Node::Content(c)) = self.body.last_mut() {
let t = c.trim_end();
if t.is_empty() {
self.body.pop();
} else {
if t.len() != c.len() {
*c = t.to_string();
}
break;
}
}
}

/// Builds a validated context from provided kwargs, checking types and applying defaults.
/// If rest_param_name is defined, unknown kwargs are collected into it.
/// Otherwise, unknown kwargs will error.
Expand Down
2 changes: 2 additions & 0 deletions tera/src/parsing/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,8 @@ impl<'a> Parser<'a> {
self.body_contexts.pop();
component_def.body = body;

component_def.maybe_trim_body();

Ok(component_def)
}

Expand Down
2 changes: 2 additions & 0 deletions tera/src/snapshot_tests/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ fn parser_errors() {
fn parser_components_definition_success() {
insta::glob!("parser_inputs/success/components/def/*.txt", |path| {
let contents = std::fs::read_to_string(path).unwrap();
let contents = normalize_line_endings(&contents);
let components = &Parser::new("", &contents, Delimiters::default())
.parse()
.unwrap()
Expand All @@ -68,6 +69,7 @@ fn parser_components_definition_success() {
fn parser_components_render_success() {
insta::glob!("parser_inputs/success/components/*.txt", |path| {
let contents = std::fs::read_to_string(path).unwrap();
let contents = normalize_line_endings(&contents);
println!("{path:?}");
let nodes = &Parser::new("", &contents, Delimiters::default())
.parse()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% component greet(label) %}
{{label}}
{% endcomponent %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% component greet(label) %}
{# this has ws after #}
{{label}}
{% endcomponent %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% component greet(label) {"trim": true} %}
{{label}}
{% endcomponent %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
$$ components
{% component coverlink(dest) {"trim": true} %}
<a href="{{ dest }}">{{ dest }}</a>
{% endcomponent coverlink %}
$$ tpl
<div>
{{<coverlink dest="a"/>}}
{{<coverlink dest="b"/>}}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
$$ components
{% component note() {"trim": true} %}
<aside>{{ body }}</aside>
{% endcomponent note %}
$$ tpl
{% <note> %}
hello
{% </note> %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: tera/src/snapshot_tests/parser.rs
expression: "components[0]"
input_file: tera/src/snapshot_tests/parser_inputs/success/components/def/trim_default.txt
---
ComponentDefinition {
name: "greet",
kwargs: {
"label": ComponentArgument {
default: None,
typ: None,
},
},
rest_param_name: None,
metadata: {},
body: [
"\n",
Var {
name: "label",
} @ 2:2-2:7 (31..36),
"\n",
],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
source: tera/src/snapshot_tests/parser.rs
expression: "components[0]"
input_file: tera/src/snapshot_tests/parser_inputs/success/components/def/trim_default_with_comment.txt
---
ComponentDefinition {
name: "greet",
kwargs: {
"label": ComponentArgument {
default: None,
typ: None,
},
},
rest_param_name: None,
metadata: {},
body: [
"\n",
"\n",
Var {
name: "label",
} @ 3:2-3:7 (55..60),
"\n",
],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: tera/src/snapshot_tests/parser.rs
expression: "components[0]"
input_file: tera/src/snapshot_tests/parser_inputs/success/components/def/trim_optin.txt
---
ComponentDefinition {
name: "greet",
kwargs: {
"label": ComponentArgument {
default: None,
typ: None,
},
},
rest_param_name: None,
metadata: {
"trim": Bool(true),
},
body: [
Var {
name: "label",
} @ 2:2-2:7 (46..51),
],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: tera/src/snapshot_tests/parser.rs
expression: "components[0]"
input_file: tera/src/snapshot_tests/parser_inputs/success/components/def/trim_optout.txt
---
ComponentDefinition {
name: "greet",
kwargs: {
"label": ComponentArgument {
default: None,
typ: None,
},
},
rest_param_name: None,
metadata: {
"trim": Bool(false),
},
body: [
"\n",
Var {
name: "label",
} @ 2:2-2:7 (47..52),
"\n",
],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: tera/src/snapshot_tests/rendering.rs
expression: "&out"
input_file: tera/src/snapshot_tests/rendering_inputs/success/components/trim_body_def.txt
---
<div>
<a href="a">a</a>
<a href="b">b</a>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: tera/src/snapshot_tests/rendering.rs
expression: "&out"
input_file: tera/src/snapshot_tests/rendering_inputs/success/components/trim_def_doesnt_touch_body.txt
---
<aside>
hello
</aside>
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ expression: out
<a href="https://docs.rs/tera" class="nav-link">API Docs</a>
</nav>



<ul>
<ul>

<li>1. Hello world</li>

Expand Down Expand Up @@ -67,7 +65,6 @@ expression: out

</ul>




</div>
Expand Down