Expand description
§rainier-view
The V in MVC: a View value a controller returns, a ViewEngine
port, and TemplateEngine — the file-backed template engine. The directive
syntax will be familiar from PHP templating engines.
use rainier_view::{MemoryEngine, View, ViewEngine};
let engine = MemoryEngine::new().with(
"posts.index",
"<ul>@foreach(posts as post)<li>{{ post.title }}</li>@endforeach</ul>",
);
let view = View::with("posts.index", serde_json::json!({
"posts": [{ "title": "Hello & welcome" }],
})).unwrap();
assert_eq!(
engine.render_view(&view).unwrap(),
"<ul><li>Hello & welcome</li></ul>",
);§Escaped by default
{{ … }} HTML-escapes; {!! … !!} does not. The safe form is the short
one, so the way you write a value without thinking is the way that cannot
inject a <script>. Emitting raw HTML is possible but has to be asked for.
§A deliberately small language
{{ }}, {!! !!}, @if/@elseif/@else, @foreach, @include, and
@extends/@section/@yield. There is no arbitrary expression evaluation
and no way to call a function, because a template that can compute is a
template that ends up holding business logic. Prepare the data in the
controller; let the template lay it out.
Re-exports§
pub use engine::escape_html;pub use engine::MemoryEngine;pub use engine::TemplateEngine;pub use engine::View;pub use engine::ViewEngine;pub use template::CompareOp;pub use template::Condition;pub use template::Expr;pub use template::Literal;pub use template::Node;pub use template::Template;
Modules§
- engine
- Rendering — the
ViewEngineport, theViewvalue, and theTemplateEngineimplementation. - template
- Parsing a template into a
Nodetree.