| Crates.io | templify |
| lib.rs | templify |
| version | 0.1.0 |
| created_at | 2025-01-15 14:37:35.603095+00 |
| updated_at | 2025-01-15 14:37:35.603095+00 |
| description | File generation helper using a data dictionary and Jinja2 template files. |
| homepage | |
| repository | https://github.com/robinbreast/templify |
| max_upload_size | |
| id | 1517585 |
| size | 49,466 |
File generation helper using a data dictionary and Jinja2 template files.
*.j2)*.inj)Rendered or updated files based on the provided templates and data dictionary.
*.j2 files in the given template folder.MANUAL SECTION START and MANUAL SECTION END.*.inj templates with regex patterns to inject content into specific parts of the output files.use std::collections::HashMap;
use std::path::Path;
use templify::RenderHelper;
use env_logger;
use std::env;
use log::{info, error};
fn main() {
// Initialize the logger with the desired logging level
env::set_var("RUST_LOG", "info");
env_logger::init();
// Example data to be used in the template
let mut data = HashMap::new();
data.insert("name", "World");
// Create a RenderHelper instance
let render_helper = match RenderHelper::new(data, Some("context")) {
Ok(helper) => helper,
Err(e) => {
error!("Failed to create RenderHelper: {}", e);
return;
}
};
// Define paths for template and output files
let template_path = Path::new("examples/templates/simple/template.j2");
let output_folder = Path::new("output/simple");
// Generate the output file from the template file
match render_helper.generate(template_path, output_folder) {
Ok(_) => info!("Files generated successfully in: {:?}", output_folder),
Err(e) => error!("{}", e),
}
}
template.j2)Hello, {{ context.name }}!
This is a test template.
Hello, World!
This is a test template.
To preserve specific sections in the output files, use the following markers in your templates:
// MANUAL SECTION START: init-custom-var
// This content will be preserved.
custom_var = 1;
// MANUAL SECTION END
To inject content into specific parts of the output files, use the following format in your templates:
<!-- injection-pattern: test-pattern -->
^(?P<injection>.*)$
<!-- injection-string-start -->
Injected Content
<!-- injection-string-end -->