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
uberbot_*.toml
uberbot.toml
/Cargo.lock
.idea
*.db3
/target
uberbot_*.toml
uberbot.toml
/Cargo.lock
.idea
*.db3

View file

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

View file

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

View file

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

View file

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