Skip to content
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

Feat/tera: Support Tera Templating #515

Open
wants to merge 26 commits into
base: master
Choose a base branch
from

Conversation

Galitan-dev
Copy link
Contributor

Feat/tera: Support Tera Templating

See Issue #512

Description

I added Tera Templating support (the Tera instance is sent to handlers via the request extensions). I also implemented the i18n tera filters as said in my last PR.

Usage Example

(See the new tera-i18n example)

# ressources/en-US/simple.ftl
hello-world = Hello world!
welcome = welcome { $name }!
<!-- template/hello.html.tera ->
<h1>{{ "welcome" | translate(name=name) }}</h1>
// src/main.rs
#[handler]
fn hello(Path(name): Path<String>, tera: Tera) -> TeraTemplate {
    tera.render("hello.html.tera", &ctx!{ "name": &name })
}

#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
    let app = Route::new()
        .at("/hello/:name", get(hello))
        .with(TeraTemplating::from_glob("templates/**/*"))
        .using(filters::i18n::translate)
        .data(resources);

    Server::new(TcpListener::bind("127.0.0.1:3000"))
        .run(app)
        .await
}

@Galitan-dev
Copy link
Contributor Author

Galitan-dev commented Feb 18, 2023

oops I broke the Locale extractor, I thought that my implementation of FromRequest for FromRequestSync worked
I will fix it

@Galitan-dev
Copy link
Contributor Author

I think we are good

@Galitan-dev
Copy link
Contributor Author

I force-pushed because the commit is for the next PR.

Comment on lines +331 to +340

/// Extract from request head and body synchronously.
fn from_request_sync(_req: &'a Request, _body: &mut RequestBody) -> Result<Self> {
panic!("Not implemented, please implement one of from_request and from_request_sync");
}

/// Extract from request head synchronously.
fn from_request_without_body_sync(req: &'a Request) -> Result<Self> {
Self::from_request_sync(req, &mut Default::default())
}
Copy link
Collaborator

@sunli829 sunli829 Jun 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's necessary, what do you think? @Galitan-dev

Suggested change
/// Extract from request head and body synchronously.
fn from_request_sync(_req: &'a Request, _body: &mut RequestBody) -> Result<Self> {
panic!("Not implemented, please implement one of from_request and from_request_sync");
}
/// Extract from request head synchronously.
fn from_request_without_body_sync(req: &'a Request) -> Result<Self> {
Self::from_request_sync(req, &mut Default::default())
}
/// Extract from request head and body synchronously.
fn from_request_sync(_req: &'a Request, _body: &mut RequestBody) -> Result<Self> {
panic!("Not implemented, please implement one of from_request and from_request_sync");
}
/// Extract from request head synchronously.
fn from_request_without_body_sync(req: &'a Request) -> Result<Self> {
Self::from_request_sync(req, &mut Default::default())
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants