diff --git a/README.md b/README.md index 17622b0..c7ddfb3 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,45 @@ The `main.layout` file wraps HTML content around a page template. A `main.layou `{{{yield}}}` is a special tag that renders the page content within the layout. `{{{yield}}}` can only be used in the `main.layout` file. +### Partials + +Partials are reusable snippets that can be included in different areas of your site. Partials must have the `.partial` extension and must be in the root of your project directory. Partials are called using two curly brackets and a greater than sign. + + +

Welcome!

+ + {{> nav}} + +

Thanks for checking out my site!

+ + +The `nav.partial` file could have the following contents: + + + +This would make the above template expand to: + + +

Welcome!

+ + + +

Thanks for checking out my site!

+ + + ### Helpers The `helpers.awk` file is an awk script that can make custom data available to your templates. You also have access to the page and global data. Here is a peak at the script included in the examples folder: diff --git a/bin/zod-render b/bin/zod-render index 563de39..f1cf124 100755 --- a/bin/zod-render +++ b/bin/zod-render @@ -21,4 +21,4 @@ set -- "$@" - set -- "$@" "$file" [ -f "$proj/main.layout" ] && set -- "$@" "$proj/main.layout" -__zod_config | awk "$@" > "$destination/$page" +__zod_config | awk -v proj="$proj" "$@" > "$destination/$page" diff --git a/lib/render.awk b/lib/render.awk index 85b1418..1e1c271 100644 --- a/lib/render.awk +++ b/lib/render.awk @@ -77,17 +77,66 @@ END { } function bind_data(txt, tag, key) { - if (match(txt, /{{([^}]*)}}/)) { + if (match(txt, /{{> ([^}]*)}}/)) { tag = substr(txt, RSTART, RLENGTH) match(tag, /([[:alnum:]_]|[?]).*[^}]/) key = substr(tag, RSTART, RLENGTH) - gsub(tag, data[key], txt) - return bind_data(txt, data) + partial_txt = load_partial(key) + gsub(tag, escape_special_chars(partial_txt), txt) + return bind_data(txt) + } else if (match(txt, /{{([^}]*)}}/)) { + tag = substr(txt, RSTART, RLENGTH) + match(tag, /([[:alnum:]_]|[?]).*[^}]/) + key = substr(tag, RSTART, RLENGTH) + gsub(tag, escape_special_chars(data[key]), txt) + return bind_data(txt) } else { return txt } } +# Returns the text from a partial +# +# It will load the partial from the cache if possible. +# Otherwise it will open the partial file and load each +# line. +# +# Nothing is returned if the file doesn't exist. +function load_partial(key, partial, partial_file, line) { + partial = partials[key] + if (partial) { + return partial + } else { + pwd = ENVIRON["PWD"] + partial_file = pwd "/" proj "/" key ".partial" + if (is_file(partial_file)) { + while((getline line < partial_file) > 0) { + if (partial_txt) { + partial_txt = partial_txt "\n" line + } else { + partial_txt = line + } + } + + close(partial_file) + + partials[key] = partial_txt + + return partial_txt + } + } +} + +# Check if a file exists +function is_file(file) { + check = "[ -f " file " ] && echo yes" + check | getline response + close(check) + if (response == "yes") { + return "yes" + } +} + function render_content(type, ext_key, filter_ext, filter_cmd, txt) { ext_key = type "_ext"