pub struct Unreact<'a> { /* private fields */ }Expand description
Unreact app
Create a new app with Unreact::new()
§Examples
use unreact::prelude::*;
const URL: &str = "https://example.com";
fn main() -> Result<(), Error> {
let mut app = Unreact::new(Config::default(), false, URL)?;
app
.index("page", object! {})?
.route("hi", "hello", object! {
world: "World!"
})?;
app.run()
}Implementations§
Source§impl<'a> Unreact<'a>
impl<'a> Unreact<'a>
Sourcepub fn route(
&mut self,
path: &str,
template: &str,
data: Object,
) -> Result<&mut Self, Error>
pub fn route( &mut self, path: &str, template: &str, data: Object, ) -> Result<&mut Self, Error>
Create a route
NOTE: Route will only validate if template exists in production. In dev mode, this function will always pass, and error will occur during run function
§Parameters
path: The folder (relative to build directory) that file should be written in ({build}/{path}/index.html)template: The name of the template to usedata: Data to pass into the template, as anObject
§Examples
app
// Create a route to '/some_path' with the template 'page.hbs' and a message
.route("some_path", "page", object! {message: "this is at '/some_path'"})?
// Create a route without a template (raw string)
.route_raw("hello", "this is my hello page".to_string())
// Create a route without data
.route("article", "other/article", object! {})?
// Index page with a message
.index("page", object! {message: "World"})?
// 404 page with no data
.not_found("404", object! {})?;§Routing Methods
route: Create a normal routeroute_raw: Create a route without a templateroute_raw_html: Create a HTML page route without a templateindex: Create an index route (/)not_found: Create a 404 route (/404)
Sourcepub fn route_raw(&mut self, path: &str, content: impl Into<String>) -> &mut Self
pub fn route_raw(&mut self, path: &str, content: impl Into<String>) -> &mut Self
Create a route, with raw page content instead of a template
§Parameters
path: The folder (relative to build directory) that file should be written in ({build}/{path}/index.html)content: The raw file contents to write to the file
§Examples
app
// Create a route to '/some_path' with the template 'page.hbs' and a message
.route("some_path", "page", object! {message: "this is at '/some_path'"})?
// Create a route without a template (raw string)
.route_raw("hello", "this is my hello page".to_string())
// Create a route without data
.route("article", "other/article", object! {})?
// Index page with a message
.index("page", object! {message: "World"})?
// 404 page with no data
.not_found("404", object! {})?;§Routing Methods
route: Create a normal routeroute_raw: Create a route without a templateroute_raw_html: Create a HTML page route without a templateindex: Create an index route (/)not_found: Create a 404 route (/404)
Sourcepub fn route_raw_html(
&mut self,
path: &str,
content: impl Into<String>,
) -> &mut Self
pub fn route_raw_html( &mut self, path: &str, content: impl Into<String>, ) -> &mut Self
Create a route, with raw page content instead of a template
Adds HTML boilerplate around content (Unlike route_raw)
§Parameters
path: The folder (relative to build directory) that file should be written in ({build}/{path}/index.html)content: The raw file contents to write to the file
§Examples
app
// Create a route to '/some_path' with the template 'page.hbs' and a message
.route("some_path", "page", object! {message: "this is at '/some_path'"})?
// Create a route without a template (raw string)
.route_raw("hello", "this is my hello page".to_string())
// Create a route without data
.route("article", "other/article", object! {})?
// Index page with a message
.index("page", object! {message: "World"})?
// 404 page with no data
.not_found("404", object! {})?;§Routing Methods
route: Create a normal routeroute_raw: Create a route without a templateroute_raw_html: Create a HTML page route without a templateindex: Create an index route (/)not_found: Create a 404 route (/404)
Sourcepub fn index(
&mut self,
template: &str,
data: Object,
) -> Result<&mut Self, Error>
pub fn index( &mut self, template: &str, data: Object, ) -> Result<&mut Self, Error>
Create the index route
Alias of app.route("", ...)
File is written to {build}/index.html
NOTE: Route will only validate if template exists in production. In dev mode, this function will always pass, and error will occur during run function
§Parameters
template: The name of the template to usedata: Data to pass into the template, as anObject
§Examples
app
// Create a route to '/some_path' with the template 'page.hbs' and a message
.route("some_path", "page", object! {message: "this is at '/some_path'"})?
// Create a route without a template (raw string)
.route_raw("hello", "this is my hello page".to_string())
// Create a route without data
.route("article", "other/article", object! {})?
// Index page with a message
.index("page", object! {message: "World"})?
// 404 page with no data
.not_found("404", object! {})?;§Routing Methods
route: Create a normal routeroute_raw: Create a route without a templateroute_raw_html: Create a HTML page route without a templateindex: Create an index route (/)not_found: Create a 404 route (/404)
Sourcepub fn not_found(
&mut self,
template: &str,
data: Object,
) -> Result<&mut Self, Error>
pub fn not_found( &mut self, template: &str, data: Object, ) -> Result<&mut Self, Error>
Create the 404 route
Alias of app.route("404", ...).
Used as the 404 page, for a path not found
File is written to {build}/404/index.html
NOTE: Route will only validate if template exists in production. In dev mode, this function will always pass, and error will occur during run function
§Parameters
template: The name of the template to usedata: Data to pass into the template, as anObject
§Examples
app
// Create a route to '/some_path' with the template 'page.hbs' and a message
.route("some_path", "page", object! {message: "this is at '/some_path'"})?
// Create a route without a template (raw string)
.route_raw("hello", "this is my hello page".to_string())
// Create a route without data
.route("article", "other/article", object! {})?
// Index page with a message
.index("page", object! {message: "World"})?
// 404 page with no data
.not_found("404", object! {})?;§Routing Methods
route: Create a normal routeroute_raw: Create a route without a templateroute_raw_html: Create a HTML page route without a templateindex: Create an index route (/)not_found: Create a 404 route (/404)
Source§impl<'a> Unreact<'a>
impl<'a> Unreact<'a>
Sourcepub fn new(config: Config, is_dev: bool, url: &str) -> Result<Self, Error>
pub fn new(config: Config, is_dev: bool, url: &str) -> Result<Self, Error>
Create a new empty Unreact app
§Parameters
config: Configuration for the app (SeeConfig)is_dev: Whether the app should build in dev mode (Seeis_dev)url: The url that should be given to rendered templates. Overridden in dev mode. Trailing forward-slash is added if not present
§Examples
§Quick Start
use unreact::prelude::*;
fn main() -> Result<(), Error> {
// Create the app
// Using default config, not in dev mode, and an example url
let mut app = Unreact::new(Config::default(), false, "https://example.com")?;
// Create an index route
// This uses the template 'page.hbs' in 'templates/'
// A json object with a value for 'foo' is passed into the template
app.index("page", object! { foo: "World!" });
// Compile it!
app.run()
}§Long Example
use unreact::prelude::*;
fn main() -> Result<(), Error> {
// Custom config
let config = Config {
// Strict mode enabled
strict: true,
..Config::default()
};
// Create app, with `is_dev` function
let mut app = Unreact::new(config, is_dev(), "https://bruh.news/").expect("Could not create app");
// Set a global variable named 'smiley'
app.globalize(object! {
smiley: "(^_^)"
});
// Some routes
app.index("page", object! {message: "World"})?
.not_found("404", object! {})?
.route_raw("hello", "this is my hello page".to_string())
.route("article", "other/article", object! {})?;
// Run app
app.run().expect("Could not compile app");
println!("Compiled successfully");
Ok(())
}Sourcepub fn globalize(&mut self, data: Object) -> &mut Self
pub fn globalize(&mut self, data: Object) -> &mut Self
Set global variables for templates
§Example
Unreact::new(Config::default(), false, "https://example.com")?
// Index page
.index("page", object! {})?
// Globalize does not need to be ran before routes
.globalize(object! {smiley: "(^_^)"})
// Compiles with a smiley face replacing `{{GLOBAL.smiley}}`
.run()Sourcepub fn handlebars(&mut self) -> &mut Handlebars<'a>
pub fn handlebars(&mut self) -> &mut Handlebars<'a>
Get Handlebars registry as mutable reference
Sourcepub fn run(&self) -> Result<(), Error>
pub fn run(&self) -> Result<(), Error>
Compile app to build directory
Compile app to build directory
NOTE: The "dev" feature is not enabled, so app not open dev server, even in dev mode
Add features = "dev" or features = "watch" to the unreact dependency in Cargo.toml to use the ‘dev server’
§Examples
§Quick Start
use unreact::prelude::*;
fn main() -> Result<(), Error> {
// Create the app
// Using default config, not in dev mode, and an example url
let mut app = Unreact::new(Config::default(), false, "https://example.com")?;
// Create an index route
// This uses the template 'page.hbs' in 'templates/'
// A json object with a value for 'foo' is passed into the template
app.index("page", object! { foo: "World!" });
// Compile it!
app.run()
}Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for Unreact<'a>
impl<'a> !RefUnwindSafe for Unreact<'a>
impl<'a> Send for Unreact<'a>
impl<'a> Sync for Unreact<'a>
impl<'a> Unpin for Unreact<'a>
impl<'a> !UnwindSafe for Unreact<'a>
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