Space doesn’t matter, except when it does.
The other day Koha bug 11308 was filed reporting a problem with the public catalog search RSS feed. This affected just the new Bootstrap theme.
The bug report noted that when clicking on the RSS feed icon, the page rendered “not like an rss feed should”. That means different things to different web browsers, but we can use an RSS feed validation service like validator.w3.org to see what feed parsers are likely to think.
Before the bug was fixed, the W3C validator reported this:
This feed does not validate.
line 2, column 0: XML parsing error: :2:0: XML or text declaration not at start of entity [help]
<?xml version=’1.0′ encoding=’utf-8′ ?>
Of course, at first glance, the XML declaration looks just fine — the key bit is that it is starting at the second line of the response.
Space matters — XML requires that if an XML declaration is present, it must be the very first thing in the document.
Let’s take a look at the patch, written by Chris Cormack, that fixes the bug:
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-opensearch.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-opensearch.tt @@ -1,5 +1,5 @@ -[% USE Koha %] +[% USE Koha %] [% IF ( opensearchdescription ) %][% LibraryName |html %] Search
This patch moves the [% USE Koha %]
Template Toolkit directive from before the XML declaration to after it. [% USE Koha %]
loads a custom Template Toolkit module called “Koha”; further down in the template there is a use of Koha.Preference()
to check the value of a system preference.
But why should importing a TT module add a blank line? By default, Template Toolkit will include all of the whitespace present in the template. Since there is a newline after the [% USE Koha %]
directive, that newline is included in the response.
Awkward, when spaces matter.
However, Template Toolkit does have a way to chomp whitespace before or after template directives.
This means that an alternative fix could be something like this:
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-opensearch.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-opensearch.tt @@ -1,4 +1,4 @@ -[% USE Koha %] +[% USE Koha -%]
Adding a single hyphen here means that whitespace after the TT directive should be chomped — in other words, not included in the response.
Most of the time, extra whitespace doesn’t matter for the HTML emitted by Koha. But when space matters… you can use TT to control it.
Koha Dev Tidbit #1: control of space by Galen Charlton is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
One thought on “Koha Dev Tidbit #1: control of space”
Comments are closed.