Move to Tera templating engine for better customizability

This commit is contained in:
lemonsh 2022-02-04 15:24:32 +01:00
parent d191a339f8
commit 707b2da0d3
5 changed files with 21 additions and 31 deletions

12
.gitignore vendored
View file

@ -1,6 +1,6 @@
/target /target
uberbot_*.toml uberbot_*.toml
uberbot.toml uberbot.toml
/Cargo.lock /Cargo.lock
.idea .idea
*.db3 *.db3

View file

@ -27,7 +27,7 @@ rusqlite = { version = "0.26", features = ["bundled"] }
warp = "0.3" warp = "0.3"
futures-util = "0.3" futures-util = "0.3"
irc = { version = "0.15", default-features = false } irc = { version = "0.15", default-features = false }
handlebars = "4.2" tera = { version = "1.15", default-features = false }
[features] [features]
tls = ["irc/tls-rust"] tls = ["irc/tls-rust"]

View file

@ -136,11 +136,6 @@ impl ExecutorConnection {
Option<Quote>, Option<Quote>,
author: Option<String> author: Option<String>
); );
executor_wrapper!( executor_wrapper!(search, Task::Search, Option<Vec<Quote>>, query: String);
search,
Task::Search,
Option<Vec<Quote>>,
query: String
);
executor_wrapper!(random20, Task::Random20, Option<Vec<Quote>>); executor_wrapper!(random20, Task::Random20, Option<Vec<Quote>>);
} }

View file

@ -1,6 +1,5 @@
use crate::database::Quote; use crate::database::Quote;
use crate::ExecutorConnection; use crate::ExecutorConnection;
use handlebars::Handlebars;
use irc::client::Client; use irc::client::Client;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use reqwest::StatusCode; use reqwest::StatusCode;
@ -8,16 +7,12 @@ use serde::{Deserialize, Serialize};
use serde_json::Value::Null; use serde_json::Value::Null;
use std::net::SocketAddr; use std::net::SocketAddr;
use std::sync::Arc; use std::sync::Arc;
use tera::{Context, Tera};
use tokio::sync::broadcast::Receiver; use tokio::sync::broadcast::Receiver;
use warp::{reply, Filter, Reply}; use warp::{reply, Filter, Reply};
lazy_static! { lazy_static! {
static ref HANDLEBARS: Handlebars<'static> = { static ref TERA: Tera = Tera::new("templates/**/*").unwrap();
let mut reg = Handlebars::new();
reg.register_template_string("quotes", include_str!("res/quote_tmpl.hbs"))
.unwrap();
reg
};
} }
pub async fn run( pub async fn run(
@ -58,7 +53,7 @@ struct QuotesTemplate {
#[derive(Deserialize)] #[derive(Deserialize)]
struct QuotesQuery { struct QuotesQuery {
q: Option<String> q: Option<String>,
} }
async fn handle_get_quote(query: QuotesQuery, db: ExecutorConnection) -> impl Reply { async fn handle_get_quote(query: QuotesQuery, db: ExecutorConnection) -> impl Reply {
@ -84,7 +79,7 @@ async fn handle_get_quote(query: QuotesQuery, db: ExecutorConnection) -> impl Re
flash: Some("Displaying up to 20 random quotes".into()), flash: Some("Displaying up to 20 random quotes".into()),
} }
}; };
match HANDLEBARS.render("quotes", &template) { match TERA.render("quotes.html", &Context::from_serialize(&template).unwrap()) {
Ok(o) => reply::html(o).into_response(), Ok(o) => reply::html(o).into_response(),
Err(e) => { Err(e) => {
tracing::warn!("Error while rendering template: {}", e); tracing::warn!("Error while rendering template: {}", e);

View file

@ -23,10 +23,10 @@
</label> </label>
<button type="submit">Search</button> <button type="submit">Search</button>
</form> </form>
{{#if flash}} {% if flash %}
<p>{{flash}}</p> <p>{{ flash }}</p>
{{/if}} {% endif %}
{{#if quotes}} {% if quotes %}
<table> <table>
<thead> <thead>
<tr> <tr>
@ -35,15 +35,15 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{{#each quotes}} {% for q in quotes %}
<tr> <tr>
<td>{{author}}</td> <td>{{ q.author }}</td>
<td>{{quote}}</td> <td>{{ q.quote }}</td>
</tr> </tr>
{{/each}} {% endfor %}
</tbody> </tbody>
</table> </table>
{{/if}} {% endif %}
</main> </main>
</body> </body>
</html> </html>