-
-
Notifications
You must be signed in to change notification settings - Fork 297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
response from template #682
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
pub mod auth; | ||
pub mod cache; | ||
pub mod dashboard; | ||
pub mod middlewares; | ||
pub mod mylayer; | ||
pub mod mysession; | ||
pub mod notes; | ||
pub mod responses; | ||
pub mod upload; | ||
pub mod user; | ||
pub mod view_engine; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
pub mod auth; | ||
pub mod dashboard; | ||
pub mod engine; | ||
pub mod notes; | ||
pub mod upload; | ||
pub mod user; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ mod prepare_data; | |
mod responses; | ||
mod upload; | ||
mod user; | ||
mod view_engine; |
8 changes: 8 additions & 0 deletions
8
examples/demo/tests/requests/snapshots/hello@view_engine.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
source: tests/requests/view_engine.rs | ||
expression: "(response.status_code(), response.text())" | ||
--- | ||
( | ||
200, | ||
"hello", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
source: tests/requests/view_engine.rs | ||
expression: "(response.status_code(), response.text())" | ||
--- | ||
( | ||
200, | ||
"<html><body>\n <img src=\"/static/image.png\" width=\"200\"/>\n <br/>\n find this tera template at <code>assets/views/home/hello.html</code>: \n <br/>\n <br/>\n Hello World!, \n <br/>\n Hallo Welt!\n \n</body></html>\n ", | ||
) |
8 changes: 8 additions & 0 deletions
8
examples/demo/tests/requests/snapshots/simple@view_engine.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
source: tests/requests/view_engine.rs | ||
expression: "(response.status_code(), response.text())" | ||
--- | ||
( | ||
200, | ||
"Loco website", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use blo::app::App; | ||
use insta::assert_debug_snapshot; | ||
use loco_rs::testing; | ||
use rstest::rstest; | ||
use serial_test::serial; | ||
// TODO: see how to dedup / extract this to app-local test utils | ||
// not to framework, because that would require a runtime dep on insta | ||
macro_rules! configure_insta { | ||
($($expr:expr),*) => { | ||
let mut settings = insta::Settings::clone_current(); | ||
settings.set_prepend_module_to_snapshot(false); | ||
settings.set_snapshot_suffix("view_engine"); | ||
let _guard = settings.bind_to_scope(); | ||
}; | ||
} | ||
|
||
#[rstest] | ||
#[case("home")] | ||
#[case("hello")] | ||
#[case("simple")] | ||
#[tokio::test] | ||
#[serial] | ||
async fn can_get_view_engine(#[case] uri: &str) { | ||
configure_insta!(); | ||
testing::request::<App, _, _>(|request, _ctx| async move { | ||
let response = request.get(&format!("/view-engine/{uri}")).await; | ||
|
||
assert_debug_snapshot!( | ||
uri.replace('/', "_"), | ||
(response.status_code(), response.text()) | ||
); | ||
}) | ||
.await; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,31 @@ impl<E> ViewEngine<E> { | |
} | ||
} | ||
|
||
/// A struct representing an inline Tera view renderer. | ||
/// | ||
/// This struct provides functionality to render templates using the Tera templating engine | ||
/// directly from raw template strings. | ||
/// | ||
/// # Example | ||
/// ``` | ||
/// use serde_json::json; | ||
/// use loco_rs::controller::views; | ||
/// let render = views::template("{{name}} website", json!({"name": "Loco"})).unwrap(); | ||
/// assert_eq!(render, "Loco website"); | ||
/// ``` | ||
/// | ||
/// # Errors | ||
/// | ||
/// This function will return an error if building fails | ||
pub fn template<S>(template: &str, data: S) -> Result<String> | ||
where | ||
S: Serialize, | ||
{ | ||
let mut tera = tera::Tera::default(); | ||
tera.add_raw_template("default", template)?; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we want: https://docs.rs/tera/latest/tera/struct.Tera.html#method.render_str |
||
Ok(tera.render("default", &tera::Context::from_serialize(data)?)?) | ||
} | ||
|
||
impl<E> From<E> for ViewEngine<E> { | ||
fn from(inner: E) -> Self { | ||
Self::new(inner) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blob
->glob