pub struct View { /* private fields */ }Expand description
Implementations§
Source§impl View
impl View
Sourcepub fn with_input(self, request: &Request) -> View
pub fn with_input(self, request: &Request) -> View
Give the view the request’s input as old input, so the template’s old(..) helper reads it
when rendering a form directly (such as a 422 fragment response without a redirect).
Laravel leaves out uploaded files, the top-level password, password_confirmation, and
current_password fields, the fields given to
Errors::dont_flash, and, once the request has been
validated, every Password field at any depth, with its
confirmation.
use laravel::http::{IntoResponse, Request, Response, StatusCode};
use laravel::validation::{Validate, ValidationError};
use laravel::view::{data, view};
#[derive(serde::Deserialize, Validate)]
#[validate(anyone)]
struct StorePost {
#[validate(required)]
title: String,
}
async fn store(request: Request) -> laravel::Result<Response> {
let _form = match request.validate::<StorePost>().await {
Ok(form) => form,
Err(ValidationError::Invalid(errors)) => {
return Ok(view("posts.create", data! {})
.with_errors(errors)
.with_input(&request)
.fragment("form")
.into_response()
.with_status(StatusCode::UNPROCESSABLE_ENTITY));
}
Err(ValidationError::Failed(error)) => return Err(error),
};
Ok(Response::new("saved"))
}Source§impl View
impl View
Sourcepub fn with_errors(self, errors: impl Borrow<InvalidInput>) -> View
pub fn with_errors(self, errors: impl Borrow<InvalidInput>) -> View
Gives the view a form’s field messages as errors, in place of the messages the session
flashed. Every view has an errors map, which is empty when there are no messages, so a
template can always look up a field’s messages.
Messages for a named form, or for the form of the
type the request validated, go in form_errors instead, under the form’s name. Templates read them with error("email", form="login").
async fn store(email: String) -> (StatusCode, View) {
match check(&email) {
Ok(()) => (StatusCode::OK, view("signup.done", ())),
Err(errors) => (
StatusCode::UNPROCESSABLE_ENTITY,
view("signup.form", data! { email }).with_errors(errors).fragment("form"),
),
}
}Source§impl View
impl View
Send a cookie with this response. A (name, value) pair uses the default attributes,
and a Cookie sets its own. The cookie replaces any cookie
that this response already sends with the same name, path, and domain.
use laravel::http::{Redirect, Response};
let response = Response::new("Saved").with_cookie(("theme", "dark"));
let redirect = Redirect::to("/").with_cookie(("theme", "dark"));Remove a browser cookie by sending an expired cookie. A name uses the default path and
domain. To match a custom path or domain, pass a Cookie. The
cookie values that the current request returns stay unchanged.
use laravel::http::Response;
use laravel::cookie::Cookie;
let response = Response::new("Signed out")
.without_cookie("remember")
.without_cookie(Cookie::new("tour", "").path("/admin"));Sourcepub fn with_header<K, V>(self, name: K, value: V) -> View
pub fn with_header<K, V>(self, name: K, value: V) -> View
Send a header with the response, replacing any header with the same name, including one the response would otherwise send:
use laravel::view::{View, view};
async fn report() -> View {
view("reports.show", ()).with_header("cache-control", "no-store")
}A name or value that isn’t a valid header turns the response into a 500 error when
the response is sent.
Sourcepub fn with_headers<K, V>(
self,
headers: impl IntoIterator<Item = (K, V)>,
) -> View
pub fn with_headers<K, V>( self, headers: impl IntoIterator<Item = (K, V)>, ) -> View
Send several headers with the response, such as
.with_headers([("x-one", "1"), ("x-two", "2")]). Each header replaces any header
with the same name, so when the list holds a name twice, the last value wins.
A name or value that isn’t a valid header turns the response into a 500 error when
the response is sent.
Sourcepub fn with_html(self, name: impl Into<String>, markup: Html) -> View
pub fn with_html(self, name: impl Into<String>, markup: Html) -> View
Gives the view markup to display without escaping, such as HTML rendered from trusted Markdown. The markup replaces any data with the same name.
async fn show() -> View {
let body = render_markdown("# Hello");
view("posts.show", data! { title => "Hello" }).with_html("body", body)
}Sourcepub fn fragment(self, block: impl Into<String>) -> View
pub fn fragment(self, block: impl Into<String>) -> View
Renders only the {% block %} named block, rather than the whole page, such as to
replace part of a page from a JavaScript request.
async fn search(partial: bool) -> View {
let results = vec!["Taylor"];
let page = view("users.index", data! { results });
if partial { page.fragment("results") } else { page }
}Sourcepub fn fragment_if(self, condition: bool, block: impl Into<String>) -> View
pub fn fragment_if(self, condition: bool, block: impl Into<String>) -> View
Renders only one block when condition is true, as in
view(..).fragment_if(partial, "results").
Sourcepub const fn data(&self) -> &ViewData
pub const fn data(&self) -> &ViewData
The view’s own data, without the data shared with every view.
Trait Implementations§
Source§impl From<View> for EventData
Converts a view into event data, which renders the view when the event is sent.
impl From<View> for EventData
Converts a view into event data, which renders the view when the event is sent.
Source§impl IntoResponse for View
Renders the view, or the error that kept the view from rendering. The view’s name and data
stay on the response for assert_view_is in
tests.
impl IntoResponse for View
Renders the view, or the error that kept the view from rendering. The view’s name and data
stay on the response for assert_view_is in
tests.
Source§fn with_header<K, V>(self, name: K, value: V) -> Response
fn with_header<K, V>(self, name: K, value: V) -> Response
500 error.Source§fn with_headers<K, V>(
self,
headers: impl IntoIterator<Item = (K, V)>,
) -> Response
fn with_headers<K, V>( self, headers: impl IntoIterator<Item = (K, V)>, ) -> Response
500 error.Auto Trait Implementations§
impl Forget for View
impl Freeze for Viewwhere
*const u8 is !null: Freeze,
PhantomData<u8>: Freeze,
PhantomData<usize>: Freeze,
PhantomData<Bucket<String, Value>>: Freeze,
*const ArcInner<str> is !null: Freeze,
PhantomData<ArcInner<str>>: Freeze,
PhantomData<(String, Html)>: Freeze,
*const [Pos] is !null: Freeze,
PhantomData<[Pos]>: Freeze,
PhantomData<Bucket<HeaderValue>>: Freeze,
PhantomData<ExtraValue<HeaderValue>>: Freeze,
*const ArcInner<Inner> is !null: Freeze,
PhantomData<ArcInner<Inner>>: Freeze,
&'static Location<'static>: Freeze,
impl Move for View
impl RefUnwindSafe for Viewwhere
Atomic<usize>: RefUnwindSafe,
Atomic<*mut ()>: RefUnwindSafe,
Error: RefUnwindSafe,
NonZero<u16>: RefUnwindSafe,
impl Send for View
impl Sync for View
impl Unpin for View
impl UnsafeUnpin for Viewwhere
*const u8 is !null: UnsafeUnpin,
PhantomData<u8>: UnsafeUnpin,
PhantomData<usize>: UnsafeUnpin,
PhantomData<Bucket<String, Value>>: UnsafeUnpin,
*const ArcInner<str> is !null: UnsafeUnpin,
PhantomData<ArcInner<str>>: UnsafeUnpin,
PhantomData<(String, Html)>: UnsafeUnpin,
*const [Pos] is !null: UnsafeUnpin,
PhantomData<[Pos]>: UnsafeUnpin,
PhantomData<Bucket<HeaderValue>>: UnsafeUnpin,
PhantomData<ExtraValue<HeaderValue>>: UnsafeUnpin,
*const ArcInner<Inner> is !null: UnsafeUnpin,
PhantomData<ArcInner<Inner>>: UnsafeUnpin,
&'static Location<'static>: UnsafeUnpin,
impl UnwindSafe for Viewwhere
RawVecInner: UnwindSafe,
NonNull<u8>: UnwindSafe,
Arc<str>: UnwindSafe,
BoxRaw<[Pos]>: UnwindSafe,
*const u8: UnwindSafe,
Align8<*mut ()>: UnwindSafe,
&'static Vtable: UnwindSafe,
Arc<Inner>: UnwindSafe,
&'static Location<'static>: UnwindSafe,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<K> ForeignKey<K> for K
impl<K> ForeignKey<K> for K
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
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> ⓘ
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