Комплект шаблонов со стандартными переменными Hugo

Матеріал з Wiki.Zina
Перейти до навігації Перейти до пошуку

←Hugo

При создании страниц Hugo вычисляет значения стандартных переменных.

Увидеть все переменные с реальными значениями на вашем сайте можно, подключив в любом подходящем шаблоне — например, в layouts/_default/single.html — файл со списком переменных:

{{ partial "variables/page.html" . }}
{{ partial "variables/file.html" . }}
{{ partial "variables/site.html" . }}
{{ partial "variables/hugo.html" . }}

Или один файл, в котором уже подключаются все эти файлы:

{{ partial "variables/all.html" . }}

Содержимое этих шаблонов:

Page Variables

[ред. | ред. код]
layouts/partials/variables/page.html
<h3>Page Variables</h3>

<h4>Main</h4>

<table class="table">
  <tr>
    <td><pre>.Title</pre></td>
    <td>{{ .Title }}</td>
    <td class="small">The title for this page.</td>
  </tr>
  <tr>
    <td><pre>.LinkTitle</pre></td>
    <td>{{ .LinkTitle }}</td>
    <td class="small">Access when creating links to this content. Will use linktitle if set in front matter, else title.</td>
  </tr>
  <tr>
    <td><pre>.Section</pre></td>
    <td>{{ .Section }}</td>
    <td class="small">The section this content belongs to.</td>
  </tr>
  <tr>
    <td><pre>.URL</pre></td>
    <td>{{ .URL }}</td>
    <td class="small">The relative URL for this page. Note that if URL is set directly in frontmatter, that URL is returned as-is.</td>
  </tr>
  <tr>
    <td><pre>.Weight</pre></td>
    <td>{{ .Weight }}</td>
    <td class="small">Assigned weight (in the front matter) to this content, used in sorting.</td>
  </tr>
  <tr>
    <td><pre>.Draft</pre></td>
    <td>{{ .Draft }}</td>
    <td class="small">A boolean, true if the content is marked as a draft in the front matter.</td>
  </tr>
  <tr>
    <td><pre>.Type</pre></td>
    <td>{{ .Type }}</td>
    <td class="small">The content type (e.g. post).</td>
  </tr>
  <tr>
    <td><pre>.Kind</pre></td>
    <td>{{ .Kind }}</td>
    <td class="small">What kind of page is this: is one of page, home, section, taxonomy or taxonomyTerm. There are also RSS, sitemap, robotsTXT and 404, but these will only available during rendering of that kind of page, and not available in any of the Pages collections.</td>
  </tr>
  <tr>
    <td><pre>.IsHome</pre></td>
    <td>{{ .IsHome }}</td>
    <td class="small">True if this is the home page.</td>
  </tr>
  <tr>
    <td><pre>.IsNode</pre></td>
    <td>{{ .IsNode }}</td>
    <td class="small">Always false for regular content pages.</td>
  </tr>
  <tr>
    <td><pre>.IsPage</pre></td>
    <td>{{ .IsPage }}</td>
    <td class="small">Always true for regular content pages.</td>
  </tr>
</table>

<h4>Date</h4>

<table class="table">
  <tr>
    <td><pre>.Date</pre></td>
    <td>{{ .Date }}</td>
    <td class="small">The date the page is associated with.</td>
  </tr>
  <tr>
    <td><pre>.Lastmod</pre></td>
    <td>{{ .Lastmod }}</td>
    <td class="small">The date the content was last modified.</td>
  </tr>
  <tr>
    <td><pre>.PublishDate</pre></td>
    <td>{{ .PublishDate }}</td>
    <td class="small">The date the content is published on.</td>
  </tr>
  <tr>
    <td><pre>.ExpiryDate</pre></td>
    <td>{{ .ExpiryDate }}</td>
    <td class="small">The date where the content is scheduled to expire on.</td>
  </tr>
</table>

<h4>SEO</h4>

<table class="table">
  <tr>
    <td><pre>.Description</pre></td>
    <td>{{ .Description }}</td>
    <td class="small">The description for the page.</td>
  </tr>
  <tr>
    <td><pre>.Keywords</pre></td>
    <td>{{ .Keywords }}</td>
    <td class="small">The meta keywords for this content.</td>
  </tr>
</table>

<h4>Contents</h4>

<table class="table">
  <tr>
    <td><pre>.Content</pre></td>
    <td>(skip)</td>
    <td class="small">The content itself, defined below the front matter.</td>
  </tr>
  <tr>
    <td><pre>.RawContent</pre></td>
    <td>(skip)</td>
    <td class="small">Raw Markdown content without the metadata header. Useful with remarkjs.com</td>
  </tr>
  <tr>
    <td><pre>.TableOfContents</pre></td>
    <td>{{ .TableOfContents }}</td>
    <td class="small">The rendered table of contents for this content.</td>
  </tr>
  <tr>
    <td><pre>.Summary</pre></td>
    <td>{{ .Summary }}</td>
    <td class="small">A generated summary of the content for easily showing a snippet in a summary view. Note that the breakpoint can be set manually by inserting &lt;!--more--&gt; at the appropriate place in the content page. See <a href="https://gohugo.io/content/summaries/">Summaries</a> for more details.</td>
  </tr>
  <tr>
    <td><pre>.Truncated</pre></td>
    <td>{{ .Truncated }}</td>
    <td class="small">A boolean, true if the .Summary is truncated. Useful for showing a “Read more…” link only if necessary. See Summaries for more details.</td>
  </tr>
  <tr>
    <td><pre>.WordCount</pre></td>
    <td>{{ .WordCount }}</td>
    <td class="small">The number of words in the content.</td>
  </tr>
  <tr>
    <td><pre>.FuzzyWordCount</pre></td>
    <td>{{ .FuzzyWordCount }}</td>
    <td class="small">The approximate number of words in the content.</td>
  </tr>
  <tr>
    <td><pre>.ReadingTime</pre></td>
    <td>{{ .ReadingTime }}</td>
    <td class="small">The estimated time it takes to read the content in minutes.</td>
  </tr>
</table>

<h4>Lang</h4>

<table class="table">
  <tr>
    <td><pre>.Lang</pre></td>
    <td>{{ .Lang }}</td>
    <td class="small">Language taken from the language extension notation.</td>
  </tr>
  <tr>
    <td><pre>.Language</pre></td>
    <td>{{ .Language }}</td>
    <td class="small">A language object that points to this the language’s definition in the site config.</td>
  </tr>
  <tr>
    <td><pre>.IsTranslated</pre></td>
    <td>{{ .IsTranslated }}</td>
    <td class="small">Whether there are any translations to display.</td>
  </tr>
  <tr>
    <td><pre>.Translations</pre></td>
    <td>{{ .Translations }}</td>
    <td class="small">A list of translated versions of the current page. See Multilingual for more info.</td>
  </tr>
</table>

<h4>Navigation</h4>

<table class="table">
  <tr>
    <td><pre>.Permalink</pre></td>
    <td>{{ .URL }}</td>
    <td class="small">The Permanent link for this page.</td>
  </tr>
  <tr>
    <td><pre>.RelPermalink</pre></td>
    <td>{{ .RelPermalink }}</td>
    <td class="small">The Relative permanent link for this page.</td>
  </tr>
  <tr>
    <td><pre>.UniqueID</pre></td>
    <td>{{ .UniqueID }}</td>
    <td class="small">The MD5-checksum of the content file’s path</td>
  </tr>
  <tr>
    <td><pre>.Next</pre></td>
    <td>{{ .Next }}</td>
    <td class="small">Pointer to the following content (based on pub date).</td>
  </tr>
  <tr>
    <td><pre>.NextInSection</pre></td>
    <td>{{ .NextInSection }}</td>
    <td class="small">Pointer to the following content within the same section (based on pub date)</td>
  </tr>
  <tr>
    <td><pre>.Prev</pre></td>
    <td>{{ .Prev }}</td>
    <td class="small">Pointer to the previous content (based on pub date).</td>
  </tr>
  <tr>
    <td><pre>.PrevInSection</pre></td>
    <td>{{ .PrevInSection }}</td>
    <td class="small">Pointer to the previous content within the same section (based on pub date). For example, {{if .PrevInSection}}{{.PrevInSection.Permalink}}{{end}}.</td>
  </tr>
</table>

<h4>Other</h4>

<table class="table">
  <tr>
    <td><pre>.Site</pre></td>
    <td>{{ .Site }}</td>
    <td class="small">See Site Variables below.</td>
  </tr>
  <tr>
    <td><pre>.Pages</pre></td>
    <td>{{ .Pages }}</td>
    <td class="small">a collection of associated pages. This will be nil for regular content pages. This is an alias for .Data.Pages.</td>
  </tr>
  <tr>
    <td><pre>.RSSLink</pre></td>
    <td>{{ .RSSLink }}</td>
    <td class="small">Link to the taxonomies’ RSS link.</td>
  </tr>
  <tr>
    <td><pre>.Ref</pre></td>
    <td>(skip)</td>
    <td class="small">Returns the permalink for a given reference. Example: .Ref "sample.md". See cross-references. Does not handle in-page fragments correctly.</td>
  </tr>
  <tr>
    <td><pre>.RelRef</pre></td>
    <td>(skip)</td>
    <td class="small">Returns the relative permalink for a given reference. Example: RelRef "sample.md". See cross-references. Does not handle in-page fragments.</td>
  </tr>
  <tr>
    <td><pre>.Hugo</pre></td>
    <td>{{ .Hugo }}</td>
    <td class="small">See Hugo Variables below.</td>
  </tr>
</table>

File Variables

[ред. | ред. код]
layouts/partials/variables/file.html
<h3>File Variables</h3>

<h4>Main</h4>

<table class="table">
  <tr>
    <td><pre>.File.Path</pre></td>
    <td>{{ .File.Path }}</td>
    <td class="small">The original relative path of the page, e.g. content/posts/foo.en.md</td>
  </tr>
  <tr>
    <td><pre>.File.LogicalName</pre></td>
    <td>{{ .File.LogicalName }}</td>
    <td class="small">The name of the content file that represents a page, e.g. foo.en.md</td>
  </tr>
  <tr>
    <td><pre>.File.TranslationBaseName</pre></td>
    <td>{{ .File.TranslationBaseName }}</td>
    <td class="small">The filename without extension or optional language identifier, e.g. foo</td>
  </tr>
  <tr>
    <td><pre>.File.BaseFileName</pre></td>
    <td>{{ .File.BaseFileName }}</td>
    <td class="small">The filename without extension, e.g. foo.en</td>
  </tr>
  <tr>
    <td><pre>.File.Ext</pre></td>
    <td>{{ .File.Ext }}</td>
    <td class="small">The file extension of the content file, e.g. md</td>
  </tr>
  <tr>
    <td><pre>.File.Extension</pre></td>
    <td>{{ .File.Extension }}</td>
    <td class="small">The file extension of the content file, e.g. md</td>
  </tr>
  <tr>
    <td><pre>.File.Lang</pre></td>
    <td>{{ .File.Lang }}</td>
    <td class="small">The language associated with the given file if Multilingual is enabled, e.g. en</td>
  </tr>
  <tr>
    <td><pre>.File.Dir</pre></td>
    <td>{{ .File.Dir }}</td>
    <td class="small">Given the path content/posts/dir1/dir2/, the relative directory path of the content file will be returned, e.g. posts/dir1/dir2/</td>
  </tr>
</table>

Site Variables

[ред. | ред. код]
layouts/partials/variables/site.html
<h3>Site Variables</h3>

<h4>Main</h4>

<table class="table">
  <tr>
    <td><pre>.Site.Title</pre></td>
    <td>{{ .Site.Title }}</td>
    <td class="small">A string representing the title of the site.</td>
  </tr>
  <tr>
    <td><pre>.Site.BaseURL</pre></td>
    <td>{{ .Site.BaseURL }}</td>
    <td class="small">The base URL for the site as defined in the site configuration file.</td>
  </tr>
  <tr>
    <td><pre>.Site.LastChange</pre></td>
    <td>{{ .Site.LastChange }}</td>
    <td class="small">A string representing the date/time of the most recent change to your site, based on the date variable in the front matter of your content pages.</td>
  </tr>
</table>

<h4>Options</h4>

<table class="table">
  <tr>
    <td><pre>.Site.BuildDrafts</pre></td>
    <td>{{ .Site.BuildDrafts }}</td>
    <td class="small">A boolean (Default: false) to indicate whether to build drafts. Defined in the site configuration.</td>
  </tr>
  <tr>
    <td><pre>.Site.RSSLink</pre></td>
    <td>{{ .Site.RSSLink }}</td>
    <td class="small">The URL for the site RSS.</td>
  </tr>
  <tr>
    <td><pre>.Site.Copyright</pre></td>
    <td>{{ .Site.Copyright }}</td>
    <td class="small">A string representing the copyright of your web site as defined in the site configuration.</td>
  </tr>
  <tr>
    <td><pre>.Site.DisqusShortname</pre></td>
    <td>{{ .Site.DisqusShortname }}</td>
    <td class="small">A string representing the shortname of the Disqus shortcode as defined in the site configuration.</td>
  </tr>
  <tr>
    <td><pre>.Site.GoogleAnalytics</pre></td>
    <td>{{ .Site.GoogleAnalytics }}</td>
    <td class="small">A string representing your tracking code for Google Analytics as defined in the site configuration.</td>
  </tr>
  <tr>
    <td><pre>.Site.Params</pre></td>
    <td>{{ .Site.Params }}</td>
    <td class="small">A container holding the values from the params section of your site configuration file. For example, a TOML config file might look like this:</td>
  </tr>
</table>

<h4>Lang</h4>

<table class="table">
  <tr>
    <td><pre>.Site.LanguageCode</pre></td>
    <td>{{ .Site.LanguageCode }}</td>
    <td class="small">A string representing the language as defined in the site configuration. This is mostly used to populate the RSS feeds with the right language code.</td>
  </tr>
  <tr>
    <td><pre>.Site.Language</pre></td>
    <td>{{ .Site.Language }}</td>
    <td class="small">This indicates which language you are currently rendering the website for. This is an object with the attributes set in your language definition in your site config.</td>
  </tr>
  <tr>
    <td><pre>.Site.Language.Lang</pre></td>
    <td>{{ .Site.Language.Lang }}</td>
    <td class="small">The language code of the current locale, e.g. en.</td>
  </tr>
  <tr>
    <td><pre>.Site.Languages</pre></td>
    <td>{{ .Site.Languages }}</td>
    <td class="small">An ordered list (ordered by defined weight) of languages.</td>
  </tr>
  <tr>
    <td><pre>.Site.IsMultiLingual</pre></td>
    <td>{{ .Site.IsMultiLingual }}</td>
    <td class="small">Whether there are more than one language in this site. See <a href="https://gohugo.io/content/multilingual/">Multilingual</a> for more info.</td>
  </tr>
  <tr>
    <td><pre>.Site.Language.Weight</pre></td>
    <td>{{ .Site.Language.Weight }}</td>
    <td class="small">The weight that defines the order in the .Site.Languages list.</td>
  </tr>
  <tr>
    <td><pre>.Site.Language.LanguageName</pre></td>
    <td>{{ .Site.Language.LanguageName }}</td>
    <td class="small">The full language name, e.g. English.</td>
  </tr>
  <tr>
    <td><pre>.Site.LanguagePrefix</pre></td>
    <td>{{ .Site.LanguagePrefix }}</td>
    <td class="small"> This can be used to prefix theURLs with whats needed to point to the correct language. It will even work when only one language defined. See also the functions absLangURL and relLangURL.</td>
  </tr>
</table>

  <h4>Other</h4>

<table class="table">
  <tr>
    <td><pre>.Site.Pages</pre></td>
    <td>{{ .Site.Pages }}</td>
    <td class="small">Array of all content ordered by Date, newest first. Replaces the now-deprecated .Site.Recent starting v0.13. This array contains only the pages in the current language.</td>
  </tr>
  <tr>
    <td><pre>.Site.AllPages</pre></td>
    <td>{{ .Site.AllPages }}</td>
    <td class="small">Array of all pages regardless of their translation.</td>
  </tr>
  <tr>
    <td><pre>.Site.RegularPages</pre></td>
    <td>{{ .Site.RegularPages }}</td>
    <td class="small">A shortcut to the regular page collection. Equivalent to where .Site.Pages "Kind" "page".</td>
  </tr>
  <tr>
    <td><pre>.Site.Sections</pre></td>
    <td>(skip)</td>
    <td class="small">Top level directories of the site.</td>
  </tr>
  <tr>
    <td><pre>.Site.Taxonomies</pre></td>
    <td>{{ .Site.Taxonomies }}</td>
    <td class="small">The taxonomies for the entire site. Replaces the now-obsolete .Site.Indexes since v0.11. Also see section Taxonomies elsewhere.</td>
  </tr>
  <tr>
    <td><pre>.Site.Files</pre></td>
    <td>{{ .Site.Files }}</td>
    <td class="small">All of the source files of the site.</td>
  </tr>
  <tr>
    <td><pre>.Site.Menus</pre></td>
    <td>{{ .Site.Menus }}</td>
    <td class="small">All of the menus in the site.</td>
  </tr>
  <tr>
    <td><pre>.Site.Author</pre></td>
    <td>{{ .Site.Author }}</td>
    <td class="small">A map of the authors as defined in the site configuration.</td>
  </tr>
  <tr>
    <td><pre>.Site.Permalinks</pre></td>
    <td>{{ .Site.Permalinks }}</td>
    <td class="small">A string to override the default permalink format. Defined in the site configuration.</td>
  </tr>
  <tr>
    <td><pre>.Site.Data</pre></td>
    <td>{{ .Site.Data }}</td>
    <td class="small">Custom data, see <a href="https://gohugo.io/extras/datafiles/">Data Files</a>.</td>
  </tr>
</table>

Hugo Variables

[ред. | ред. код]
layouts/partials/variables/hugo.html
<h3>Hugo Variables</h3>

<table class="table">
  <tr>
    <td><pre>.Hugo.Generator</pre></td>
    <td>{{ .Hugo.Generator }}</td>
    <td class="small">Meta tag for the version of Hugo that generated the site. Highly recommended to be included by default in all theme headers so we can start to track the usage and popularity of Hugo. Unlike other variables it outputs a complete HTML tag, e.g. &lt;meta name="generator" content="Hugo 0.15" /&gt;</td>
  </tr>
  <tr>
    <td><pre>.Hugo.Version</pre></td>
    <td>{{ .Hugo.Version }}</td>
    <td class="small">The current version of the Hugo binary you are using e.g. 0.13-DEV</td>
  </tr>
  <tr>
    <td><pre>.Hugo.CommitHash</pre></td>
    <td>{{ .Hugo.CommitHash }}</td>
    <td class="small">The git commit hash of the current Hugo binary e.g. 0e8bed9ccffba0df554728b46c5bbf6d78ae5247</td>
  </tr>
  <tr>
    <td><pre>.Hugo.BuildDate</pre></td>
    <td>{{ .Hugo.BuildDate }}</td>
    <td class="small">The compile date of the current Hugo binary formatted with RFC 3339 e.g. 2002-10-02T10:00:00-05:00</td>
  </tr>
</table>