pub struct View { /* private fields */ }Expand description
视图入口(对齐 PHP think\View)
PHP think\View 继承 Manager(多驱动管理),通过 __call 转发到默认驱动。
Rust 实现简化为直接持有引擎实例。
§PHP 对齐方法
| PHP 方法 | Rust 方法 | 说明 |
|---|---|---|
assign($name, $value) | View::assign | 赋值模板变量 |
filter(callable) | View::set_filter | 设置内容过滤器 |
fetch($template, $vars) | View::fetch | 渲染模板文件 |
display($content, $vars) | View::display | 渲染字符串内容 |
engine($type) | View::engine / View::set_engine | 获取/切换引擎 |
exists($template) | View::exists | 模板是否存在 |
__get($name) | View::get_var | 读取变量 |
__isset($name) | View::has_var | 变量是否存在 |
Implementations§
Source§impl View
impl View
Sourcepub fn new(engine: Box<dyn TemplateEngine>) -> View
pub fn new(engine: Box<dyn TemplateEngine>) -> View
创建新视图(对齐 PHP new View())
Sourcepub fn with_default_engine() -> View
pub fn with_default_engine() -> View
创建使用 SimpleTemplateEngine 的默认视图
Sourcepub fn with_config(config: ViewConfig) -> View
pub fn with_config(config: ViewConfig) -> View
创建使用指定配置的默认视图
Sourcepub fn assign(&self, name: &str, value: Value) -> &View
pub fn assign(&self, name: &str, value: Value) -> &View
赋值模板变量(对齐 PHP View::assign)
PHP: $view->assign('name', 'value') 或 $view->assign(['k1' => 'v1'])
Sourcepub fn set_filter(
&self,
filter: Arc<dyn Fn(&str) -> String + Sync + Send>,
) -> &View
pub fn set_filter( &self, filter: Arc<dyn Fn(&str) -> String + Sync + Send>, ) -> &View
设置内容过滤器(对齐 PHP View::filter)
PHP: $view->filter(function($content) { return strtoupper($content); })
Sourcepub fn clear_filter(&self) -> &View
pub fn clear_filter(&self) -> &View
清除过滤器
Sourcepub fn fetch(
&self,
template: &str,
vars: Option<HashMap<String, Value>>,
) -> Result<String, ViewError>
pub fn fetch( &self, template: &str, vars: Option<HashMap<String, Value>>, ) -> Result<String, ViewError>
渲染模板文件(对齐 PHP View::fetch)
PHP: $content = $view->fetch('index', ['name' => 'value'])
合并规则:$vars 优先于 $this->data(对齐 PHP array_merge)
Sourcepub fn display(
&self,
content: &str,
vars: Option<HashMap<String, Value>>,
) -> Result<String, ViewError>
pub fn display( &self, content: &str, vars: Option<HashMap<String, Value>>, ) -> Result<String, ViewError>
渲染字符串内容(对齐 PHP View::display)
PHP: $content = $view->display('Hello {$name}!', ['name' => 'World'])
Sourcepub fn clear_vars(&self) -> &View
pub fn clear_vars(&self) -> &View
清除所有变量
Sourcepub fn engine(&self) -> RwLockReadGuard<'_, RawRwLock, Box<dyn TemplateEngine>>
pub fn engine(&self) -> RwLockReadGuard<'_, RawRwLock, Box<dyn TemplateEngine>>
获取引擎(对齐 PHP View::engine)
Sourcepub fn set_engine(&self, engine: Box<dyn TemplateEngine>) -> &View
pub fn set_engine(&self, engine: Box<dyn TemplateEngine>) -> &View
替换引擎
Auto Trait Implementations§
impl !Freeze for View
impl !RefUnwindSafe for View
impl !UnwindSafe for View
impl Send for View
impl Sync for View
impl Unpin for View
impl UnsafeUnpin for View
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.