document evaltag
[bse.git] / site / cgi-bin / modules / BSE / Variables.pm
CommitLineData
234312d8
TC
1package BSE::Variables;
2use strict;
3use Scalar::Util qw(blessed);
69dfe3a1 4use BSE::TB::Site;
234312d8 5
69dfe3a1 6our $VERSION = "1.001";
234312d8
TC
7
8sub variables {
9 my ($self, %opts) = @_;
10 my $site;
11 return
12 {
13 site => BSE::TB::Site->new,
14 url =>
15 ($opts{admin} || $opts{admin_links}
16 ? sub { $_[0]->admin }
17 : sub { $_[0]->link }
18 ),
19 admin => $opts{admin},
20 admin_links => $opts{admin_links},
21 dumper => sub {
22 require Data::Dumper;
23 return escape_html(Data::Dumper::Dumper(shift));
24 },
25 categorize_tags => \&_categorize_tags,
26 };
27}
28
29sub _categorize_tags {
30 my ($tags) = @_;
31
32 $DB::single = 1;
33 require BSE::TB::Tags;
34 my %cats;
35 for my $tag (@$tags) {
36 my $work = blessed $tag ? $tag->json_data : $tag;
37 my $cat = lc $tag->{cat};
38 unless ($cats{$cat}) {
39 $cats{$cat} =
40 {
41 name => $tag->{cat},
42 tags => [],
43 };
44 }
45 push @{$cats{$cat}{tags}}, $tag;
46 }
47
48 for my $cat (values %cats) {
49 @{$cat->{tags}} = sort { lc $a->{val} cmp lc $b->{val} } @{$cat->{tags}};
50 }
51
52 return [ sort { lc $a->{name} cmp $b->{name} } values %cats ];
53}
54
551;
56
57=head1 NAME
58
59BSE::Variables - commonly set variables
60
61=head1 SYNOPSIS
62
63 # in perl code
64 require BSE::Variables;
65 $foo->set_variable(bse => BSE::Variables->variables(%opts));
66
67 # in templates
68 <:.set level1 = bse.site.children :>
69 <:= url(article) | html :>
70 <:= tagcats = bse.categorize_tags(article.tag_objects) :>
71 <:.if bse.admin:>...
72 <:= dumper(somevar) :> lots of noise
73
74=head1 DESCRIPTION
75
76Common BSE functionality for use from the new template tags.
77
78=over
79
80=item bse.site
81
82a BSE::TB::Site object, behaves like an article in owning files and
83images, and having children.w
84
85=item bse.url(somearticle)
86
87Return the article admin link in admin (or admin_links) mode,
88otherwise the normal article link.
89
90=item bse.admin
91
92Return true in admin mode.
93
94=item bse.admin_links
95
96Return true in admin_links mode
97
98=item dumper(value)
99
100Dump the value in perl syntax using L<Data::Dumper>.
101
102=item categorize_keys(tags)
103
104Returns the given tags as a list of tag categories, each category has
105a name (of the category) and a list of tags in that category.
106
107=back
108
109=head1 AUTHOR
110
111Tony Cook <tony@develop-help.com>
112
113=cut