Skip to content

Commit

Permalink
Merge pull request sunng87#11 from sunng87/chore/feature-opt-in
Browse files Browse the repository at this point in the history
(chore) make watch function an optional feature
  • Loading branch information
sunng87 committed Jul 8, 2015
2 parents 4873e05 + cb0e5a5 commit 0a0a614
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ handlebars = "*"
rustc-serialize = "*"
plugin = "*"
walker = "*"
notify = "*"
notify = { version = "*", optional = true }

[features]
default = []
watch = ["notify"]
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,28 @@ During development you may want to live-reload your templates without
having to restart your web server. Here comes the live-reload
feature.

Since live-reload may only be useful in development phase, we have
made it a optional feature. In order to enable it, you will need to
add feature `watch` in your cargo declaration:

```
[features]
## create a feature in your app
watch = ["handlebars-iron/watch"]
[dependencies]
handlebars-iron = "*"
```

```rust
let template_engine_ref = Arc::new(HandlebarsEngine::new("./examples/templates/", ".hbs"));
template_engine_ref.watch();

chain.link_after(template_engine_ref);
```

Check `examples/watch_server.rs` for further information.
Check `examples/watch_server.rs` for further information. To test it:
`cargo run --example watch_server --features watch`.

## License

Expand Down
11 changes: 10 additions & 1 deletion examples/watch_server.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#![allow(dead_code, unused_imports)]
extern crate iron;
extern crate handlebars_iron as hbs;
extern crate rustc_serialize;

use iron::prelude::*;
use iron::{status};
use hbs::{Template, HandlebarsEngine, Watchable};
use hbs::{Template, HandlebarsEngine};
#[cfg(feature = "watch")]
use hbs::Watchable;
use rustc_serialize::json::{ToJson, Json};
use std::collections::BTreeMap;
use std::sync::Arc;
Expand Down Expand Up @@ -50,6 +53,7 @@ fn hello_world(_: &mut Request) -> IronResult<Response> {
Ok(resp)
}

#[cfg(feature = "watch")]
fn main() {
let mut chain = Chain::new(hello_world);
let template_engine_ref = Arc::new(HandlebarsEngine::new("./examples/templates/", ".hbs"));
Expand All @@ -60,3 +64,8 @@ fn main() {
println!("Server running at http://localhost:3000/");
Iron::new(chain).http("localhost:3000").unwrap();
}

#[cfg(not(feature = "watch"))]
fn main() {
println!("Watch only enabled via --features watch option");
}
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ extern crate iron;
extern crate rustc_serialize as serialize;
extern crate handlebars;
extern crate plugin;
extern crate notify;
extern crate walker;
#[cfg(feature = "watch")]
extern crate notify;


pub use self::middleware::Template;
pub use self::middleware::HandlebarsEngine;
#[cfg(feature = "watch")]
pub use self::watch::Watchable;

mod middleware;
#[cfg(feature = "watch")]
mod watch;

0 comments on commit 0a0a614

Please sign in to comment.