Skip to content

Commit

Permalink
Allow arbitrary json objects as context
Browse files Browse the repository at this point in the history
  • Loading branch information
Andras Mocsary committed Jun 3, 2019
1 parent 6b8a8c5 commit f92af35
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
7 changes: 7 additions & 0 deletions examples/sbadmin2/templates/index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"products": [
"Apple",
"Pear",
"Banana"
]
}
6 changes: 6 additions & 0 deletions examples/sbadmin2/templates/topbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ <h6 class="dropdown-header">
</div>
</li>

{% for product in products %}
<li class="nav-item">
<a class="nav-link" href="#">{{loop.index}}. {{product}}</a>
</li>
{% endfor %}

<!-- Nav Item - Messages -->
<li class="nav-item dropdown no-arrow mx-1">
<a class="nav-link dropdown-toggle" href="#" id="messagesDropdown" role="button" data-toggle="dropdown"
Expand Down
File renamed without changes.
35 changes: 19 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ extern crate tera;
#[macro_use]
extern crate log;

use std::collections::HashMap;

use actix_files as fs;
use actix_web::http::StatusCode;
use actix_web::{
error, get, guard, middleware, web, App, Error, FromRequest, HttpRequest, HttpResponse,
error, get, middleware, web, App, Error, FromRequest, HttpRequest, HttpResponse,
HttpServer,
};

Expand Down Expand Up @@ -40,13 +37,18 @@ fn templates(
fl
}
};
let ctx = get_context(&file).unwrap();
tmpl.render(&file, &ctx).map_err(|e| {
error::ErrorInternalServerError(format!(
"Template error: {} with context: {:?}",
e, &ctx
))
})?
match get_context(&file) {
Ok(ctx) => tmpl.render(&file, &ctx).map_err(|e| {
error::ErrorInternalServerError(format!(
"Template error: {} with context: {:?}",
e, &ctx
))
})?,
Err(e) => {
error!("{}",e);
format!("Couldn't read context json: {}", e)
}
}
} else {
tmpl.render("404.html", &tera::Context::new())
.map_err(|_| error::ErrorInternalServerError("Template error"))?
Expand All @@ -69,32 +71,33 @@ fn main() -> std::io::Result<()> {
.service(fs::Files::new("/js", "js").show_files_listing())
.service(fs::Files::new("/vendor", "vendor").show_files_listing())
.service(fs::Files::new("/img", "img").show_files_listing())
.service(fs::Files::new("/fonts", "fonts").show_files_listing())
.service(web::resource("{any:.*}").route(web::get().to(templates)))
})
.bind("127.0.0.1:8080")?
.run()
}

fn get_context(file: &str) -> Result<HashMap<String, String>, Error> {
fn get_context(file: &str) -> Result<serde_json::Value, Error> {
let mut fl = concat!(env!("CARGO_MANIFEST_DIR"), "/templates/").to_owned();
fl.push_str(file);
let ctx_file = if let Some(file_ext) = std::path::Path::new(&fl)
.extension()
.and_then(std::ffi::OsStr::to_str)
{
fl.replace(file_ext, "ctx")
fl.replace(file_ext, "json")
} else {
let mut s = fl.to_owned();
s.push_str(".ctx");
s.push_str(".json");
s
};
if std::path::Path::new(&ctx_file).exists() {
let file = std::fs::File::open(ctx_file)?;
let reader = std::io::BufReader::new(file);
let json: HashMap<String, String> = serde_json::from_reader(reader)?;
let json: serde_json::Value = serde_json::from_reader(reader)?;
return Ok(json);
}
Ok(HashMap::new())
Ok(serde_json::Value::Null)
}

/// favicon handler
Expand Down
Binary file removed tera_design_0-1-1.zip
Binary file not shown.

0 comments on commit f92af35

Please sign in to comment.