Andrew Ducker (andrewducker) wrote,
Andrew Ducker

I am such an idiot sometimes #51125 (Coding edition)

So, I've been working away at this feed posting tool, and I was refactoring it last night to insert a few "span" elements into it, so that [personal profile] matgb can format it the way he likes.

And I've wanted to make it more configurable anyway - so that people could have it come out the way they wanted. So I decided to break it into bits and put it together with code, ending up with this:

String postTemplate = "<ul class=\"links\">$postContents\n</ul>";
String descriptionTemplate ="<BR><span class=\"link-description\">$description</span>"; 
String entryTemplate ="\n<li class=\"link\"><A href=\"$url\">$title</A>$descriptionContents$tagContents</li>"; 
String tagsTemplate = "<BR><span class=\"link-tags\">(tags:$tags)</span>";
String tagTemplate = "<A href=\"$tagUrl\">$tag</A> ";

String postContents = "";
for (LinkEntry linkEntry : links) {
	String entryContents=entryTemplate.replace("$url", linkEntry.URL).replace("$title", linkEntry.Title);
	if(linkEntry.Description!= null && linkEntry.Description != "")
		entryContents = entryContents.replace("$descriptionContents", descriptionTemplate.replace("$description", linkEntry.Description));
		entryContents  = entryContents.replace("$descriptionContents","");
	String tags = ""; 
	for (LinkTag tag : linkEntry.Tags) {
		tags += tagTemplate.replace("$tagUrl", tag.TagURL).replace("$tag", tag.Tag);
	entryContents = entryContents.replace("$tagContents", tagsTemplate.replace("$tags", tags));
	postContents += entryContents;
return postTemplate.replace("$postContents", postContents)

Which, y'know, works. But it's not _nice_.

And that's when I realised that I'm an idiot. And that I have a template engine* that I'm using to produce the actual web page. So theoretically all I need is:
<ul class="links">
#foreach($link in $links)
<li class="link"><A href="$link.URL">$link.Title</A>#if($link.Description)<BR><span class="link-description">$link.Description</span>#end <BR><span class="link-tags">(tags:#foreach($tag in $link.Tags) <A href="$tag.TagURL">$tag.Tag</A>#end )</span></li>

and I get _exactly_ the same result.**

Which is awesome, because it means that, once I get it all set up nicely, I can allow anyone to format their posts the way they want to.

I have no idea why I didn't think of doing it this way originally, but I'm glad it occurred to me in the end.

*Velocity. Took me a little while to get it running with Google App Engine, but I'm largely blaming that on my unfamiliarity with Eclipse and Java.
**Okay, so I have to also write seven lines of code to initialise the templating system, pass in the links, select the correct template and merge them together.

Original post on Dreamwidth - there are comment count unavailable comments there.

  • Interesting Links for 13-06-2021

    How to Mute Twitter's Suggested Tweets on Your Timeline (tags: twitter ) Can't See Pictures in Your Mind? You're Not Alone. And Now We Know…

  • Interesting Links for 12-06-2021

    Sperm DNA methylation epimutation biomarker for paternal offspring autism susceptibility (tags: autism epigenetics genetics ) The Connecticut…

  • Interesting Links for 11-06-2021

    Biden Allies Have Been Meeting With Northern Ireland Groups For Months About The Protocol (tags: NorthernIreland UK Europe USA ) Deliveroo…

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened