pub struct TemplateEngine { /* private fields */ }Expand description
Wraps MiniJinja and provides site-generation-specific rendering.
Implementations§
Source§impl TemplateEngine
impl TemplateEngine
Sourcepub fn init(config: TemplateConfig) -> Result<Option<Self>>
pub fn init(config: TemplateConfig) -> Result<Option<Self>>
Initializes the template engine from a template directory.
Uses a path-based loader for lazy template resolution.
Returns Ok(None) if the template directory does not exist
(graceful fallback for projects without templates).
§Examples
use ssg::template_engine::{TemplateConfig, TemplateEngine};
use tempfile::tempdir;
let dir = tempdir().unwrap();
let cfg = TemplateConfig {
template_dir: dir.path().join("missing"),
..TemplateConfig::default()
};
// Missing dir ⇒ Ok(None), never an error.
assert!(TemplateEngine::init(cfg).unwrap().is_none());Sourcepub fn render_page(
&self,
template_name: &str,
page_content: &str,
frontmatter: &HashMap<String, Value>,
site_globals: &HashMap<String, Value>,
) -> Result<String>
pub fn render_page( &self, template_name: &str, page_content: &str, frontmatter: &HashMap<String, Value>, site_globals: &HashMap<String, Value>, ) -> Result<String>
Renders a page through the template chain.
§Arguments
template_name— template to render (e.g."page.html")page_content— compiled HTML content from staticdatagenfrontmatter— parsed frontmatter as JSON key-value pairssite_globals— site-level variables (name,base_url, etc.)
§Per-page language (spec A5, plan §2 1.5)
Inside the render context, site.language and page.language
both carry the page’s resolved language rather than the raw
site-wide default: front-matter language wins, then
front-matter hreflang, then the site.language global, then
"en" — normalised to BCP-47 hyphen form. Because the default
templates emit <html lang="{{ site.language }}">, a /hi/…
page with front-matter language: hi renders
<html lang="hi"> even when the site default is en-GB, so
<html lang> agrees with the JSON-LD inLanguage,
og:locale, and hreflang self-reference sinks that resolve
through seo::lang::resolve_page_lang.
§Examples
use ssg::template_engine::{TemplateConfig, TemplateEngine};
use tempfile::tempdir;
use std::collections::HashMap;
use std::fs;
let dir = tempdir().unwrap();
let cfg = TemplateConfig {
template_dir: dir.path().to_path_buf(),
..TemplateConfig::default()
};
// No matching template ⇒ returns the content unchanged.
let engine = TemplateEngine::init(cfg).unwrap().unwrap();
let html = engine.render_page("p.html", "<p>hi</p>", &HashMap::new(), &HashMap::new()).unwrap();
assert!(html.contains("hi"));Sourcepub fn has_template(&self, name: &str) -> bool
pub fn has_template(&self, name: &str) -> bool
Reports whether name resolves to a loadable template.
Callers use this to distinguish “rendered through a template”
from render_page’s pass-through arm,
which hands the input back unchanged when neither the requested
template nor the page.html fallback exists.
§Examples
use ssg::template_engine::{TemplateConfig, TemplateEngine};
use tempfile::tempdir;
use std::fs;
let dir = tempdir().unwrap();
fs::write(dir.path().join("page.html"), "{{ page.content }}").unwrap();
let cfg = TemplateConfig {
template_dir: dir.path().to_path_buf(),
..TemplateConfig::default()
};
let engine = TemplateEngine::init(cfg).unwrap().unwrap();
assert!(engine.has_template("page.html"));
assert!(!engine.has_template("missing.html"));Sourcepub fn site_globals_from_config(config: &SsgConfig) -> HashMap<String, Value>
pub fn site_globals_from_config(config: &SsgConfig) -> HashMap<String, Value>
Builds site-level globals from an SsgConfig.
§Examples
use ssg::cmd::SsgConfig;
use ssg::template_engine::TemplateEngine;
let cfg = SsgConfig::default();
let globals = TemplateEngine::site_globals_from_config(&cfg);
assert!(globals.contains_key("name"));
assert!(globals.contains_key("base_url"));Sourcepub fn load_data_files(content_dir: &Path) -> HashMap<String, Value>
pub fn load_data_files(content_dir: &Path) -> HashMap<String, Value>
Loads data files from a data/ directory into the context.
Supports .toml, .json, and .yml/.yaml files.
Files are accessible as {{ data.filename }} in templates.
Example: data/nav.toml → {{ data.nav.links }}
§Examples
use ssg::template_engine::TemplateEngine;
use tempfile::tempdir;
let dir = tempdir().unwrap();
// Returns empty map when no sibling `data/` dir exists.
let data = TemplateEngine::load_data_files(dir.path());
assert!(data.is_empty());Trait Implementations§
Auto Trait Implementations§
impl !Freeze for TemplateEngine
impl !RefUnwindSafe for TemplateEngine
impl !UnwindSafe for TemplateEngine
impl Send for TemplateEngine
impl Sync for TemplateEngine
impl Unpin for TemplateEngine
impl UnsafeUnpin for TemplateEngine
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more