Andrew Ducker (andrewducker) wrote,
Andrew Ducker
andrewducker

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));
	}
	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.
"



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

  • Interesting Links for 06-03-2021

    Women in 20s told 'get pregnant' to ease endometriosis symptoms (tags: pregnancy healthcare women UK OhForFucksSake ) Lying liar Boris…

  • instagram cross-post

    Family. Original is here on instagram. Original post on Dreamwidth - there are comments there.

  • Interesting Links for 05-03-2021

    European Parliament Postpones Brexit Deal Vote Amid Fury Over The UK's Northern Ireland Protocol Plan I mean, normally you wait for a deal to be…

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

  • 4 comments