Amazon.com Widgets Announcing THTMLWriter

Announcing THTMLWriter

By Nick at November 22, 2010 09:44
Filed Under: Delphi, Software Development, Tech Stuff

I have started an open source project on GoogleCode called THTMLWriter.

THTMLWriter is a Delphi-based class library that enables you to create HTML code and HTML documents. It uses the fluent interface to make creating HTML text easy and natural.  You can create HTML Documents or HTML “chunks” by  using any tag as a base.

THTMLWriter is all class based.  I started out trying to do some fancy things with interfaces, but it just got too complicated, and so I opted to go with the “simplest thing that will work” option, and now you can write code that looks like this:

 

Temp := THTMLWriter.CreateDocument;
     TempHTML := Temp
                  .OpenHead
                    .AddAttribute('dweezle')
                    .AddText('farble')
                  .CloseTag
                  .OpenBody.AddAttribute('ding')
                      .OpenSpan
                        .AddAttribute('this', 'that')
                        .AddStyle('font: italic')
                        .OpenDiv
                          .AddAttribute('floo')
                          .AddText('Blah')
                        .CloseTag
                      .CloseTag
                      .AddText('Hoorah')
                      .AddBoldText(' Shadooby')
                      .OpenBold
                        .AddText('Goombah')
                      .CloseTag
                  .CloseTag
              .AsHTML;

 

The project more or less produces XHTML code at the 4.01 level – I’ve tried to be XHTML-compatible, but I confess I haven’t been fanatical about it.  That is one of the things I want to in the future.  In addition, I want to update the project to support HTML5.  I’m still thinking about how to do that.

The project is pretty complete – you should be able to construct most HTML constructs.  I’ve provided a lot of overrides and default parameters for many of the “normal” attributes on a given tag.   I’ll continue to update the project with improvements and refinements.  I’ve used HTML & XHTML Pocket Reference from O’Reilly as my guide. I haven’t quite implemented everything, but it’s definitely at the point the that it is ready for you fine  people.

The project includes a full set of DUnit tests to give you confidence that the code actually does what it is supposed to. 

I have added a lot of XMLDoc comments to the code, but it’s far from complete. I’ve used the Documentation Insight plugin from DevJET software – the guys who do the Delphi Spring Framework.  (I can’t recommend this plugin enough – an outstanding contribution to the Delphi community, above and beyond the amazing Delphi Spring Framework.)

You can get the code from Google code using Mercurial.  We are planning on migrating our codebase here at Gateway Ticketing to Mercurial after the first of the year, and this project was my learning curve for using Mercurial.  Now that I’ve been using Mercurial – a distributed source control system – I wonder how we as developers ever managed to use anything but a DVCS.  It is that good and that powerful.  I heartily recommend looking at migrating to a DVCS.  It’s so much more flexible and more powerful than a server based solution such as Subversion

I’d be very happy to add anyone to the project who is interested in contributing.  I’ll also be happy to integrate submitted patches as well.  This have been a fun project to keep my toe in the water of coding – being a manager now means I don’t get to code as much as I want --but living alone in Pennsylvania while my amazing wife moves our house out here has given me a lot of free time to code.

Enjoy and let me know what you think.

Comments (13) -

11/22/2010 9:33:00 PM #

Congradulations on this worthy project idea. I think I will be following this one closely.

Sean B. Durkin Australia |

11/23/2010 4:11:09 AM #

Nick,

Can I ask if you prefer Mercurial over Subversion and if so, why? In my limited experience, using subversion through TortoiseSVN is an amazing tool for version control of Delphi stuff so I'm interested if there's something better.

Regards

Simon

Simon Clayton United Kingdom |

11/23/2010 5:06:18 PM #

Hi Nick, nice new blog.

I think fluent interfaces are the way for every tree structure.

I like this attributes declaration more than yours:

property Attrib[const name:string; const value:string] : THTMLWriter read AddAttrib; default;


This makes interface more readable (I think):

.OpenDiv['class','this']['id','that']


Francisco Ruiz Spain |

11/23/2010 8:47:58 PM #

Very cool.. Now I just have to think up a use for it.. ;)

Vincent Parrett Australia |

11/25/2010 9:36:42 PM #

Francisco --

That is a great idea -- I'll work on that.

nick United States |

11/28/2010 9:16:00 AM #

How would you do conditional rendering?

Lars Fosdal Norway |

11/28/2010 11:22:17 AM #

I like fluent interfaces but for something like this I took a different approach. Look here:

www.cromis.net/.../

Otherwise I think that html writing through code is to rigind a most of all not flexible enough. For a simple change of one tag you have to recompile.

So I would love to see a good templating system with such fluid approach on callbacks. I havent seen a good templating system for HTML written for delphi. If you know any let me know.

Iztok Kacin Slovenia |

11/28/2010 6:21:51 PM #

Lars --

What do you mean by "conditionally rendering"?

Iztok --

I guess THTMLWriter is what it is.  You might use it for large documents, or for small discrete chunks of HTML.

I'll give your ideas some thoughts -- very interesting stuff at cromis.net.  But no RSS Feed?

nick United States |

11/29/2010 2:48:04 AM #

@Nick

I am not saying that THTMLWriter is bad, not at all. I think it is a very good piece of code. It is just that if you want to have a trully flexible HTML contenting system you would have to combine it with a good templating engine. And I don't know of any. I have been inclined on writing one myself, but I don't find the time to do it. I would love to have something like ASP.NET MVC or Ruby on Rails templating.

I have a self made MVC for both VCL and WEB, but is is far from complete. The one thing that I lack most is a good templating system. Maybe I will blog about this shortly. I have been very busy, so I negledted my blog recently Smile

I added RDD feed as a widget on the blog page. I was there all the time but not visible Smile The bloog is on delphifeeds but I haven't bloged over half a year. Shame on me Frown

Iztok Kacin Slovenia |

11/29/2010 3:02:36 AM #

By the way Nick, glad to see you are still programming Smile
Don't leg go of that habit ;)

Iztok Kacin Slovenia |

11/29/2010 8:46:20 AM #

Iztok --

No, no worries - thanks for the comments.  I'll have to give some thoughts to the notion of a templating engine.  THTMLWriter was more of a thought experiment than anything else.  It's been fun to work on, and if anyone finds it useful, so much the better.

nick United States |

11/29/2010 9:35:16 AM #

@Nick

Great, I will keep an eye on your progress. If you would like to hear, how I see a perfect templating system just drop me an e-mail through my blog page.

Iztok Kacin Slovenia |

11/30/2010 7:20:38 AM #

Nick,
Conditional rendering:

... somewhere inside your page render
HTML.OpenSomeDiv;
if SomeCondition and HasPermission
then HTML.AddSomeDropBoxPopulatedFromDB
else HTML.AddCheckBoxForSomeOption;
HTML.CloseSomeDiv;
... page render continues

i.e. rendering different sections of HTML into the result documents, depending on parameterized conditions.

Example - Content visibility based on:
- access rights
- display option (page configuration, detail level, with/without images, etc)

Lars Fosdal Norway |

Pingbacks and trackbacks (2)+

Comments are closed

My Book

A Pithy Quote for You

"It is difficult to get a man to understand something, when his salary depends upon his not understanding it."    –  Upton Sinclair

Amazon Gift Cards

General Disclaimer

The views I express here are entirely my own and not necessarily those of any other rational person or organization.  However, I strongly recommend that you agree with pretty much everything I say because, well, I'm right.  Most of the time. Except when I'm not, in which case, you shouldn't agree with me.

Month List