Themes.pm 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 = "www/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/templates/$ct", "$template_dir/$ct" );
  19. if ($is_component) {
  20. $mtd .= "/components";
  21. $mtemp .= "/components";
  22. }
  23. if ($is_dir) {
  24. return $mtd && -d "$mtd/$template" ? $mtd : $mtemp;
  25. }
  26. return $mtd && -f "$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 "$theme_dir/$resource" ? $theme_dir : '';
  31. }
  32. sub themed ($resource) {
  33. return _dir_for_resource("$resource") . "/$resource";
  34. }
  35. sub themed_style ($resource) {
  36. return _dir_for_resource("styles/$resource") . "/styles/$resource";
  37. }
  38. sub themed_script ($resource) {
  39. return _dir_for_resource("scripts/$resource") . "/scripts/$resource";
  40. }
  41. sub themed_template ($resource) {
  42. return _dir_for_resource("templates/$resource") . "/templates/$resource";
  43. }
  44. sub templates_in_dir ( $path, $ct, $is_component = 0 ) {
  45. $path = template_dir( $path, $ct, $is_component, 1 ) . "/$path";
  46. my $forms = [];
  47. return $forms unless -d $path;
  48. opendir( my $dh, $path );
  49. while ( my $form = readdir($dh) ) {
  50. push( @$forms, $form ) if -f "$path/$form" && $form =~ m/.*\.tx$/;
  51. }
  52. close($dh);
  53. return $forms;
  54. }
  55. 1;