Post #186,429
12/8/04 2:02:00 PM
|
Prebuilt template system just for you
output = ["<table>"]\nfor row in table:\n output.append("<tr>")\n for field in row:\n output.append("<td>%s</td>" % field)\n output.append("</tr>")\noutput.append("</table>")\nprint "\\n".join(output)\n
The Sig: "Despite the seemingly endless necessity for doing so, it's actually not possible to reverse-engineer intended invariants from staring at thousands of lines of code (not in C, and not in Python code either)." Tim Peters on python-dev
|
Post #186,431
12/8/04 2:30:12 PM
|
Come on.
This is exactly what I am trying to avoid. Quick and durty code writing simplistic HTML. Yes, I can write nested for loops. Thank you very much. Now, do we have any real ideas?
--
This guy's ahead of his time! He's using quantum programming methods: in universes where invalid data is passed to this function, it does not return. Thus you are ensured that you will only have valid data after calling it. Optimally you'd destroy the universe on failure, but computers haven't quite advanced to that level yet.
-- [link|http://thedailywtf.com/archive/2004/10/26/2920.aspx|The] Daily WTF
|
Post #186,439
12/8/04 4:01:30 PM
12/8/04 4:20:07 PM
|
XSLT?
Here's an XML stylesheet (festoon taggery to taste):
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="[link|http://www.w3.org/1999/XSL/Transform|http://www.w3.org/1999/XSL/Transform]"><xsl:template match="/"> <html> <body> <table> <xsl:for-each select="recordset/record"> <tr> <td><xsl:value-of select="field1" /></td> <td><xsl:value-of select="field2" /></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
But you'll need to feed it an XML document. Generate one thusly (thanks FuManChu)(4 lines in for-loop should of course be indented):
output = ['<?xml version="1.0" encoding="ISO-8859-1"?>'] output.append("<recordset>") for record in recordset: output.append("<record>") output.append("<field1>%s</field1>" % field1) output.append("<field2>%s</field2>" % field2) output.append("</record>") output.append("</recordset>") print "\\n".join(output)
For bonus IE-only points, you can apply the stylesheet at runtime with this:
<html> <body><script type="text/javascript">// Load XML var xml = new ActiveXObject("Microsoft.XMLDOM") xml.async = false xml.load("recordset.xml") var xsl = new ActiveXObject("Microsoft.XMLDOM") xsl.async = false xsl.load("stylesheet.xsl") document.write(xml.transformNode(xsl))</script> </body> </html>
;)
-- Chris Altmann
Edited by altmann
Dec. 8, 2004, 04:20:07 PM EST
|
Post #186,470
12/8/04 6:48:03 PM
8/21/07 6:35:25 AM
|
UGH! He's better off with plain ol C code.
At least you can read it.
"The significant problems we face cannot be solved at the same level of thinking we were at when we created them." --Albert Einstein
"This is still a dangerous world. It's a world of madmen and uncertainty and potential mental losses." --George W. Bush
|
Post #186,480
12/8/04 7:45:35 PM
|
Sorry
Sorry, I had to turn off HTML processing for that post because it was mangling the xsl. Or was that what you meant? Also, to insure well-formed XML, I probably should have written that middle snippet like so (in VB.Net this time): \nDim writer As New XmlTextWriter(outputStream)\n\nwriter.Formatting = Formatting.Indented\n\nwriter.WriteStartElement("recordset")\n\nForeach record in recordset\n writer.WriteStartElement("record")\n writer.WriteElementString("field1", XmlConvert.ToString(field1))\n writer.WriteElementString("field2", XmlConvert.ToString(field2))\n writer.writeEndElement()\nNext\n\nwriter.WriteEndElement()\nwriter.Close()\n Much cleaner. 8)
-- Chris Altmann
|
Post #186,486
12/8/04 8:58:01 PM
|
XSL isn't bad for simple, quick and dirty formatting
I wouldn't use it for anything remotely complex, though.
Regards,
-scott anderson
"Welcome to Rivendell, Mr. Anderson..."
|
Post #186,440
12/8/04 4:06:39 PM
|
How complex do you want to get?
There are templating systems for Perl, Python, and Java that will allow you to put the template in a separate file, then provide the template name and the data to put in it to the engine and have it produce the merged output. Most of them will have looping constructs in the template language, so you can provide, say, a list and have the template produce code for each iteration in the list.
If you're feeling lucky, you could even use Word's mail merge, I suppose. :-P
Regards,
-scott anderson
"Welcome to Rivendell, Mr. Anderson..."
|
Post #186,443
12/8/04 4:59:22 PM
|
So, what would be a good one for Python?
I'll take Perl, too. The idea is simple enough: \n===================\n| |\n| Logo |\n| Company Name |\n| Person Name |\n| Address |\n| Phone | \n| FAX |\n===================\n Company name may be a link in some cases. URL and logo file name come from DB. Repeat the above for all record in query result, placing 15 record per page, generating links to next page, previous page, first, last. It should also be flexible enough to allow fancier things if I have time later, e.g. generating alphabetical index.
--
This guy's ahead of his time! He's using quantum programming methods: in universes where invalid data is passed to this function, it does not return. Thus you are ensured that you will only have valid data after calling it. Optimally you'd destroy the universe on failure, but computers haven't quite advanced to that level yet.
-- [link|http://thedailywtf.com/archive/2004/10/26/2920.aspx|The] Daily WTF
|
Post #186,450
12/8/04 5:25:37 PM
|
Sounds like a job for Perl's HTML::Template
I've never used it, but I know people who have[TM].
-YendorMike
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." - Benjamin Franklin, 1759 Historical Review of Pennsylvania
|
Post #186,509
12/8/04 10:45:32 PM
|
There are a ton of good Perl template modules
Each tackling different kinds of problems. For a comparison, see [link|http://perl.apache.org/docs/tutorials/tmpl/comparison/comparison.html|http://perl.apache.o...n/comparison.html]. While the choices described there do not include templating tools in different languages, the article at least gives a framework to think about what you want from a templating tool, which helps you judge for other languages.
But you're right that HTML::Template aims to be very simple to get started with. That also means that it lacks features that other templating modules include.
Cheers, Ben
I have come to believe that idealism without discipline is a quick road to disaster, while discipline without idealism is pointless. -- Aaron Ward (my brother)
|
Post #186,525
12/9/04 12:48:09 AM
|
That's the reason many people would choose Python
...there's no template because there doesn't need to be. If you spend 1 week learning the language, you can write what you need yourself in about an hour, AND the next such solution and the next one..., AND it'll be readable and usable in three years when the next guy comes along (even if that's you, again).
|
Post #186,528
12/9/04 1:08:06 AM
|
That's going a bit far...
A good templating language can completely obviate the need for any programming at all in a simple job like this. Otherwise people wouldn't be writing generic templating systems for Python, and they definitely are. I've written one myself.
Regards,
-scott anderson
"Welcome to Rivendell, Mr. Anderson..."
|
Post #186,529
12/9/04 1:41:50 AM
|
Meh. I'll meet that halfway
In my experience, most of the Python templating systems are just as hard to learn as writing your own in Python. Newbies often have a hard time discerning which few are not.
|
Post #186,556
12/9/04 10:06:00 AM
|
So say it already
Which templating systems in Python are "the few that are not"? I am one of those newbies who have "hard time discerning".
--
This guy's ahead of his time! He's using quantum programming methods: in universes where invalid data is passed to this function, it does not return. Thus you are ensured that you will only have valid data after calling it. Optimally you'd destroy the universe on failure, but computers haven't quite advanced to that level yet.
-- [link|http://thedailywtf.com/archive/2004/10/26/2920.aspx|The] Daily WTF
|
Post #186,574
12/9/04 11:11:11 AM
|
I can't think of any that do *everything* you want
The idea is simple enough:
=================== | | | Logo | | Company Name | | Person Name | | Address | | Phone | | FAX | ===================
Company name may be a link in some cases. URL and logo file name come from DB.
Repeat the above for all record in query result...
At this point pick whichever you like. Cheetah is probably best, given your simple page requirements. But there's no built-in database layer--you'll have to write that glue yourself using DBAPI or an odbc package. ...placing 15 record per page, generating links to next page, previous page, first, last. I can't think of any that will do this part for you. So you'd have to write *some* code. Which leads us back to "might as well do it yourself". Soooo many projects like yours start with a templating engine, then hit the limitations of it, and roll their own anyway.
|
Post #186,578
12/9/04 11:13:47 AM
|
*Sigh*
Then again, I get to code Python. Nothing to sigh about :)
--
It is illegitimate and utterly intolerant to impose such a view upon anyone, even upon yourselves. -- [link|http://belmontclub.blogspot.com/|The Belmont Club]
|