Struct ramhorns::Ramhorns

source ·
pub struct Ramhorns<H = FnvBuildHasher> { /* private fields */ }
Expand description

Aggregator for Templates, that allows them to be loaded from the file system and use partials: {{>partial}}

For faster or DOS-resistant hashes, it is recommended to use aHash RandomState as hasher.

Implementations§

source§

impl<H: BuildHasher + Default> Ramhorns<H>

source

pub fn from_folder<P: AsRef<Path>>(dir: P) -> Result<Self, Error>

Loads all the .html files as templates from the given folder, making them accessible via their path, joining partials as required. If a custom extension is wanted, see [from_folder_with_extension]

let tpls: Ramhorns = Ramhorns::from_folder("./templates").unwrap();
let content = "I am the content";
let rendered = tpls.get("hello.html").unwrap().render(&content);
source

pub fn from_folder_with_extension<P: AsRef<Path>>( dir: P, extension: &str ) -> Result<Self, Error>

Loads all files with the extension given in the extension parameter as templates from the given folder, making them accessible via their path, joining partials as required.

let tpls: Ramhorns = Ramhorns::from_folder_with_extension("./templates", "mustache").unwrap();
let content = "I am the content";
let rendered = tpls.get("hello.mustache").unwrap().render(&content);
source

pub fn extend_from_folder<P: AsRef<Path>>( &mut self, dir: P ) -> Result<(), Error>

Extends the template collection with files with .html extension from the given folder, making them accessible via their path, joining partials as required. If there is a file with the same name as a previously loaded template or partial, it will not be loaded.

source

pub fn extend_from_folder_with_extension<P: AsRef<Path>>( &mut self, dir: P, extension: &str ) -> Result<(), Error>

Extends the template collection with files with extension from the given folder, making them accessible via their path, joining partials as required. If there is a file with the same name as a previously loaded template or partial, it will not be loaded.

source

pub fn lazy<P: AsRef<Path>>(dir: P) -> Result<Self, Error>

Create a new empty aggregator for a given folder. This won’t do anything until a template has been added using from_file.

let mut tpls: Ramhorns = Ramhorns::lazy("./templates").unwrap();
let content = "I am the content";
let rendered = tpls.from_file("hello.html").unwrap().render(&content);
source

pub fn get(&self, name: &str) -> Option<&Template<'static>>

Get the template with the given name, if it exists.

source

pub fn from_file(&mut self, name: &str) -> Result<&Template<'static>, Error>

Get the template with the given name. If the template doesn’t exist, it will be loaded from file and parsed first.

Use this method in tandem with lazy.

source

pub fn insert<S, T>(&mut self, src: S, name: T) -> Result<(), Error>
where S: Into<Cow<'static, str>>, T: Into<Cow<'static, str>>,

Insert a template parsed from src with the name name. If a template with this name is present, it gets replaced.

§Warning

This can load partials from an arbitrary path. Use only with trusted source.

Trait Implementations§

source§

impl<H> Debug for Ramhorns<H>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<H> Freeze for Ramhorns<H>
where H: Freeze,

§

impl<H> RefUnwindSafe for Ramhorns<H>
where H: RefUnwindSafe,

§

impl<H> Send for Ramhorns<H>
where H: Send,

§

impl<H> Sync for Ramhorns<H>
where H: Sync,

§

impl<H> Unpin for Ramhorns<H>
where H: Unpin,

§

impl<H> UnwindSafe for Ramhorns<H>
where H: UnwindSafe,

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

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.