Skip to content

Commit

Permalink
Added header_var twig function
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Dec 17, 2017
1 parent a1c804e commit 3e15de7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

1. [](#new)
* Transferred overall copyright from RocketTheme, LLC, to Trilby Media LLC
* Added `theme_var` and `body_class` Twig functions that are useful for themes
* Added `theme_var`, `header_var` and `body_class` Twig functions for themes
1. [](#improved)
* Updated vendor libraries to latest versions
* Removed constructor from ObjectInterface
Expand Down
34 changes: 34 additions & 0 deletions system/src/Grav/Common/Twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public function getFunctions()
new \Twig_SimpleFunction('media_directory', [$this, 'mediaDirFunc']),
new \Twig_SimpleFunction('body_class', [$this, 'bodyClassFunc']),
new \Twig_SimpleFunction('theme_var', [$this, 'themeVarFunc']),
new \Twig_SimpleFunction('header_var', [$this, 'pageHeaderVarFunc']),

];
}
Expand Down Expand Up @@ -1162,4 +1163,37 @@ public function bodyClassFunc($classes)

return $body_classes;
}

/**
* Look for a page header variable in an array of pages working its way through until a value is found
*
* @param $var
* @param null $pages
* @return mixed
*/
public function pageHeaderVarFunc($var, $pages = null)
{
if ($pages === null) {
$pages = $this->grav['page'];
}

// Make sure pages are an array
if (!is_array($pages)) {
$pages = array($pages);
}

// Loop over pages and look for header vars
foreach ($pages as $page) {
if (is_string($page)) {
$page = $this->grav['pages']->find($page);
}

if ($page) {
$header = $page->header();
if (isset($header->$var)) {
return $header->$var;
}
}
}
}
}

0 comments on commit 3e15de7

Please sign in to comment.