Skip to content

Instantly share code, notes, and snippets.

@aarsla
Last active July 26, 2017 07:20
Show Gist options
  • Save aarsla/0574f28eaa06273058e0e0f253397e90 to your computer and use it in GitHub Desktop.
Save aarsla/0574f28eaa06273058e0e0f253397e90 to your computer and use it in GitHub Desktop.
Twig Filter - git commit/branch
<?php
namespace App\Twig\Extension;
class GitExtension extends \Twig_Extension
{
public function getFilters()
{
return array(
new \Twig_SimpleFilter('gitVersion', array($this, 'showCustomGitVersion')),
);
}
/**
* Get current git version
*/
public function showCustomGitVersion()
{
$hash = exec('git rev-parse --short HEAD');
$branch = exec('git rev-parse --abbrev-ref HEAD');
$tag = exec('git describe');
return $tag. ' ' . $hash . ' on '.$branch;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'twig_git';
}
}
@aarsla
Copy link
Author

aarsla commented Jul 17, 2017

Usage: version: {{ ''|showCustomGitVersion() }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment