partials support and documentation

This commit is contained in:
nuex 2013-11-15 04:18:29 -05:00
parent 45b6f41120
commit b53401fe4b
3 changed files with 92 additions and 4 deletions

View File

@ -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. `{{{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.
<body>
<h1>Welcome!</h1>
{{> nav}}
<p>Thanks for checking out my site!</p>
</body>
The `nav.partial` file could have the following contents:
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/blog">Blog</a></li>
<li><a href="/about">About</a></li>
</ul>
</nav>
This would make the above template expand to:
<body>
<h1>Welcome!</h1>
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/blog">Blog</a></li>
<li><a href="/about">About</a></li>
</ul>
</nav>
<p>Thanks for checking out my site!</p>
</body>
### Helpers ### 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: 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:

View File

@ -21,4 +21,4 @@ set -- "$@" -
set -- "$@" "$file" set -- "$@" "$file"
[ -f "$proj/main.layout" ] && set -- "$@" "$proj/main.layout" [ -f "$proj/main.layout" ] && set -- "$@" "$proj/main.layout"
__zod_config | awk "$@" > "$destination/$page" __zod_config | awk -v proj="$proj" "$@" > "$destination/$page"

View File

@ -77,17 +77,66 @@ END {
} }
function bind_data(txt, tag, key) { function bind_data(txt, tag, key) {
if (match(txt, /{{([^}]*)}}/)) { if (match(txt, /{{> ([^}]*)}}/)) {
tag = substr(txt, RSTART, RLENGTH) tag = substr(txt, RSTART, RLENGTH)
match(tag, /([[:alnum:]_]|[?]).*[^}]/) match(tag, /([[:alnum:]_]|[?]).*[^}]/)
key = substr(tag, RSTART, RLENGTH) key = substr(tag, RSTART, RLENGTH)
gsub(tag, data[key], txt) partial_txt = load_partial(key)
return bind_data(txt, data) 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 { } else {
return txt 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) { function render_content(type, ext_key, filter_ext, filter_cmd, txt) {
ext_key = type "_ext" ext_key = type "_ext"