diff --git a/components/templates/src/global_fns.rs b/components/templates/src/global_fns.rs index 438369f..538baad 100644 --- a/components/templates/src/global_fns.rs +++ b/components/templates/src/global_fns.rs @@ -59,7 +59,13 @@ pub fn make_get_url(permalinks: HashMap, config: Config) -> Glob .map_or(false, |c| { from_value::(c.clone()).unwrap_or(false) }); - + + let trailing_slash = args + .get("trailing_slash") + .map_or(true, |c| { + from_value::(c.clone()).unwrap_or(true) + }); + match args.get("path") { Some(val) => match from_value::(val.clone()) { Ok(v) => { @@ -72,6 +78,10 @@ pub fn make_get_url(permalinks: HashMap, config: Config) -> Glob } else { // anything else let mut permalink = config.make_permalink(&v); + if !trailing_slash && permalink.ends_with("/") { + permalink.pop(); // Removes the slash + } + if cachebust { permalink = format!("{}?t={}", permalink, config.build_timestamp.unwrap()); } @@ -105,6 +115,27 @@ mod tests { args.insert("cachebust".to_string(), to_value(true).unwrap()); assert_eq!(static_fn(args).unwrap(), "http://a-website.com/app.css/?t=1"); } + + #[test] + fn can_remove_trailing_slashes() { + let config = Config::default(); + let static_fn = make_get_url(HashMap::new(), config); + let mut args = HashMap::new(); + args.insert("path".to_string(), to_value("app.css").unwrap()); + args.insert("trailing_slash".to_string(), to_value(false).unwrap()); + assert_eq!(static_fn(args).unwrap(), "http://a-website.com/app.css"); + } + + #[test] + fn can_remove_slashes_and_cachebust() { + let config = Config::default(); + let static_fn = make_get_url(HashMap::new(), config); + let mut args = HashMap::new(); + args.insert("path".to_string(), to_value("app.css").unwrap()); + args.insert("trailing_slash".to_string(), to_value(false).unwrap()); + args.insert("cachebust".to_string(), to_value(true).unwrap()); + assert_eq!(static_fn(args).unwrap(), "http://a-website.com/app.css?t=1"); + } #[test] fn can_link_to_some_static_file() { diff --git a/docs/content/documentation/templates/overview.md b/docs/content/documentation/templates/overview.md index c69ad73..78160f9 100644 --- a/docs/content/documentation/templates/overview.md +++ b/docs/content/documentation/templates/overview.md @@ -70,5 +70,12 @@ we want to link to the file that is located at `static/css/app.css`: {{ get_url(path="css/app.css") }} ``` +For assets it is reccommended that you pass `trailing_slash=false` to the `get_url` function. This prevents errors +when dealing with certain hosting providers. An example is: + +```jinja2 +{{ get_url(path="css/app.css", trailing_slash=false) }} +``` + In the case of non-internal links, you can also add a cachebust of the format `?t=1290192` at the end of a URL by passing `cachebust=true` to the `get_url` function. diff --git a/docs/templates/index.html b/docs/templates/index.html index 88b041e..c366dbb 100644 --- a/docs/templates/index.html +++ b/docs/templates/index.html @@ -7,8 +7,8 @@ {% block title %}{{ config.title }}{% endblock title %} - - + +