pub struct Ructe { /* private fields */ }
Expand description
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
:
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.
Implementations§
source§impl Ructe
impl Ructe
sourcepub fn from_env() -> Result<Ructe>
pub fn from_env() -> Result<Ructe>
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.
sourcepub fn new(outdir: PathBuf) -> Result<Ructe>
pub fn new(outdir: PathBuf) -> Result<Ructe>
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 Ructe::from_env
instead.
sourcepub fn compile_templates<P>(&mut self, indir: P) -> Result<()>
pub fn compile_templates<P>(&mut self, indir: P) -> Result<()>
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.
sourcepub fn statics(&mut self) -> Result<StaticFiles>
pub fn statics(&mut self) -> Result<StaticFiles>
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")?;
Ok(())
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§
Auto Trait Implementations§
impl Freeze for Ructe
impl RefUnwindSafe for Ructe
impl Send for Ructe
impl Sync for Ructe
impl Unpin for Ructe
impl UnwindSafe for Ructe
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
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