[][src]Struct ructe::Ructe

pub struct Ructe { /* fields omitted */ }

The main build-time interface of ructe.

Your build script should create an instance of Ructe and use it to compile templates and possibly get access to the static files handler.

Ructe compiles your templates to rust code that should be compiled with your other rust code, so it needs to be called before compiling. Assuming you use cargo, it can be done like this:

First, specify a build script and ructe as a build dependency in Cargo.toml:

build = "src/build.rs"

[build-dependencies]
ructe = "0.6.0"

Then, in build.rs, compile all templates found in the templates directory and put the output where cargo tells it to:

use ructe::{Result, Ructe};

fn main() -> Result<()> {
    Ructe::from_env()?.compile_templates("templates")
}

And finally, include and use the generated code in your code. The file templates.rs will contain mod templates { ... }, so I just include it in my main.rs:

This example is not tested
include!(concat!(env!("OUT_DIR"), "/templates.rs"));

When creating a Ructe it will create a file called templates.rs in your $OUT_DIR (which is normally created and specified by cargo). The methods will add content, and when the Ructe goes of of scope, the file will be completed.

Methods

impl Ructe[src]

pub fn from_env() -> Result<Ructe>[src]

Create a Ructe instance suitable for a cargo-built project.

A file called templates.rs (and a directory called templates containing sub-modules) will be created in the directory that cargo specifies with the OUT_DIR environment variable.

pub fn new(out_dir: PathBuf) -> Result<Ructe>[src]

Create a ructe instance writing to a given directory.

The out_dir path is assumed to be a directory that exists and is writable. A file called templates.rs (and a directory called templates containing sub-modules) will be created in out_dir.

If you are using Ructe in a project that uses cargo, you should probably use from_env instead.

pub fn compile_templates<P>(&mut self, indir: P) -> Result<()> where
    P: AsRef<Path>, 
[src]

Create a templates module in outdir containing rust code for all templates found in indir.

If indir is a relative path, it should be relative to the main directory of your crate, i.e. the directory containing your Cargo.toml file.

Files with suffix .rs.html, .rs.svg, or .rs.xml are considered templates. A templete file called template.rs.html, template.rs.svg, etc, will result in a callable function named template_html, template_svg, etc. The template_html function will get a template alias for backwards compatibility, but that will be removed in a future release.

pub fn statics(&mut self) -> Result<StaticFiles>[src]

Create a StaticFiles handler for this Ructe instance.

This will create a statics module inside the generated templates module.

Examples

This code goes into the build.rs:

let mut ructe = Ructe::from_env()?;
ructe.statics()?.add_files("static")

Assuming your project have a directory named static that contains e.g. a file called logo.svg and you have included the generated templates.rs, you can now use templates::statics::logo_png as a StaticFile in your project.

Trait Implementations

impl Drop for Ructe[src]

Auto Trait Implementations

impl Send for Ructe

impl Sync for Ructe

impl Unpin for Ructe

impl UnwindSafe for Ructe

impl RefUnwindSafe for Ructe

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]