Lawliet has asked for the wisdom of the Perl Monks concerning the following question:

A wild question appears!
Trainer sends out Monastery Monk!
The wild question attacks first!

When using HTML::Template along with DBI (and sundry other modules), I ran into an annoying annoyance (hard to believe, I know.)

I have my template set up so that the only variable ('only' meaning 'relating to the annoying annoyance') is the body of the webpage. In other words, the template is the whole HTML file minus the main content (that is, the template has the navigation bar, and so on.)

"Whence do you obtain that sole variable?" asks Monastery Monk.

Ah, good question. I connect to a database that includes (but is not limited to) the main content of every page on the site in a table. This is where problems begin to arise.

But wait! Hold off asking for my codez! Don't bother inquiring about what I use to get this datum! For that is not where the problem lies. The annoyance you keep eagerly hearing is how HTML::Template outputs the main content. Not on the page itself though, no, but in it's source! Because the HTML tags in the variable I retrieve from the table in the database are simply copy/pasted from an actual HTML document, the formatting is all nice, neat, and nested (whilst viewed in a text editor.) In the source of the webpage, the formatting is all wrong! Let me treat you to an example:

<body> <div id="body"> <img alt="alttext" src="/img/heading.gif" /> <div id="extra"><i><font color="#818689" size="4">sometext</font>< +/i></div> <div id="main"> <b class="pagename">Title Text</b> <br /> <br /> <p>Text goes here.</p> <p>More text!</p> <!-- Problem lies here --> <p>MOAR TEXT!!11!!</p> <!-- and here --> </div> </div>

The problem is that the latter paragraphs are not nested properly. Here is my template code:

<body> <div id="body"> <img alt="alttext" src="/img/heading.gif" /> <div id="extra"><i><font color="#818689" size="4">sometext</font>< +/i></div> <div id="main"> <b class="pagename"><TMPL_VAR NAME='PAGENAME'></b> <br /> <br /> <TMPL_VAR NAME='PAGEBODY'> </div> </div>

(Keep in mind I am trying to simplify this as much as possible and still not confuse you.) Let's recap. The template variable 'PAGEBODY' contains the body of the html page, which is the three paragraph tags with text throughout. It looks nested in the template, but does not look nested (thanks for the correction, person who replied saying that I was using the term nested wrong :P) in the source. This is because there are no tabs in the database's version -- there is nothing to make them indented in the source.

So, my question to you, the reader (in case you didn't know what role you played), is How can I get HTML::Template to auto-indent nested variables without editing the variable itself? I am not really sure this can be done, and it would be a shame if that is true because I wasted your time (unless my excellent wit kept you entertained.)


TL;DR? Read the bolded sentence. The rest is filler (though entertaining (to me, at least.))

Update: I suppose it does not really matter if the source looks neat but I feel that the way the source looks represents how I code. It's one of the things I hate about myself xD. I'll probably take ccn's suggestion and move the whole thing over to the left and put an html comment above and below it.

I'm so adjective, I verb nouns!

chomp; # nom nom nom

Replies are listed 'Best First'.
Re: Misformatted HTML::Template source code
by graff (Chancellor) on Nov 01, 2008 at 03:56 UTC
    Do your really, truly have a compelling and significant reason to worry about the indenting of content in the "body" portion of the page that comes from your database? Client browsers are not going to care about indentation -- they ignore it -- and the web server certainly won't care about indentation.

    In the world I'm familiar with, the only humans who worry about indenting of html content are the hapless (or hopelessly compulsive) programmers who must sometimes wade through the html content to locate and understand the points where their html generation processes are going wrong. That's sort of an "ad hoc" need for indentation -- if the html is being created correctly, humans don't have to read it, and indentation is irrelevant.

    (If humans need to edit HTML data manually, then indentation is a Good Thing, but the whole point of using a database and a template system is to make manual editing completely unnecessary.)

    So please explain why indentation is so important. We often get questions that demonstrate the so-called "XY Problem", but I wonder if this might be a case of an "imaginary problem"...

Re: Misformatted HTML::Template source code
by ww (Archbishop) on Nov 01, 2008 at 12:48 UTC
    I concur with those who ask "why do you care?" As has been noted, the missing tabs will have no impact on rendering of the page except, perhaps, to speed that rendering by some tiny fraction of a milisecond, because the rendering agent will need to wait for fewer bytes. :-)

    FTR, however, a nitpicky observation about the word "nested" as used here: only the formatting is whacked; not the "nesting." The tags remain properly nested within the sense of .html.

Re: Misformatted HTML::Template source code
by shmem (Chancellor) on Nov 01, 2008 at 15:51 UTC

    Post-process your html output with HTML Tidy.

      Will that have much of an impact on the load time?

      I'm so adjective, I verb nouns!

      chomp; # nom nom nom

        The only one who can really answer that question is you. Set up a test harness using Benchmark in something as close as possible to your actual application context, and find out.
Re: Misformatted HTML::Template source code
by ccn (Vicar) on Nov 01, 2008 at 06:56 UTC

    I do not know why do you worry about identation of generated HTML, but as workaround I can suggest to shift <TMPL_VAR NAME='PAGEBODY'> to the left, thus you can get pretty output.

    <html> <head> ... </head> <body> <div> <TMPL_VAR NAME='PAGEBODY'> </div> </body> </html>
Re: Misformatted HTML::Template source code
by Anonymous Monk on Nov 01, 2008 at 04:08 UTC
    No template system in the world has this as a core feature. You would re-process the template output using something like HTML::Tree or HTML::PrettyPrinter
Re: Misformatted HTML::Template source code
by graff (Chancellor) on Nov 02, 2008 at 00:53 UTC
    Regarding your update:
    I suppose it does not really matter if the source looks neat but I feel that the way the source looks represents how I code.

    Bear in mind: the "source" that really matters is your perl code. Proper indenting makes a big difference there, and human readability is a high priority. Another important quality factor is "parsimony" -- avoidance of unnecessary work. If you add a significant amount of effort, code, cpu cycles, etc, to "solve" things that aren't really problems and that have no practical impact on the task at hand, the people who end up having to read your code may tend to have doubts about its value. (And you should be hoping that someday someone else will have to read your perl code.)

    As for the html markup, I said before, when it works, it's unlikely that any human ever really looks at it, and those who do should know better than to have high expectations about indenting and so forth. We have HTML parsers to get around the unavoidable ugliness of it all.

      I see your point and realize my pedantry but fundamentally speaking I thought there might have been a method to take care of the ugliness. I wasn't planning on putting forth much effort into fixing the "problem" :P.

      I'm so adjective, I verb nouns!

      chomp; # nom nom nom