diff --git a/Cargo.toml b/Cargo.toml index 791f03d..3795e23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,15 +7,14 @@ edition = "2018" [dependencies.rocket] version = "0.5.0-rc.1" features = ["json"] +default-features = false [dependencies] serde = "1.0.130" serde_json = "1.0.67" lazy_static = "1.4.0" +askama_rocket = "0.10.0" [dependencies.rocket_dyn_templates] version = "0.1.0-rc.1" -features = ["tera"] - -#diesel = { version = "1.4.4", features = ["mysql"]} -#dotenv = "0.15.0" \ No newline at end of file +features = ["tera"] \ No newline at end of file diff --git a/README.md b/README.md index ab01492..990e207 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ start dev server: make run_dev -start docker container: docker-compose up [To do for me ] @@ -19,9 +18,9 @@ Frontend: ! Fix css queries ! Fix css footer ! Fix css home posts-section -! Fix js open menu ! add css Animations ! cleaner look ! Add 3d canvas, images ! webp images +! Fix js open menu ! improve js code, use TS ? diff --git a/lang.json b/lang.json index b3db076..dbd1389 100644 --- a/lang.json +++ b/lang.json @@ -1,11 +1,13 @@ { + "lang" : { - "json" : "object", - "test" : ["a", "b", "c"], - "title" : "JSON" + "en" : "English", + "fr" : "Francais", + "it" : "Italiano" }, - "title": { - "en": "Hello Json" + "content": { + "en": "Hello Json", + "it" : "Ciao Json" } } \ No newline at end of file diff --git a/src/build_rocket/config.rs b/src/build_rocket/config.rs index 89509ae..916f0d0 100644 --- a/src/build_rocket/config.rs +++ b/src/build_rocket/config.rs @@ -1,12 +1,11 @@ use std::fs::{ File }; -use rocket_dyn_templates::tera::Value; -use rocket_dyn_templates::tera::Map; -use rocket_dyn_templates::tera::Context; -use rocket_dyn_templates::tera::Error; +use serde::{ Serialize }; -use serde::Serialize; +use std::io::BufReader; + +use serde_json::Value; // Define File for language handling pub const LANG_FILE : &str ="./lang.json"; @@ -15,59 +14,89 @@ pub const LANG_FILE : &str ="./lang.json"; lazy_static! { #[derive(Serialize, Debug)] - pub static ref LANG : Result = init_lang(); + pub static ref LANG : Value = init_lang(); } -// Convert Map to Context -pub fn init_lang() -> Result { - let map = &json_to_map(); - let context : Result = Context::from_value(Value::Object(map.clone().unwrap())); - - match context { - Ok(context) => Ok(context) , - Err(error) => Err(Error::msg(error)), - } -} +fn init_lang() -> Value { + let file = File::open(LANG_FILE).expect("can not open file"); + // read file into buffer + let reader = BufReader::new(file); + + let data : Value = serde_json::from_reader(reader).expect("can not parse Json contents"); -// Convert JSON from file to Map structure -fn json_to_map()-> Option> { - let file = File::open(LANG_FILE).expect("unable to open file"); - let json : Value = serde_json::from_reader(file).expect("file should be JSON syntax"); - let value = json["lang"].as_object(); - match value { - None => None, - Some(value) => Some(value.clone()), - } + data } -pub fn all_to_map() -> Option> { - let file = File::open(LANG_FILE).expect("unable to open file"); - let json : Value = serde_json::from_reader(file).expect("file should be JSON syntax"); - let object = json.as_object(); - match object { - None => None, - Some(object) => Some(object.clone()), - } -} -pub fn debug() { - let map = &all_to_map(); +// Convert Map to Context +// pub fn init_lang() -> Result { +// let map = &json_to_map(); +// let context : Result = Context::from_value(Value::Object(map.clone().unwrap())); - println!("{:#?}", map ); -} +// match context { +// Ok(context) => { +// Ok(context) +// }, +// Err(error) => Err(Error::msg(error)), +// } +// } -pub fn get_key() { +// // Convert JSON from file to Map structure +// pub fn json_to_map()-> Option> { +// // Open lang.json +// let file = File::open(LANG_FILE).expect("can not open file"); +// // read file into buffer +// let reader = BufReader::new(file); - let map = &all_to_map().unwrap(); +// // read JSON into Value +// let json : Value = serde_json::from_reader(reader).expect("can not read Json contents"); - let keys = map.keys(); +// // store JSON key/value pairs in Map +// let obj: Map = json.as_object().unwrap().clone(); - for key in keys { +// // return struct Map +// Some(obj) +// } - println!("{:?}", key ); - } -} + + + +// fn return_value( key: &str, value: &str ) -> Option{ +// let data = json_to_map().unwrap(); + +// let value = data.get(key).unwrap().get(value).unwrap(); + +// Some(value.to_string()) + +// } + +// pub fn debug() { +// println!("context is {:#?}", init_lang()); +// } + + + + + + + + + +// pub fn print_key( key : &str) { + +// let data = json_to_map().unwrap(); + +// let mut keys = data.keys(); + +// let my_key = keys.find(|&x| x == key ).unwrap(); + +// let result = data.get(my_key).unwrap(); + +// println!("requested value is {:?}", result ); + + +// } diff --git a/src/build_rocket/get.rs b/src/build_rocket/get.rs index 1726875..4a745af 100644 --- a/src/build_rocket/get.rs +++ b/src/build_rocket/get.rs @@ -4,10 +4,9 @@ use rocket::fs::NamedFile; // Error type 404 use rocket::response::status::NotFound; -// Responder type Template -use rocket_dyn_templates::{ Template }; +use crate::build_rocket::{ templates }; -use crate::build_rocket::{ templates, config }; +use rocket_dyn_templates::Template; // Configure requests // generate routes, set attributes @@ -15,10 +14,10 @@ use crate::build_rocket::{ templates, config }; // set uri #[get("/")] // if success call handler - pub async fn index() -> Result> { - - NamedFile::open("templates/index.html").await.map_err(|e|NotFound(e.to_string())) - } + pub fn index() -> Template { + let context = templates::TplIndex::new(); + Template::render("index", &context) +} // serve other pages (html GET) // create routes for pages @@ -133,18 +132,9 @@ use crate::build_rocket::{ templates, config }; } #[get("/privacy")] - pub async fn privacy() -> Result> { - NamedFile::open("templates/privacy-policy.html").await.map_err(|e|NotFound(e.to_string())) - } + pub fn privacy() -> Template { + let context = templates::TplPrivacy::new(); + Template::render("privacy-policy", &context) +} - #[get("/test")] - // Render Template Responder - pub fn test() -> Template { - config::debug(); - - // that's the one - config::get_key(); - - Template::render("test_extend", &config::LANG ) - } - \ No newline at end of file + \ No newline at end of file diff --git a/src/build_rocket/templates.rs b/src/build_rocket/templates.rs index e82f1af..99ddf9a 100644 --- a/src/build_rocket/templates.rs +++ b/src/build_rocket/templates.rs @@ -1,35 +1,32 @@ -use rocket_dyn_templates::tera::Context; +use rocket::serde::Serialize; -// Serde Data Model conversion -use rocket::serde::{ Serialize }; - -use crate::build_rocket::{ config }; - -// Implement serde Serialize to create context +// context Template index #[derive(Serialize)] -pub struct Tpl { - pub lang : String, +pub struct TplIndex { + pub title: String, } - - - -pub fn print_value() { - let lang : &Context = &config::init_lang().unwrap(); - let value = lang.get("json").unwrap(); - println!("Value = {:#?}", value); +impl TplIndex { + pub fn new() -> TplIndex { + TplIndex { + title: String::from("Cannabinieri CBD"), + } + } } -pub fn print_object() { - let object = &config::init_lang().unwrap(); - - println!("Context = {:#?}", object); +// context privacy-policy +#[derive(Serialize)] +pub struct TplPrivacy { + // replace this with values from json file + pub title : String, } +impl TplPrivacy { + pub fn new() -> TplPrivacy { + TplPrivacy { + title : String::from("Privacy Policy"), + } - - - - - + } +} diff --git a/src/main.rs b/src/main.rs index 6035a11..3a3ce56 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,6 @@ extern crate lazy_static; mod build_rocket; use crate::build_rocket::{ get, get_static_files }; -// support for template as Responder use rocket_dyn_templates::Template; // Equivalent to main() @@ -39,14 +38,12 @@ async fn main() -> Result<(), rocket::Error> { routes! [ get::spiderpi, get::join ]) .mount("/learn", routes! [ get::about, get::partners, get::meet, get::blog, get::code ]) + .mount("/policies", routes! [ get::policies_info, get::privacy ]) - // test route template - .mount("/", - routes! [ get::test] ) - // initialize and maintain templating state - .attach(Template::fairing()) + // Add Tera Templating support + .attach(Template::fairing()) .ignite().await? diff --git a/templates/assets/css/index.css b/templates/assets/css/index.css index 28504a5..f55cfb1 100644 --- a/templates/assets/css/index.css +++ b/templates/assets/css/index.css @@ -73,7 +73,7 @@ .top-bar { background-color:white; grid-area: top-bar; - display: flex; + display: flex !important; align-items: center; } @@ -1472,7 +1472,7 @@ /* Adjust Media Queries ! */ -@media (max-width: 768px) { + body { background-color: white; @@ -2950,7 +2950,6 @@ .desktop { display: none; } -} diff --git a/templates/base.html.tera b/templates/base.html.tera index 968f999..e0b81b6 100644 --- a/templates/base.html.tera +++ b/templates/base.html.tera @@ -1,13 +1,15 @@ - - - - {{ lang.title }} - - - {%include "test_include" %} -

{{ title }}

-
{% block content %}Me too {% endblock content %} -
- + + + {% block head %} + + + + + {% block title %}Base{% endblock title %} + {% endblock head %} + + + {% block content %}{% endblock content %} + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html.tera similarity index 97% rename from templates/index.html rename to templates/index.html.tera index b1d5453..d47b3c8 100644 --- a/templates/index.html +++ b/templates/index.html.tera @@ -1,25 +1,19 @@ - - - - - - - - - - - Cannabinieri CBD - - +{%extends "base"%} + +{%block head%} +{{super()}} + + - - +{%block title%} {{title}} {%endblock title%} +{%endblock head%} +{%block content%} +{{super()}}