diff options
author | Yorhel <git@yorhel.nl> | 2017-12-16 13:20:49 +0100 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2017-12-16 13:20:49 +0100 |
commit | d77958674603a09c1d8a702dbbe876e10e2c51b6 (patch) | |
tree | 18c6a6baa3285827657fe2ff8b198b1274ba98b1 /lib/TUWF | |
parent | d185f54bdeaeb5af427732ffaec9e986920ee333 (diff) |
TUWF::XML: Add "mkclass" utility function
Diffstat (limited to 'lib/TUWF')
-rw-r--r-- | lib/TUWF/XML.pm | 10 | ||||
-rw-r--r-- | lib/TUWF/XML.pod | 18 |
2 files changed, 27 insertions, 1 deletions
diff --git a/lib/TUWF/XML.pm b/lib/TUWF/XML.pm index 43208ae..77a3857 100644 --- a/lib/TUWF/XML.pm +++ b/lib/TUWF/XML.pm @@ -56,7 +56,7 @@ BEGIN { my @htmlexport = (qw| html Html lit txt tag end |); my @xmlexport = qw| xml lit txt tag end |; - @EXPORT_OK = uniq @htmlexport, @html5tags, @htmltags, @xmlexport, 'xml_escape', 'html_escape'; + @EXPORT_OK = uniq @htmlexport, @html5tags, @htmltags, @xmlexport, 'mkclass', 'xml_escape', 'html_escape'; %EXPORT_TAGS = ( html => [ @htmlexport, @htmltags ], html5 => [ @htmlexport, @html5tags ], @@ -96,6 +96,14 @@ sub new { }; +# Convenient function to generate a dynamic class attribute. +sub mkclass { + my %c = @_; + my $c = join ' ', grep $c{$_}, keys %c; + return $c ? (class => $c) : (); +} + + # XML escape (not a method) sub xml_escape { local $_ = shift; diff --git a/lib/TUWF/XML.pod b/lib/TUWF/XML.pod index 800e1f7..f335d23 100644 --- a/lib/TUWF/XML.pod +++ b/lib/TUWF/XML.pod @@ -112,6 +112,24 @@ the control on where to (not) insert whitespace. Default: 0 (disabled). =back +=head2 mkclass(%classes) + +Dynamically constructs a I<class> attribute, which can be passed to C<tag()> +and friends. This function accepts a hash where the keys are the class names +and the value indicates whether the class is enabled or not. This function +returns an empty list if none of the classes are enabled, or returns +C<< (class => $list_of_enabled_classes) >>. + +This is convenient when the classes are dependant on other variables, e.g.: + + tag 'div', mkclass(hidden => $is_hidden, warning => $is_warning), 'Text'; + # Output: + # !$is_hidden && !$is_warning: <div>Text</div> + # $is_hidden && !$is_warning: <div class="hidden">Text</div> + # $is_hidden && $is_warning: <div class="hidden warning">Text</div> + +Note that the order in which classes are returned may be somewhat random. + =head2 xml_escape(string) Returns the XML-escaped string. The characters C<&>, C<E<lt>>, C<E<gt>> and |