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
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:
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:
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."
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));
}
else
{
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> #end </ul>
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."
no subject
Date: 2011-10-13 10:43 pm (UTC)Nice bit of code refactoring. One of these days I must get back to hacking.
no subject
Date: 2011-10-14 06:57 am (UTC)They're definitely going to be alterable as well.
no subject
Date: 2011-10-13 07:38 pm (UTC)no subject
Date: 2011-10-13 07:42 pm (UTC)When I was writing it it seemed to collapse onto the next line on less wide windows.
no subject
Date: 2011-10-13 09:12 pm (UTC)no subject
Date: 2011-10-14 12:14 am (UTC)You just don't have to wait 20 years between iterations.