Grav-主题变量 


Grav-主题变量


在本章中,让我们了解Grav中的主题变量。在设计主题时,可以从树枝模板访问对象和变量,而这些对象和变量由Twig模板框架读取和操作。

核心对象

Twig模板具有许多核心对象。每个对象都有一组变量函数

下表显示了带有简要说明的变量。

Sr.No. Variable & Description
1

{{ base_dir }}

We can get Grav installations base file directory by using this variable.

2

{{ base_url }}

The base url of the Grav site can be accessed by using this variable.

3

{{ base_url_relative }}

It returns the relative path of the base url to Grav site.

4

{{ base_url_absolute }}

It returns the absolute path of the base url to Grav site.

5

{{ theme_dir }}

It is used to return the current theme’s file directory folder.

6

{{ theme_url }}

It is used to return current theme’s relative URL.

配置对象

configuration.yaml文件中,将config对象设置为访问任何Grav配置设置。

{{ config.pages.theme }}

它将返回当前配置的主题。

网站对象

config.site对象提供别名,表示在site.yaml文件中设置的配置。

样式表对象

这些对象为要存储的CSS样式表资产提供了一个数组,可以循环将其添加到模板中。

脚本对象

这些对象提供了一个由JavaScript资产组成的数组,该数组被循环并且JavaScript被添加到模板中。

页面对象

由于Grav的结构是在pages /文件夹中定义的,因此page对象负责表示每个页面。页面对象包含有关您当前正在处理的页面的所有信息。

下表显示了页面对象的方法。

Sr.No. Method & Description Example
1

summary([size])

It gives a gist of the page content with the specified size provided as a parameter.

If size is not specified then the value is obtained from summary.size variable in system/config/site.yaml file.

You can also delimiter === in your content. The content before the delimiter will be used for summary.

{{ page.summary }}

Or

{{ page.summary(80) }}
2

content()

It is used to get the entire HTML content of the page.

{{ page.content }}
3

headers()

It returns the page headers defined in the YAML front-matter of the page.

title: About Us
author: Johnson

The above headers can be accessed as:

The author of this page is:  {{ page.header.author }}
4

media()

It is used to access all the media files such as images, videos and other files. It will return an array containing all the media associated with a page.

{% set first_image = page.media|first %}
{% set my_pdf = page.media['myfile.pdf'] %}
{% for image in page.media.images  %}
   {{ image.html }}
{% endfor %}
5

title()

It is set to return the title of the page which is defined in YAML headers for the page.

title: My Page
6

menu()

The value of the menu variable is returned which is specified in YAML headers of the page. If the title variable is not set, then it will default to title.

title: My Blog
menu: my blog page
7

visible()

It is used to set the visibility of the page. Usually pages with numeric value followed by period (i.e., 01.foldername) is displayed in menu and the folder name that does not contain numeric value (i.e., foldername) are not visible. We can override it in page header.

title: About Us
visible: true
8

routable()

By using this we can determine whether a page is routable or not routable meaning that whether you can receive content back while pointing your browser to the page. The pages which are not routable can be used in plugins, templates, etc., and these pages cannot be directly accessed. This is set in page headers.

title: My Page
routable: true
9

slug()

By using this variable, we can get direct name as displayed in the URL of the page.

my-page
10

url([include_host = false])

It is used to return the page’s URL

{{ page.url }}  { # could return /myfolder/mypage #}

Or

{{ page.url(true) }}  {# could return http: //mysite.com/ myfolder/mypage #}
11

route()

It is used to return the internal routing of the page.

12

home()

Using this variable you can determine whether the page is configured as home page or not. This returns true when a page is configured as home page and false when not configured. You can find this setting in system.yaml file.

13

root()

It determines whether the current page is the root page of the hierarchy or not. It returns true if it’s a root page or false if it’s not root page.

14

active()

You can determine whether the browser is accessing the current page by using this variable. It returns true if browser is accessing this page or false if it’s not.

15

modular()

By using this variable, we can determine whether this page is modular or not. If this is a modular page then it returns true and false if it’s not.

16

activeChild()

This variable can determine whether this URI’s URL has the URL of the active page; or, in simple words this page’s URL in current URL. This is very useful when you are working on navigations and you want to know that whether the pages are iterating over same parent page.

17

find(url)

As specified by the route URL, the page object is returned by this variable.

{% include 'modular/author-detail.html.twig' with {'page': page.find('/authors/ john-bloggs')} %}
18

collection()

This variable is used to return the group of pages for a context as determined by the collection page headers.

{% for child in page.collection %}
   {% include 'partials /blog_item.html.twig' with {'page':child, 'truncate':true} %}
{% endfor %}
19

isFirst()

If the current page is first of it’s sibling page, then it returns true else returns false.

20

isLast()

If the current page is last of it’s sibling page, then it returns true else returns false.

21

nextSibling()

With reference to the current position, it returns the next sibling page from the array.

22

prevSibling()

With reference to the current position, it returns the previous sibling page from the array.

23

children()

As defined in the pages content structure, the array of child pages is returned by this variable.

24

orderBy()

The sorted children’s order type is returned by this method. The values that may be included are default, title, date and folder and these values configured in page headers.

25

orderDir()

The sorted child pages’ order direction is returned by this method. And the values can be either asc(ascending) or desc(descending). Usually these values are configured in page headers.

26

orderManual()

This method returns an array consisting of manual page ordering and this ordering will be for any children of the page. This value will be typically set in page headers.

27

maxCount()

This variable tells that at most how many children pages are allowed to be returned. Usually the value is specified in page headers.

28

children.count()

This variable returns how many child pages are there for a page.

29

children.current()

This variable will return the current child item.

30

children.next()

This will return the next child item from an array of child pages.

31

children.prev()

This will return the previous child item from an array of child pages.

32

children.nth(position)

This will return the position of the child in the array of children.

33

parent()

In a nested tree structure when you want to navigate back up to the parent page, then you can use this variable. It will return the parent page object for the current page.

34

isPage()

By using this variable, you can determine whether this page has an actual .md file or it’s just a folder for routing.

35

isDir()

By using this variable you can determine whether current page is only a folder for routing. It returns true or false based on it.

36

id()

This will return unique id for the page.

37

modified()

It returns the timestamp of when the page was last modified.

38

date()

The date timestamp for the page is returned by this method. Usually this is configured in headers that represents the page’s or post’s date. If no value is provided by default the modified timestamp is used.

39

filePath()

By using this, you can get the full file path of the page.

/Users/yourname/sites/ Grav/user/pages/ 01.home/default.md
40

filePathClean()

This will return the relative path.

user/pages/ 01.home/default.md
41

path()

This will return a full path to the directory in which the current page is present.

/Users/yourname /sites/ Grav/user/pages /01.home
42

folder()

This will return the folder name for the page.

43

taxonomy()

This will return an array of taxonomy which is connected with the page.

页面对象

Pages对象表示为页面对象的嵌套树。在创建导航,站点地图查找特定页面时此嵌套树非常有用。

儿童方法

这将返回由子页面组成的页面对象数组。具有树状结构的页面对象可以在文件夹中的每个页面上进行迭代。

为了获得菜单的顶层页面,请使用以下代码。

<ul class="navigation">
   {% for page in pages.children %}
      {% if page.visible %}
         <li><a href="%7B%7B%20page.url%20%7D%7D">{{ page.menu }}</a></li>
      {% endif %}
   {% endfor %}
</ul>

uri对象

可以使用Uri对象的几种方法来访问当前URI的一部分。

http://mysite.com/Grav/section/category/page.json/param1:foo/param2:bar/?query1 = baz&query2 = qux:

下表显示了Uri对象的方法。

Sr.No. Method & Description Example
1

path()

The part of the current url can be accessed by using this method.

uri.path = /section/category/page
2

paths()

The array of path elements is returned by using this method.

uri.paths = [section, category, page])
3

route([absolute = false][, domain = false])

This method returns route with either absolute or relative URL.

uri.route(true) = http://mysite.com/Grav/ section/category/page

Or,

uri.route() = /section/category/page)
4

params()

This will return the parameter portion in the URL.

uri.params = /param1:foo/param2:bar
5

param(id)

This will return the value of the param.

uri.param('param1') = foo
6

query()

The query portion of the URL can be accessed by using this method.

uri.query = query1=bar&query2=qux
7

query(id)

Using this you can access the specific query item.

uri.query('query1') = bar
8

url([include_host = true])

This returns the full URL which may or may not contain host.

uri.url(false) = Grav/section/ category/page/param:foo?query = bar
9

extension()

This will return the extension or if not provided, then it will return the html.

uri.extension = json)
10

host()

This return the host of the URL.

uri.host = mysite.com
11

base()

This will return the base part of the URL.

uri.base = http://mysite.com
12

rootUrl([include_host = true])

This will return the Grav instance’s root URL.

uri.rootUrl() = http://mysite.com/Grav
13

referrer()

The referrer information of the page is returned by this method.

标头对象

它是原始页面的page.header()的替代方法。遍历子页面时,更适合使用原始页面标题。

内容对象

它是原始页面的page.content()的替代方法。

分类对象

该站点的所有分类信息都包含在全局分类对象中。

浏览器对象

Grav通过使用内置支持以编程方式确定用户的平台,浏览器和版本。

{{ browser.platform}}   # windows
{{ browser.browser}}    # chrome
{{ browser.version}}    # 24

添加自定义变量

自定义变量以几种方式添加。如果您使用站点范围的变量,则将其放在user / config / site.yaml文件中,您可以如下所示访问它。

{{ site.my_variable }}

如果变量仅用于特定页面,则可以将其添加到YAML前端,并使用page.header对象进行访问。

例如

title: My Page
author: John

作者名称可以访问-

The author of this page is: {{ page.header.author }}

添加自定义对象

通过使用插件,可以将自定义对象添加到Twig对象。这是一个高级主题,我们将在插件一章中看到更多信息。

0



error: 内容受到保护 !!