Skip to content

Commit

Permalink
Added some new theme twig functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Dec 14, 2017
1 parent 592c3ea commit aadb8bb
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions system/src/Grav/Common/Twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ public function getFunctions()
new \Twig_SimpleFunction('isajaxrequest', [$this, 'isAjaxFunc']),
new \Twig_SimpleFunction('exif', [$this, 'exifFunc']),
new \Twig_SimpleFunction('media_directory', [$this, 'mediaDirFunc']),
new \Twig_SimpleFunction('body_class', [$this, 'bodyClassFunc']),
new \Twig_SimpleFunction('theme_var', [$this, 'themeVarFunc']),

];
}
Expand Down Expand Up @@ -1124,4 +1126,40 @@ public function niceNumberFunc($n)

return number_format($n);
}

/**
* Get a theme variable
*
* @param $var
* @return string
*/
public function themeVarFunc($var)
{
return $this->config->get('theme.' . $var, false) ?: '';
}

/**
* takes an array of classes, and if they are not set on body_classes
* look to see if they are set in theme config
*
* @param $classes
* @return string
*/
public function bodyClassFunc($classes)
{

$header = $this->grav['page']->header();
$body_classes = isset($header->body_classes) ? $header->body_classes : '';

foreach ((array)$classes as $class) {
if (!empty($body_classes) && Utils::contains($body_classes, $class)) {
continue;
} else {
$val = $this->config->get('theme.' . $class, false) ? $class : false;
$body_classes .= $val ? ' ' . $val : '';
}
}

return $body_classes;
}
}

0 comments on commit aadb8bb

Please sign in to comment.