getJson and getCsv documentation proof reading by @msjulias

This commit is contained in:
Cyrill Schumacher 2015-02-15 17:41:46 +11:00 committed by spf13
parent 4342dd84fc
commit b209f44335

View file

@ -13,20 +13,20 @@ weight: 91
Dynamic content with a static site generator? Yes it is possible! Dynamic content with a static site generator? Yes it is possible!
Besides 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
any [JSON](http://www.json.org/) or any [JSON](http://www.json.org/) or
[CSV](http://en.wikipedia.org/wiki/Comma-separated_values) file [CSV](http://en.wikipedia.org/wiki/Comma-separated_values) file
from nearly any resource. from nearly any resource.
"Dynamic Content" consists at the moment of two functions `getJson` "Dynamic Content" currently consists of two functions, `getJson`
and `getCsv` which are available in **all template files**. and `getCsv`, which are available in **all template files**.
## Implementation details ## Implementation details
### Calling the functions with an URL ### Calling the functions with an URL
In any template file call the functions like: In any HTML template or Markdown document call the functions like:
{{ $dataJ := getJson "url" }} {{ $dataJ := getJson "url" }}
@ -34,7 +34,7 @@ In any template file call the functions like:
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
accepts [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" }}
@ -103,14 +103,15 @@ 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`. But 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
At the moment you can only use those authentication methods which can Currently you can only use those authentication methods that can
be put into an URL. OAuth or other stuff is not implemented. be put into an URL. [OAuth](http://en.wikipedia.org/wiki/OAuth) or
other authentication methods are not implemented.
### Loading local files ### Loading local files
@ -123,15 +124,15 @@ It applies the same output logic as in the topic: *Calling the functions with an
## Live reload ## Live reload
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. But when a local JSON/CSV file changes the content of an URL changes. However when a local JSON/CSV file changes
then of course a live reload will be triggered. Symlinks not supported. then a live reload will be triggered of course. Symlinks not supported.
**URLs and Live reload**: If you change any local file and the live reload **URLs and Live reload**: If you change any local file and the live reload
got trigger 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 creates a huge traffic and also you may 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
@ -146,7 +147,7 @@ your markdown files until the content has been downloaded.
If the community demands the implementation of *getYaml* If the community demands the implementation of *getYaml*
[YAML](http://yaml.org/) or *getToml* [TOML](https://github.com/toml-lang/toml) [YAML](http://yaml.org/) or *getToml* [TOML](https://github.com/toml-lang/toml)
these functions will for sure follow. these functions will certainly follow.
### getSql ### getSql
@ -160,7 +161,7 @@ Maybe adding a new CLI option:
The file must start with `[mysql|postres|mssql|...]_[\w]+.ext` The file must start with `[mysql|postres|mssql|...]_[\w]+.ext`
The part until the first underscore specifies the driver to use which can The part before the first underscore specifies the driver to use, which can
be one from [https://github.com/golang/go/wiki/SQLDrivers](https://github.com/golang/go/wiki/SQLDrivers). be one from [https://github.com/golang/go/wiki/SQLDrivers](https://github.com/golang/go/wiki/SQLDrivers).
The file itself contains only the connection string and no other comments The file itself contains only the connection string and no other comments
@ -177,16 +178,17 @@ The file `mysql_credentials.txt` contains the connection string:
In your template you can process `getSql` the same way as with In your template you can process `getSql` the same way as with
the `getJson` function: the `getJson` function:
{{ $rows := getSql "SELECT id,artist,genre,title from musicTable" }} {{ $rows := getSql "SELECT id,artist,genre,title FROM musicTable" }}
<ul> <ul>
{{range first 5 $rows }} {{range first 5 $rows }}
<li>#{{ .id }} {{.artist}}</a></li> <li>#{{ .id }} {{.artist}}</a></li>
{{end}} {{end}}
</ul> </ul>
Queries with `SELECT * from table` are of course also possible. Queries with `SELECT * FROM table` are of course also possible but
should not be used.
Returned **values** will be converted to **strings**. Returned **values** will be converted to **strings**.
Abusing `getSql` with [DML](http://en.wikipedia.org/wiki/Data_manipulation_language) Abusing `getSql` with [DML](http://en.wikipedia.org/wiki/Data_manipulation_language)
or [DDL](http://en.wikipedia.org/wiki/Data_definition_language) statements or [DDL](http://en.wikipedia.org/wiki/Data_definition_language) statements
is up to you. causes Hugo to hang up.