Themes.pm 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. # For style we want to load *both* style files and have the override come later.
  36. sub themed_style ($resource) {
  37. my @styles = ("/styles/$resource");
  38. my $styled = _dir_for_resource("styles/$resource");
  39. $styled =~ s/^www\///;
  40. push( @styles, "/$styled/styles/$resource" ) if $styled;
  41. return @styles;
  42. }
  43. sub themed_script ($resource) {
  44. return _dir_for_resource("scripts/$resource") . "/scripts/$resource";
  45. }
  46. sub themed_template ($resource) {
  47. return _dir_for_resource("templates/$resource") . "/templates/$resource";
  48. }
  49. sub templates_in_dir ( $path, $ct, $is_component = 0 ) {
  50. $path = template_dir( $path, $ct, $is_component, 1 ) . "/$path";
  51. my $forms = [];
  52. return $forms unless -d $path;
  53. opendir( my $dh, $path );
  54. while ( my $form = readdir($dh) ) {
  55. push( @$forms, $form ) if -f "$path/$form" && $form =~ m/.*\.tx$/;
  56. }
  57. close($dh);
  58. return $forms;
  59. }
  60. 1;