Themes.pm 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package Trog::Themes;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures state};
  6. use Trog::Vars;
  7. use Trog::Config;
  8. =head1 Trog::Themes
  9. Utility functions for getting themed paths.
  10. =cut
  11. my $conf = Trog::Config::get();
  12. our $template_dir = 'www/templates';
  13. our $theme_dir = '';
  14. $theme_dir = "themes/" . $conf->param('general.theme') if $conf->param('general.theme') && -d "www/themes/" . $conf->param('general.theme');
  15. our $td = $theme_dir ? "/$theme_dir" : '';
  16. sub template_dir ($template, $content_type, $is_component=0, $is_dir=0) {
  17. my $ct = $Trog::Vars::byct{$content_type};
  18. my ($mtd, $mtemp) = ("$theme_dir/$ct", "$template_dir/$ct");
  19. if ($is_component) {
  20. $mtd .= "/components";
  21. $mtemp .= "/components";
  22. }
  23. if ($is_dir) {
  24. return $mtd && -d "www/$mtd/$template" ? $mtd : $mtemp;
  25. }
  26. return $mtd && -f "www/$mtd/$template" ? $mtd : $mtemp;
  27. }
  28. # Pick appropriate dir based on whether theme override exists
  29. sub _dir_for_resource ($resource) {
  30. return $theme_dir && -f "www/$theme_dir/$resource" ? $theme_dir : '';
  31. }
  32. sub themed_style ($resource) {
  33. return _dir_for_resource("styles/$resource") . "/styles/$resource";
  34. }
  35. sub themed_script ($resource) {
  36. return _dir_for_resource("scripts/$resource") . "/scripts/$resource";
  37. }
  38. sub themed_template ($resource) {
  39. return _dir_for_resource("templates/$resource") . "/templates/$resource";
  40. }
  41. sub templates_in_dir ($path, $ct, $is_component=0) {
  42. $path = template_dir($path, $ct, $is_component, 1)."/$path";
  43. my $forms = [];
  44. return $forms unless -d $path;
  45. opendir( my $dh, $path );
  46. while ( my $form = readdir($dh) ) {
  47. push( @$forms, $form ) if -f "$path/$form" && $form =~ m/.*\.tx$/;
  48. }
  49. close($dh);
  50. return $forms;
  51. }
  52. 1;