Questions about Maud html templating library


#1
  1. I want to declare html layout in external html files like it’s normally done. Can I use Maud with external html files?

  2. Also, can I use master-layout html templates and inherit child-templates from them? That is, I’d have one master html layout page with the shared header and footer and a few child templates with their own content but which share the same footer and header.

https://lfairy.gitbooks.io/maud/content/


#2

Those are good questions! Maud isn’t mature yet so the best practices aren’t set in stone. But I’ll answer your questions as well as I can.

  1. If you just want to keep your templates in a separate file, then Rust’s module system works great:

    // views.rs
    use std::io;
    use maud;
    
    fn main_page(w: &mut io::Write, title: &str) -> io::Result<()> {
        html_utf8!(w, {
            title ^title
            // ...
        }
    }
    

    If you want to load templates at run-time or use <html> syntax, then I’m afraid that’s out of scope for the library. Maud is integrated tightly with Rust itself (especially the macro system) so it’ll be hard to add anything dynamic.

    A utility that translates plain HTML to Maud syntax would be a great though. With libraries like html5ever such a tool should be straightforward to write.

  2. There is a (currently undocumented) bit of syntax, @call, which can invoke a callback in the middle of a template:

    use std::fmt;
    use std::io;
    fn master(w: &mut io::Write, title: &str, body: F) where
        F: FnOnce(&mut fmt::Write) -> fmt::Result
    {
        html_utf8!(w, {
            title { "My favorite pony - " ^title }
            link rel="stylesheet" href="styles.css" /
            // Call the child template here
            @call body
        })
    }
    

    Then you can call the master template with a sub-template like so:

    master(w, "You!", |w| html!(w, {
        p "My favorite pony is... you!"
        p "You're the cutest, funniest, all round pony-est pony there is!"
    })
    

Hope this helps.


#3
  1. Thanks. But I’d like an external html file.