Themes.pm 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package Trog::Themes;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures state};
  6. use FindBin::libs;
  7. use Trog::Vars;
  8. use Trog::Config;
  9. =head1 Trog::Themes
  10. Utility functions for getting themed paths.
  11. =cut
  12. our $template_dir = 'www/templates';
  13. sub routes {
  14. my $rdir = get_dir();
  15. return () unless -f "$rdir/routes.pm";
  16. local $@;
  17. eval { require "$rdir/routes.pm"; 1; } or do {
  18. die "Could not load Theme routing package: $@";
  19. };
  20. return %Theme::routes;
  21. }
  22. sub get_dir {
  23. state $tdir = '';
  24. return $tdir if $tdir;
  25. my $conf = Trog::Config::get();
  26. my $theme = $conf->param('general.theme') || '';
  27. if( $theme ) {
  28. my $themedir = "www/themes/$theme";
  29. $tdir = $themedir if -d $themedir;
  30. }
  31. return $tdir;
  32. };
  33. sub td {
  34. my $dir = get_dir();
  35. return $dir ? "/$dir" : '';
  36. }
  37. sub template_dir ( $template, $content_type, $is_component = 0, $is_dir = 0 ) {
  38. my $ct = $Trog::Vars::byct{$content_type};
  39. my ( $mtd, $mtemp ) = ( get_dir() . "/templates/$ct", "$template_dir/$ct" );
  40. if ($is_component) {
  41. $mtd .= "/components";
  42. $mtemp .= "/components";
  43. }
  44. if ($is_dir) {
  45. return $mtd && -d "$mtd/$template" ? $mtd : $mtemp;
  46. }
  47. return $mtd && -f "$mtd/$template" ? $mtd : $mtemp;
  48. }
  49. # Pick appropriate dir based on whether theme override exists
  50. sub _dir_for_resource ($resource) {
  51. my $theme_dir = get_dir();
  52. return $theme_dir && -f "$theme_dir/$resource" ? $theme_dir : '';
  53. }
  54. sub themed ($resource) {
  55. return _dir_for_resource("$resource") . "/$resource";
  56. }
  57. # For style we want to load *both* style files and have the override come later.
  58. sub themed_style ($resource) {
  59. my @styles = ("/styles/$resource");
  60. my $styled = _dir_for_resource("styles/$resource");
  61. $styled =~ s/^www\///;
  62. push( @styles, "/$styled/styles/$resource" ) if $styled;
  63. return @styles;
  64. }
  65. sub themed_script ($resource) {
  66. return _dir_for_resource("scripts/$resource") . "/scripts/$resource";
  67. }
  68. sub themed_template ($resource) {
  69. return _dir_for_resource("templates/$resource") . "/templates/$resource";
  70. }
  71. sub templates_in_dir ( $path, $ct, $is_component = 0 ) {
  72. $path = template_dir( $path, $ct, $is_component, 1 ) . "/$path";
  73. my $forms = [];
  74. return $forms unless -d $path;
  75. opendir( my $dh, $path );
  76. while ( my $form = readdir($dh) ) {
  77. push( @$forms, $form ) if -f "$path/$form" && $form =~ m/.*\.tx$/;
  78. }
  79. close($dh);
  80. return $forms;
  81. }
  82. 1;