Skip to main content

ViewFallback

Struct ViewFallback 

Source
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

Source

pub fn new(view: View) -> ViewFallback

创建新的模板渲染兜底 helper

Source

pub fn with_default_engine() -> ViewFallback

从默认配置创建模板渲染兜底 helper

Source

pub fn with_config(config: ViewConfig) -> ViewFallback

从指定配置创建模板渲染兜底 helper

Source

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:模板名(对齐 PHP fetch($template)
  • vars:模板变量(可选,对齐 PHP fetch($template, $vars)
§返回

Ok(Response):HTTP 200,Content-Type: text/html; charset=utf-8 Err(ViewError):模板未找到 / 渲染失败

Source

pub fn render_display( &self, content: &str, vars: Option<HashMap<String, Value>>, ) -> Result<Response<Body>, ViewError>

渲染字符串内容为 HTML Response(兜底场景)

对齐 PHP $this->display($content) + Response::create($content, 'html')

§参数
  • content:模板字符串内容
  • vars:模板变量(可选)
§返回

Ok(Response):HTTP 200,Content-Type: text/html; charset=utf-8 Err(ViewError):渲染失败

Source

pub fn render_to_string( &self, template: &str, vars: Option<HashMap<String, Value>>, ) -> Result<String, ViewError>

渲染模板文件为字符串(用于 PDF/Excel 生成器输入)

PDF/Excel 生成器通常需要 HTML 字符串作为输入,而非 HTTP Response。 本方法直接返回渲染后的 HTML 字符串。

§参数
  • template:模板名
  • vars:模板变量(可选)
§返回

Ok(String):渲染后的 HTML 字符串 Err(ViewError):模板未找到 / 渲染失败

Source

pub fn display_to_string( &self, content: &str, vars: Option<HashMap<String, Value>>, ) -> Result<String, ViewError>

渲染字符串内容为字符串(用于 PDF/Excel 生成器输入)

§参数
  • content:模板字符串内容
  • vars:模板变量(可选)
§返回

Ok(String):渲染后的 HTML 字符串 Err(ViewError):渲染失败

Source

pub fn view(&self) -> &View

获取内部 View 引用(用于直接操作 View)

Source

pub fn assign(&self, name: &str, value: Value) -> &ViewFallback

赋值模板变量(对齐 PHP View::assign

Source

pub fn assign_many(&self, vars: HashMap<String, Value>) -> &ViewFallback

批量赋值模板变量

Source

pub fn clear_vars(&self) -> &ViewFallback

清除所有变量

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

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

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more