pub struct ViewFallback { /* private fields */ }Expand description
模板渲染兜底场景 helper(对齐 DefaultResponseType::Html)
项目主策略为前后端分离(JSON 默认返回),但 PDF/Excel 导出、邮件内容、 报表页面等场景需要渲染 HTML 模板。本结构体封装了 View 渲染到 HTML Response 的桥接逻辑,提供便捷的链式调用。
§用法
ⓘ
use sz_rust_core::view::{ViewFallback, ViewConfig};
use serde_json::json;
let fallback = ViewFallback::with_default_engine();
fallback.assign("name", json!("World"));
// 渲染字符串内容为 HTML Response
let response = fallback.render_display("Hello {$name}!", None).unwrap();
// 渲染为字符串(用于 PDF/Excel 生成器输入)
let html = fallback.display_to_string("Hello {$name}!", None).unwrap();Implementations§
Source§impl ViewFallback
impl ViewFallback
Sourcepub fn new(view: View) -> ViewFallback
pub fn new(view: View) -> ViewFallback
创建新的模板渲染兜底 helper
Sourcepub fn with_default_engine() -> ViewFallback
pub fn with_default_engine() -> ViewFallback
从默认配置创建模板渲染兜底 helper
Sourcepub fn with_config(config: ViewConfig) -> ViewFallback
pub fn with_config(config: ViewConfig) -> ViewFallback
从指定配置创建模板渲染兜底 helper
Sourcepub fn render_template(
&self,
template: &str,
vars: Option<HashMap<String, Value>>,
) -> Result<Response<Body>, ViewError>
pub fn render_template( &self, template: &str, vars: Option<HashMap<String, Value>>, ) -> Result<Response<Body>, ViewError>
渲染模板文件为 HTML Response(兜底场景)
对齐 PHP $this->fetch('template') + Response::create($content, 'html')。
项目主策略为 JSON,但 PDF/Excel 导出等场景需要 HTML 渲染。
§参数
template:模板名(对齐 PHPfetch($template))vars:模板变量(可选,对齐 PHPfetch($template, $vars))
§返回
Ok(Response):HTTP 200,Content-Type: text/html; charset=utf-8
Err(ViewError):模板未找到 / 渲染失败
Sourcepub fn render_display(
&self,
content: &str,
vars: Option<HashMap<String, Value>>,
) -> Result<Response<Body>, ViewError>
pub fn render_display( &self, content: &str, vars: Option<HashMap<String, Value>>, ) -> Result<Response<Body>, ViewError>
Sourcepub fn render_to_string(
&self,
template: &str,
vars: Option<HashMap<String, Value>>,
) -> Result<String, ViewError>
pub fn render_to_string( &self, template: &str, vars: Option<HashMap<String, Value>>, ) -> Result<String, ViewError>
Sourcepub fn display_to_string(
&self,
content: &str,
vars: Option<HashMap<String, Value>>,
) -> Result<String, ViewError>
pub fn display_to_string( &self, content: &str, vars: Option<HashMap<String, Value>>, ) -> Result<String, ViewError>
Sourcepub fn assign(&self, name: &str, value: Value) -> &ViewFallback
pub fn assign(&self, name: &str, value: Value) -> &ViewFallback
赋值模板变量(对齐 PHP View::assign)
Sourcepub fn assign_many(&self, vars: HashMap<String, Value>) -> &ViewFallback
pub fn assign_many(&self, vars: HashMap<String, Value>) -> &ViewFallback
批量赋值模板变量
Sourcepub fn clear_vars(&self) -> &ViewFallback
pub fn clear_vars(&self) -> &ViewFallback
清除所有变量
Auto Trait Implementations§
impl !Freeze for ViewFallback
impl !RefUnwindSafe for ViewFallback
impl !UnwindSafe for ViewFallback
impl Send for ViewFallback
impl Sync for ViewFallback
impl Unpin for ViewFallback
impl UnsafeUnpin for ViewFallback
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
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
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> ⓘ
Converts
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> ⓘ
Converts
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.