templates Rust¶
Template loading with user override support
This module provides template loading functionality that supports:
- Bundled default templates (embedded at compile time)
- User overrides from .plissken/templates/ directory
User templates take precedence over bundled defaults on a per-file basis.
Structs¶
struct TemplateLoader¶
pub
Template loader with user override support.
The loader first checks for user-provided templates in the configured override directory, then falls back to bundled defaults.
Examples:
use plissken_core::render::TemplateLoader;
use std::path::Path;
// Create loader without user overrides (uses bundled only)
let loader = TemplateLoader::new(None);
// Create loader with project root for user overrides
let loader = TemplateLoader::new(Some(Path::new("/path/to/project")));
// Get a template
let badge_template = loader.get("partials/badge.html").unwrap();
Fields¶
| Name | Type | Description |
|---|---|---|
bundled |
HashMap < & 'static str , & 'static str > |
Bundled templates (name -> content) |
user_dir |
Option < PathBuf > |
Optional user override directory |
Methods¶
new pub¶
Create a new template loader.
Parameters:
| Name | Type | Description |
|---|---|---|
project_root |
- |
Optional path to project root. If provided, user templates will be loaded from {project_root}/.plissken/templates/. |
Source
get pub¶
Get a template by name.
First checks for a user override, then falls back to the bundled default.
Parameters:
| Name | Type | Description |
|---|---|---|
name |
- |
Template name (e.g., "partials/badge.html", "module.html") |
Returns:
The template content as a string, or an error if not found.
Source
pub fn get(&self, name: &str) -> crate::error::Result<String> {
// Check user override first
if let Some(ref dir) = self.user_dir {
let user_path = dir.join(name);
if user_path.exists() {
return std::fs::read_to_string(&user_path)
.map_err(|e| crate::error::PlisskenError::file_read(&user_path, e));
}
}
// Fall back to bundled
self.bundled
.get(name)
.map(|s| s.to_string())
.ok_or_else(|| crate::error::PlisskenError::Template {
message: format!("template not found: {}", name),
source: tera::Error::msg(format!("template '{}' not found in bundled templates", name)),
})
}
has_user_override pub¶
Check if a user override exists for the given template.
Source
template_names pub¶
List all available template names.
user_override_dir pub¶
Get the user override directory, if configured and exists.
load_bundled private¶
Source
fn load_bundled() -> HashMap<&'static str, &'static str> {
let mut map = HashMap::new();
map.insert("partials/badge.html", bundled::BADGE);
map.insert("partials/code_block.html", bundled::CODE_BLOCK);
map.insert("partials/signature.html", bundled::SIGNATURE);
map.insert("module.html", bundled::MODULE);
map
}