Add a benefit and a simple example to return current year

Thanks @bep!
This commit is contained in:
Rick Cogley 2016-02-03 22:45:21 +09:00 committed by Anthony Fok
parent 49c2761b28
commit d1d7702c06

View file

@ -1,7 +1,7 @@
---
aliases:
- /doc/shortcodes/
lastmod: 2015-11-29
lastmod: 2016-02-03
date: 2013-07-01
menu:
main:
@ -29,6 +29,10 @@ using a predefined template. Note that shortcodes will not work in template
files---if you need a functionality like that in a template, you most likely
want a [partial template](/templates/partials/) instead.
Another benefit is, you can update your shortcode with any related new classes or
techniques, and upon generation, Hugo will easily merge in your changes. You
avoid a possibly complicated search and replace operation.
## Using a shortcode
In your content files, a shortcode can be called by using the `{{%/* name parameters
@ -165,7 +169,7 @@ Assuming that standard Hugo pretty URLs are turned on.
### Twitter
You want to include a single tweet into your blog post? Everything you need is the URL of the tweet, e.g.:
* https://twitter.com/spf13/status/666616452582129664
Pass the tweet's ID from the URL as parameter to the shortcode as shown below:
@ -180,7 +184,7 @@ This shortcode embeds a responsive video player for [YouTube](https://www.youtub
Copy the ID from behind `v=` and pass it the shortcode:
{{</* youtube w7Ft2ymGmfc */>}}
{{</* youtube w7Ft2ymGmfc */>}}
Furthermore, you can autostart the embedded video by setting the `autostart` parameter to true. Remember that you can't mix named an unamed parameters. Assign the yet unamed video id to the parameter `id` like below too.
@ -373,4 +377,14 @@ And will be rendered as:
Please notice that this template makes use of a Hugo-specific template function
called `highlight` which uses Pygments to add the highlighting code.
## Simple Single-word Example: Year
Let's assume you would like to have a shortcode to be replaced by the current year in your Markdown content files, for a license or copyright statement. Calling a shortcode like this:
{{</* year */>}}
... would load your one-line template ``/layouts/shortcodes/year.html``, which contains:
{{ .Page.Now.Year }}
More shortcode examples can be found at [spf13.com](https://github.com/spf13/spf13.com/tree/master/layouts/shortcodes).