views:

59

answers:

2

I have a number of applications written in different languages (Java, JavaScript, PHP, etc.). Their view/presentation layers display things in various formats: HTML, plain text, etc.

I want to display some textual/numeric data in roughly the same way in each application. Of course, the result would be slightly different depending on the output format (e.g. if you're outputting HTML, you'd have to run some HTML-encoding functions, and if you're outputting plain text, you'd have to ignore things like links.)

So I was thinking of storing the formatting information in some sort of abstract configuration language. Then each application could parse the formatting info, convert it to code in its own language, and run the code to generate the actual display text.

For example:

    Welcome <username>.
    Your balance is <balance format:usDollars>.
    <if returning>You last logged in on <lastLoginDate format:m/d/Y></if>

My question is: do I need to invent such a syntax from scratch? Or is there an existing templating/formatting language that's made to be cross-platform?

+2  A: 

I would recommend looking at XSLT and pass the data in XML form. That is what XSLT was created for (you can convert to plain text or HTML, and most mainstream languages these days have XSLT libraries.

DVK
This sounded like a pretty good idea. However, I've spent the last several hours trying to get XSLT to do something as simple as format a date, and I'm ready to break something. None of the "standard" libraries like EXSLT, FunctX, or xsltsl seem to be working. I'll keep trying, though...
JW
Yikes :(On the good side, if this works, you'll have a major XSLT notch on your resume.+1 for persistance!
DVK
A: 

JSON Template is designed exactly for this purpose. It has implementations in JavaScript, Python, PHP, and Java. Need some help updating the PHP and Java version with the latest features.

http://json-template.googlecode.com/svn/trunk/doc/Introducing-JSON-Template.html

Andy C