@@ -22,6 +22,9 @@ use tera::{
2222/// Wrapper for [`Tera`].
2323#[ derive( Debug ) ]
2424pub struct Template {
25+ /// Template name.
26+ name : String ,
27+ /// Internal Tera instance.
2528 tera : Tera ,
2629 /// Template variables.
2730 #[ cfg_attr( not( feature = "github" ) , allow( dead_code) ) ]
@@ -30,16 +33,16 @@ pub struct Template {
3033
3134impl Template {
3235 /// Constructs a new instance.
33- pub fn new ( mut template : String , trim : bool ) -> Result < Self > {
36+ pub fn new ( name : & str , mut content : String , trim : bool ) -> Result < Self > {
3437 if trim {
35- template = template
38+ content = content
3639 . lines ( )
3740 . map ( |v| v. trim ( ) )
3841 . collect :: < Vec < & str > > ( )
3942 . join ( "\n " ) ;
4043 }
4144 let mut tera = Tera :: default ( ) ;
42- if let Err ( e) = tera. add_raw_template ( "template" , & template ) {
45+ if let Err ( e) = tera. add_raw_template ( & name , & content ) {
4346 return if let Some ( error_source) = e. source ( ) {
4447 Err ( Error :: TemplateParseError ( error_source. to_string ( ) ) )
4548 } else {
@@ -48,7 +51,8 @@ impl Template {
4851 }
4952 tera. register_filter ( "upper_first" , Self :: upper_first_filter) ;
5053 Ok ( Self {
51- variables : Self :: get_template_variables ( & tera) ?,
54+ name : name. to_string ( ) ,
55+ variables : Self :: get_template_variables ( name, & tera) ?,
5256 tera,
5357 } )
5458 }
@@ -129,13 +133,13 @@ impl Template {
129133 }
130134
131135 /// Returns the variable names that are used in the template.
132- fn get_template_variables ( tera : & Tera ) -> Result < Vec < String > > {
136+ fn get_template_variables ( name : & str , tera : & Tera ) -> Result < Vec < String > > {
133137 let mut variables = HashSet :: new ( ) ;
134- let ast = & tera. get_template ( "template" ) ?. ast ;
138+ let ast = & tera. get_template ( name ) ?. ast ;
135139 for node in ast {
136140 Self :: find_identifiers ( node, & mut variables) ;
137141 }
138- trace ! ( "Template variables: {variables:?}" ) ;
142+ trace ! ( "Template variables for {name} : {variables:?}" ) ;
139143 Ok ( variables. into_iter ( ) . collect ( ) )
140144 }
141145
@@ -159,7 +163,7 @@ impl Template {
159163 context. insert ( key. clone ( ) , & value) ;
160164 }
161165 }
162- match self . tera . render ( "template" , & context) {
166+ match self . tera . render ( & self . name , & context) {
163167 Ok ( mut v) => {
164168 for postprocessor in postprocessors {
165169 postprocessor. replace ( & mut v, vec ! [ ] ) ?;
@@ -242,7 +246,7 @@ mod test {
242246 ### {{ commit.group }}
243247 - {{ commit.message | upper_first }}
244248 {% endfor %}"# ;
245- let mut template = Template :: new ( template. to_string ( ) , false ) ?;
249+ let mut template = Template :: new ( "test" , template. to_string ( ) , false ) ?;
246250 let release = get_fake_release_data ( ) ;
247251 assert_eq ! (
248252 "\n \t \t ## 1.0 - 2023\n \t \t \n \t \t ### feat\n \t \t - Add xyz\n \t \t \n \t \t ### \
@@ -281,7 +285,7 @@ mod test {
281285 let template = r#"
282286 ## {{ version }}
283287 "# ;
284- let template = Template :: new ( template. to_string ( ) , true ) ?;
288+ let template = Template :: new ( "test" , template. to_string ( ) , true ) ?;
285289 let release = get_fake_release_data ( ) ;
286290 assert_eq ! (
287291 "\n ## 1.0\n " ,
@@ -300,7 +304,7 @@ mod test {
300304 let template =
301305 "{% set hello_variable = 'hello' %}{{ hello_variable | upper_first }}" ;
302306 let release = get_fake_release_data ( ) ;
303- let template = Template :: new ( template. to_string ( ) , true ) ?;
307+ let template = Template :: new ( "test" , template. to_string ( ) , true ) ?;
304308 let r = template. render (
305309 & release,
306310 Option :: < HashMap < & str , String > > :: None . as_ref ( ) ,
0 commit comments