carllerche / tower-web

A fast, boilerplate free, web framework for Rust
MIT License
981 stars 51 forks source link

Allow templates to be loaded without Cargo env #139

Closed bryanburgers closed 6 years ago

bryanburgers commented 6 years ago

Add locations to look for a templates directory when using view::Handlebars::new().

Previously, this would only look at the environment variable CARGO_MANIFEST_DIR, which is only set when run using cargo. This makes it hard to deploy an application that uses handlebars templates.

This change introduces a chain of places to check for a templates directory.

  1. A templates directory under the TOWER_WEB_TEMPLATE_DIR environment variable
  2. A templates directory under the crate root (CARGO_MANIFEST_DIR environment variable)
  3. A templates directory under the current working directory.

This commit also adds an improved error message when one of these locations does not exist when the handlebars location is registered. This is a small breaking change because previously an application would not panic! if the templates directory did not exist on application startup.

Fixes #127 and #136

bryanburgers commented 6 years ago

@carllerche OK, I decided to panic if either TOWER_WEB_TEMPLATE_DIR or ${TOWER_WEB_TEMPLATE_DIR}/templates does not exist.

I'm still a little bit torn if we should require the templates sub-directory or not.