Thread: [htmltmpl] Stripping blank lines caused by TMPL tags
Brought to you by:
samtregar
From: David S. <ca...@po...> - 2005-09-27 19:23:20
|
Hello, I would first like to say I'm a long time user, first time poster... I have always used HTML::Template to generate web pages in the past but have recently started work on a project that creates a config file and I decided to use HTML::Template to help separated the hard coded config info from my code. This has worked out great in all but one regard. All the non TMPL_VAR tags end up leaving blank lines behind because I leave them on their own lines for the sake of being able to read the tmpl file easily. This how ever makes it hard to read the generated config file because of all the extra white space. I have always felt this was an issue but did not care enough to post about it because the results were "just" HTML source and the like. I can deal with reading spaced out HTML source but this config is going to be read by others that will need to be able to read and understand it quickly. While looking over the documentation I found the ability to filter the tmpl before it is written out and created the following code for the following contrived sample: my $filter = sub { my $text_ref = shift; $$text_ref =~ s/\n\| *//g; }; my $tmpl = HTML::Template->new( filename => $template, filter => $filter ); |<tmpl_if name="stuff"> | <tmpl_loop name="stuff"> This <tmpl_var name="key"> has that <tmpl_var name="value"> | </tmpl_loop> |<tmpl_else> # Nothing to see here |</tmpl_if> This gets me the results I was looking for but feel that it is very much a kludge. Have I missed something in the docs like a param for the constructor that automagicly does the same thing I have done minus me having to do anything? Having seen the results of an HTML::Template minus the blank lines makes me want to see such results all the time without modifying my code and templates to do so. Thanks, -- David Steinbrunner |
From: Clifton R. <cli...@ti...> - 2005-09-27 20:05:31
|
On Tue, Sep 27, 2005 at 03:23:14PM -0400, David Steinbrunner wrote: > Hello, > > I would first like to say I'm a long time user, first time poster... > > I have always used HTML::Template to generate web pages in the past but have > recently started work on a project that creates a config file and I decided > to use HTML::Template to help separated the hard coded config info from my > code. This has worked out great in all but one regard. > > All the non TMPL_VAR tags end up leaving blank lines behind because I leave > them on their own lines for the sake of being able to read the tmpl file > easily. This how ever makes it hard to read the generated config file > because of all the extra white space. I had similar problems when I was trying to use it to generate a text-only version of an email along with an HTML-version of the same email. > I have always felt this was an issue but did not care enough to post about > it because the results were "just" HTML source and the like. I can deal > with reading spaced out HTML source but this config is going to be read by > others that will need to be able to read and understand it quickly. This can also cause problems even in HTML, when (for example) there are conditional expressions with the result of the expression embedded in a variable value. As I recall there is a kludge where you can work around it with some very odd formatting of the original template - place the trailing newline *inside* the intermediate and end tags, e.g : "<TMPL_IF foo>bar< TMPL_ELSE>baz< /TMPL_IF >" results in "bar" or "baz" rather than having embedded newlines. ... > This gets me the results I was looking for but feel that it is very much a > kludge. Have I missed something in the docs like a param for the > constructor that automagicly does the same thing I have done minus me having > to do anything? Having seen the results of an HTML::Template minus the > blank lines makes me want to see such results all the time without modifying > my code and templates to do so. FWIW, I'm not very actively working with HTML::Template right now, but if I were I would definitely want to use such a feature. -- Clifton -- Clifton Royston -- cli...@ti... Tiki Technologies Lead Programmer/Software Architect "My own personal theory is that this is the very dawn of the world. We're hardly more than an eyeblink away from the fall of Troy, and scarcely an interglaciation removed from the Altamira cave painters. We live in extremely interesting ancient times. I like this idea. It encourages us to be earnest and ingenious and brave, as befits ancestral peoples; but keeps us from deciding that because we don't know all the answers, they must be unknowable and thus unprofitable to pursue." -- Teresa Nielsen Hayden, 1995 |
From: Mitar <mm...@gm...> - 2005-09-28 18:36:54
|
Hi! > This gets me the results I was looking for but feel that it is very much = a > kludge. Have I missed something in the docs like a param for the > constructor that automagicly does the same thing I have done minus me hav= ing > to do anything? Having seen the results of an HTML::Template minus the > blank lines makes me want to see such results all the time without modify= ing > my code and templates to do so. I use this filter: our $line_merger =3D sub { =09my $text_ref =3D shift; =09# Removes HTML comment style format from the tags (so that next regex wo= rks) =09$$text_ref =3D~ s/<!--\s*(~?\/?TMPL_.+?)\s*-->/<$1>/gis; =09# "Cleans" tags in such a way that it removes the space characters and ~ (so if there is no ~ it does not =09# remove anything) =09$$text_ref =3D~ s/(?:\n[ \t]*(?=3D<~))?<~?(\/?TMPL_[^>~]+)~?(\/?)>(?:(?:(?<=3D~\/>)|(?<=3D~>))[ \t]*\n)?/<$1$2>/gis; }; The idea is that I can use ~ character to specify that I want newline remov= ed. For example: <~TMPL_VAR NAME=3D"foo"~> will remove newline before and after the tag (if there is any). In most cases I use only second ~ as this efficiently (in most template uses) removes all newlines after the tags and so there are no empty lines in the output. Mitar |