AdminTwigExtension.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace Grav\Plugin;
  3. use \Grav\Common\Grav;
  4. use Symfony\Component\Yaml\Yaml;
  5. use Symfony\Component\Yaml\Dumper;
  6. class AdminTwigExtension extends \Twig_Extension
  7. {
  8. protected $grav;
  9. public function __construct()
  10. {
  11. $this->grav = Grav::instance();
  12. $this->lang = $this->grav['user']->language;
  13. }
  14. /**
  15. * Returns extension name.
  16. *
  17. * @return string
  18. */
  19. public function getName()
  20. {
  21. return 'AdminTwigExtension';
  22. }
  23. public function getFilters()
  24. {
  25. return [
  26. new \Twig_SimpleFilter('tu', [$this, 'tuFilter']),
  27. new \Twig_SimpleFilter('toYaml', [$this, 'toYamlFilter']),
  28. new \Twig_SimpleFilter('fromYaml', [$this, 'fromYamlFilter']),
  29. ];
  30. }
  31. public function tuFilter()
  32. {
  33. return $this->grav['admin']->translate(func_get_args(), [$this->grav['user']->authenticated ? $this->lang : 'en']);
  34. }
  35. public function toYamlFilter($value, $inline = true)
  36. {
  37. return Yaml::dump($value, $inline);
  38. }
  39. public function fromYamlFilter($value)
  40. {
  41. $yaml = new Parser();
  42. return $yaml->parse($value);
  43. }
  44. }