Long story short I have written my own custom HTML template system that I am very happy with. However I have done that with manually concatenating HTML tags, and it is a bit messy. There has to be a perl library that will help you build a static HTML document and then output the result. But when I google for a solution I get plenty of options about parsing HTML documents, using templates to generate HTML, or frameworks to generate HTML pages dynamically, but I can't seem to find a perl library that you can generate an HTML document with. Does anybody have any suggestions?

8 Replies 8

The traditional way used to be to use the HTML functions from CGI. Nowadays, templating systems are preferred (e.g. Template::Toolkit or Text::Xslate). Separating the code from the presentation leads to easier readability, maintainability, reusability, extensibility, and sometimes even performance.

Thanks! I agree, and separating my code from the presentation is exactly what I am trying to do. I basically wrote my own template library, so now I am just trying to clean that code up a bit. After I posted I dug a little harder and found that you could use HTML::TreeBuilder/HTML::Element (push_content) to create HTML documents as well. But I took a look at CGI:HTML as you suggested and it suggested HTML::Tiny, and that looks exactly like what I am looking for. In my application in some places I also generate HTML and then use HTMLDOC to convert it to a PDF, so this will come in handy there as well.

"help you build a static HTML document" from what? Data? Code? An example of what you are trying to do would go a long way toward making your question more answerable.

I built a framework where you create a database table and then you get online forms to go with it. Sort of a limited version of MS Access where the forms are on the web browser. Metadata about forms (titles, button and field names an locations, permissions, etc) are also stored in tables, so there is no hard coded UI elements. All UI is generated dynamically from data in tables for every request. There is generic code to search, display, edit, print, export, etc but when needed you can override the default code to do something more specific like a complicated report that joins tables. I currently generate the HTML to present to the user by just concatenating HTML tags into a string, and then returning the string. I also print the forms by generating similar HTML in a similar way and then use HTMLDOC to generate a PDF. HTML::Tiny should clean that both of those processes up.

I agree with choroba's response, and would add a note of support to look at Template::Toolkit. Template::Toolkit is one of the most elegant templating solutions out there for a programmer that needs to output HTML. I say this from long experience with many libraries, languages, and operating systems. I have often considered porting TT to C or Python. Good luck with your project.

I used Mojo::Template and Mojo::DOM for non Mojolicious apps and been happy with those. I think it was for emails generation.

@smonff This issue using Mojo::Template and Mojo::DOM is you have to pull in Mojolicious. In thin environments that can be undesirable.

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.