Skip to main content

TemplateEngine

Struct TemplateEngine 

Source
pub struct TemplateEngine { /* private fields */ }
Expand description

Wraps MiniJinja and provides site-generation-specific rendering.

Implementations§

Source§

impl TemplateEngine

Source

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());
Source

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 staticdatagen
  • frontmatter — parsed frontmatter as JSON key-value pairs
  • site_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"));
Source

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"));
Source

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"));
Source

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§

Source§

impl Debug for TemplateEngine

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more