This commit is contained in:
Anthony Fok 2015-03-17 23:53:17 -06:00
parent bcdad02c06
commit a220a3ba37

View file

@ -11,7 +11,7 @@ title: Dynamic Content
weight: 91 weight: 91
--- ---
Dynamic content with a static site generator? Yes it is possible! Dynamic content with a static site generator? Yes, it is possible!
In addition to the [data files](/extras/datafiles/) feature, we have also In addition to the [data files](/extras/datafiles/) feature, we have also
implemented the feature "Dynamic Content", which lets you load implemented the feature "Dynamic Content", which lets you load
@ -26,23 +26,21 @@ and `getCsv`, which are available in **all template files**.
### Calling the functions with an URL ### Calling the functions with an URL
In any HTML template or Markdown document call the functions like: In any HTML template or Markdown document, call the functions like this:
{{ $dataJ := getJson "url" }} {{ $dataJ := getJson "url" }}
{{ $dataC := getCsv "separator" "url" }} {{ $dataC := getCsv "separator" "url" }}
or, if you use a prefix or postfix for the URL, the functions
or if you use a prefix or postfix for the URL the functions
accept [variadic arguments](http://en.wikipedia.org/wiki/Variadic_function): accept [variadic arguments](http://en.wikipedia.org/wiki/Variadic_function):
{{ $dataJ := getJson "url prefix" "arg1" "arg2" "arg n" }} {{ $dataJ := getJson "url prefix" "arg1" "arg2" "arg n" }}
{{ $dataC := getCsv "separator" "url prefix" "arg1" "arg2" "arg n" }} {{ $dataC := getCsv "separator" "url prefix" "arg1" "arg2" "arg n" }}
The separator for `getCsv` must be put on the first position and can be The separator for `getCsv` must be put in the first position and can only
only one character long. be one character long.
All passed arguments will be joined to the final URL, example: All passed arguments will be joined to the final URL; for example:
{{ $urlPre := "https://api.github.com" }} {{ $urlPre := "https://api.github.com" }}
{{ $gistJ := getJson $urlPre "/users/GITHUB_USERNAME/gists" }} {{ $gistJ := getJson $urlPre "/users/GITHUB_USERNAME/gists" }}
@ -51,23 +49,23 @@ will resolve internally to:
{{ $gistJ := getJson "https://api.github.com/users/GITHUB_USERNAME/gists" }} {{ $gistJ := getJson "https://api.github.com/users/GITHUB_USERNAME/gists" }}
Eventually you can range over the array. This example will output the Finally, you can range over an array. This example will output the
first 5 Github gists for a user: first 5 gists for a GitHub user:
<ul> <ul>
{{ $urlPre := "https://api.github.com" }} {{ $urlPre := "https://api.github.com" }}
{{ $gistJ := getJson $urlPre "/users/GITHUB_USERNAME/gists" }} {{ $gistJ := getJson $urlPre "/users/GITHUB_USERNAME/gists" }}
{{range first 5 $gistJ }} {{ range first 5 $gistJ }}
{{ if .public }} {{ if .public }}
<li><a href="{{ .html_url }}" target="_blank">{{.description}}</a></li> <li><a href="{{ .html_url }}" target="_blank">{{ .description }}</a></li>
{{ end }} {{ end }}
{{end}} {{ end }}
</ul> </ul>
### Example for CSV files ### Example for CSV files
For `getCsv` the one character long separator must be placed on the For `getCsv`, the one-character long separator must be placed in the
first position followed by the URL. first position followed by the URL.
<table> <table>
@ -97,46 +95,46 @@ the current row.
### Caching of URLs ### Caching of URLs
Each downloaded URL will be cached in the default folder `$TMPDIR/hugo_cache/`. Each downloaded URL will be cached in the default folder `$TMPDIR/hugo_cache/`.
The variable `$TMPDIR` will be resolved to your system dependent The variable `$TMPDIR` will be resolved to your system-dependent
temporary directory. temporary directory.
With the command line flag `--cacheDir` you can specify any folder on With the command-line flag `--cacheDir`, you can specify any folder on
your system as a caching directory. your system as a caching directory.
If you don't like caching at all, you can fully disable to read from the If you don't like caching at all, you can fully disable to read from the
cache with the command line flag `--ignoreCache`. However Hugo will always cache with the command line flag `--ignoreCache`. However, Hugo will always
write, on each build of the site, to the cache folder (silent backup). write, on each build of the site, to the cache folder (silent backup).
### Authentication when using REST URLs ### Authentication when using REST URLs
Currently you can only use those authentication methods that can Currently, you can only use those authentication methods that can
be put into an URL. [OAuth](http://en.wikipedia.org/wiki/OAuth) or be put into an URL. [OAuth](http://en.wikipedia.org/wiki/OAuth) or
other authentication methods are not implemented. other authentication methods are not implemented.
### Loading local files ### Loading local files
To load local files with the two functions `getJson` and `getCsv` the To load local files with the two functions `getJson` and `getCsv`, the
source files must reside within Hugos working directory. The file source files must reside within Hugo's working directory. The file
extension does not matter but the content. extension does not matter but the content.
It applies the same output logic as in the topic: *Calling the functions with an URL*. It applies the same output logic as in the topic: *Calling the functions with an URL*.
## Live reload ## LiveReload
There is no chance to trigger a [LiveReload](/extras/livereload/) when There is no chance to trigger a [LiveReload](/extras/livereload/) when
the content of an URL changes. However when a local JSON/CSV file changes the content of an URL changes. However, when a local JSON/CSV file changes,
then a live reload will be triggered of course. Symlinks not supported. then a LiveReload will be triggered of course. Symlinks not supported.
**URLs and Live reload**: If you change any local file and the live reload **URLs and LiveReload**: If you change any local file and the LiveReload
got triggered Hugo will either read the URL content from the cache or, if got triggered, Hugo will either read the URL content from the cache or, if
you have disabled the cache, Hugo will re-download the content. you have disabled the cache, Hugo will re-download the content.
This can create huge traffic and you may also reach API limits quickly. This can create huge traffic and you may also reach API limits quickly.
As downloading of content takes a while, Hugo stops with processing As downloading of content takes a while, Hugo stops with processing
your markdown files until the content has been downloaded. your Markdown files until the content has been downloaded.
## Examples ## Examples
- Photo gallery JSON powered: [https://github.com/pcdummy/hugo-lightslider-example](https://github.com/pcdummy/hugo-lightslider-example) - Photo gallery JSON powered: [https://github.com/pcdummy/hugo-lightslider-example](https://github.com/pcdummy/hugo-lightslider-example)
- Github Starred Repositories [in a posts](https://github.com/SchumacherFM/blog-cs/blob/master/content%2Fposts%2Fgithub-starred.md) with the related [short code](https://github.com/SchumacherFM/blog-cs/blob/master/layouts%2Fshortcodes%2FghStarred.html). - GitHub Starred Repositories [in a posts](https://github.com/SchumacherFM/blog-cs/blob/master/content%2Fposts%2Fgithub-starred.md) with the related [short code](https://github.com/SchumacherFM/blog-cs/blob/master/layouts%2Fshortcodes%2FghStarred.html).
- more? - More? Please tell us!