Esgaroth
Thought Expounding
Newer Posts Older Posts

more interviewing
by alan on Fri 1st Jun 2007 7:35PM

I had another interview with a different head hunter this afternoon. If I could make money off interviewing head hunters I'd have a good business. This one is about a specific position and she's set me up with an interview for Tuesday morning. I'll post more when I learn more.

Now, other programmers out there might not agree with my assessment of what should be in a template engine. I can see the look of disdain on their faces when they're told they can't include html in their output as it will be escaped. This, of course, being on the face of programmers who aren't fanatical about the Model-View-Controller methodologies. Personally, I'm still exploring the whole Model-View-Controller methodologies myself. I have been for over 7 years. As a general principle to aim toward to make things easier to modify later seems good. I've never worked with a system that enforced the seperation as my template engine will. I created the engine more as a way of trying this out than anything.

Anyway, I'm going to explain the "syntax" that my template engine understands. In a word: XHTML! (That's by a loose definition of "word".) The engine is specifically for the web and I'm using standard html syntax. I've been doing some searching and haven't found many other template engine that uses straight HTML as the template. And those that do seem to use strange extensions like <jsp:include page="pagename"> or worse <%@ include file="pagename" %> (Those is out of JavaServer Pages from O'Reilly and Associates.) We may as well just return to PHP with <?php include 'filename'; ?>

Historical note, yes, I know PHP and JSP are old. There are newer template engines, but I've noticed that they all either ignore HTML (like Smarty) or they extend it in weird ways like JSP. I like neither.

So, instead I use the standard HTML syntax. No extensions, and you can use sample data where you're going to have real data show up. I do this (as I explained at the beginning of last month) using the class attribute. The class attribute is generally considered to be good for specifying things with CSS. Whether you're describing that all elements with the class 'important' are in red, or whatever. And true, class seems to have been placed into the HTML spec for that purpose; however, it's not limited to only that. Any HTML processor (including a template engine) can use it. They used to say that you knew a piece of software was a success when it was being used for something its creator had never dreamed of. I'm not sure if the creators of the class attribute dreamed of this or not, but to me the class attribute is a success.

So, look at this fragment of code: <span class="important aw_message">This is the template message</span>. You can see that it's an HTML fragment of a span that has two classes associated with it and some text in it. One of the classes is a normal CSS class "important" and the template designer would use her favourite web site editor to associate some CSS with that class. According to the HTML spec, an element may have multiple classes associated with it, seperated by spaces. The browser will apply CSS designs to the element according to the classes that are referenced in the CSS and will ignore any it doesn't know about. That's important, it means we can set the class on the element without worrying about using up the class attribute, such as if I had chosen to use the id attribute.

Now, when the template engine processes the template, it recognizes the aw_ prefix on one of the classnames and when the page is rendered, the engine looks for a field in its data source by the name (without the aw_ prefix) and replaces the text inside the element with it. So, for this case, if the data source had a field 'message' with the value "Hello, World", the output would be: <span class="important aw_message">Hello, World</span>.

You'll notice that the class aw_message is still on the span. This is intentional. It is so that I can do my #1 idea from yesterday: "handle output data as an example with which to work". Someone could then take that output data and use it to create a new template (tweaking what's there for instance) and then return it to the template engine as the template to work with.

My #2 idea is mainly to make things work properly when you come to trying to deal with input elements. <input class="aw_firstname" /> would be enough to produce the output <input class="aw_firstname" name="firstname" value="Alan" /> assuming that there was a field called firstname with my name as its value. The template designer need neither know nor care what the input name is going to be since it's not her job to know that. She just needs to know the class name to set. If the name or value attributes are set, they'll be overwritten by the real values.

For more advanced use, such as outputting css on the fly (which, at my last job, we needed to do for various reasons), the template designer can set the class attribute on the style element. Yes, that's right. The HTML spec allows a class attribute on the style element. Normally you wouldn't, but we're still not breaking anything. The designer might have the CSS that doesn't change (and the "normal" css) in a seperate file using link rel="stylesheet", or a style element, and then after that style element have one with a class="aw_programmerscss" and the programmer sets a field on the datasource with the variable css as its value (probably picked from a database). This can also work with the script tag!

For completeness, what about the case where the template designer wants to put negative numbers red (for an accounting package for instance)? First off, the programmer will have to give the ability to decide if a number is negative by adding an attribute (or we'll break seperation rules 2 through 4). Negativity is a business decision. So, say the programmer creates a field negative, then she could use conditional inclusion as described below, but that will break my idea #1. So instead, the programmer creates a field sign which can be negative, positive, zero. There's another special class prefix: ac_. The designer could create a class in CSS ac_sign_negative {color: red} and then assign it to the element that outputs the number like so. <td class="ac_sign_what aw_total">17.00</td> which would replace the word what with value of the field sign (negative etc.) and the 17.00 with the value of the field total.

Okay, that covers Dr. Parr's first rule: attribute reference. Conditional inclusion is the second one. This is straightforward for simple text such as our span above. <span class="important aw_message">This is the template message</span> will end up with an empty text since there's nothing to show if there is no field called message on the datasource. So, following that if we have <span class="aw_showordont">This is the template text.<a href="url2.com">link</a> message</span>, we have a conditional. If there's a field not equal to the value false in the datasource, the contents of the element This is the template text.<a href="url2.com">link</a> message will be shown. And there could be a attribute reference or even a conditional which will be treated as expected inside there as well. If there is no such field, or it's equal false. The contents won't be shown.

I'll have to get to the other rules another day. Time for me to go.

Template engine part 4.
by alan on Mon 4th Jun 2007 12:12PM

Okay, I'll try to finish up the description of the philosophy of my template engine today, so that those who come here for stories can get back to their irregularly scheduled posts about Marchan.

Conditional inclusion means that we're wrapping an area with a name. For instance, <span class="aw_employee">look at <a class="aw_name" href="bob.html">Bob</a></span>, we have the field employee around the name, then we can have another span <span class="aw_manager">look at <a class="aw_name" href="john.html">John</a></span> and we might expect that the second time the name is referenced it is referring to the manager's name instead of the employee's name. And that's exactly what happens, provided the programmer passes in data that way. The programmer can set up employee as a namespace (allowed by my rule 3) and manager as a namespace and assign a name attribute to each of them. The two spans above would then refer to the correct name attributes.

If the template writer desired to refer to the manager's name outside of a conditional inclusion element, she could write the following <span class="aw_manager-name">John</span>. This could even be inside the employee span, since the rendering engine will look for manager in side employee and not finding it, will look in the outer namespace.

I'm going to skip Parr's rule 3 and come back to it. I'm instead going to skip to his rule 4: template application to a multivalued attribute. I built this on top of conditional inclusion and namespaces. The template writer creates something similar to <ul><li class="aw_friends-1">one</li> <li class="aw_friends-2">two</li> <li class="aw_friends-3">three</li></ul> and the template engine recognises that the friends attribute will be a multivalued attribute that should then be looped over. The loop is implicit on the fact that the name of the attribute is an integer! You may notice of course that using the namespaces I described earlier, that you don't really need to type friends each time, instead doing this: <ul class="aw_friends"><li class="aw_1">one</li> <li class="aw_2">two</li> <li class="aw_3">three</li></ul>. That's perfectly acceptable too, although when you start getting into multivalued attributes which have multivalued attributes as their values, it gets confusing.

Here's an example of that: <table class="aw_table "> <tr><th class="aw_header-1 ">col1</th><th class="aw_header-2 ">col2</th><th class="aw_header-3 ">col3</th><th class="aw_header-4 ">col4</th></tr> <tr class="aw_1 "><td class="aw_1 ">data1</td><td class="aw_2 ">data2</td><td class="aw_3 ">data3</td><td class="aw_4 ">d3</td></tr> <tr class="aw_2 "><td class="aw_1 ">data1</td><td class="aw_2 ">data2</td><td class="aw_3 ">data3</td><td class="aw_4 ">d3</td></tr> <tr class="aw_3 "><td class="aw_1 ">data1</td><td class="aw_2 ">data2</td><td class="aw_3 ">data3</td><td class="aw_4 ">d3</td></tr> <tr class="aw_4 "><td class="aw_1 ">data1</td><td class="aw_2 ">data2</td><td class="aw_3 ">data3</td><td class="aw_4 ">d3</td></tr> </table>

Okay, now returning to the recursive template inclusion. I've added another prefix at_. Our three prefixes are aw_, ac_, and at_. That's all I've found are needed, and based on Dr. Parr's work, I'm confident that's all that are needed. Template inclusion is straightforward <div class="at_otherfile">this will be replaced by the other template</div> will replace the inside text with whatever the otherfile.html template will produce. In this case, the otherfile.html will have the same datasource and be in the same namespace as the div it's included within. For looping over a list of items, the programmer could make them all namespaces and the template writer would set the class to "at_otherfile aw_list-1", where list is the multivalued attribute which is itself a namespace.

That rounds out the philosophy of my template engine. I've been mostly describing things from the template writer's point of view, since making things understandable to the template writer was one of my primary concerns. That reminds me of the other concerns I had:

  • Understandable to a template writer
  • Standard template input with no special editor. A template writer can use standard html editing tools
  • enforces the view being seperate from the model and controller (See Dr. Parr's guidelines.)
  • remove chances of accidently allowing datasource data from messing up template (or cross site scripting).
  • Template writer uses real (or at least realistic) data to lay out the template

I'm going to set up a page this week with all the info including some code that I have that works. It's really messy right now, but it does seem to do all the things I've described in these last four posts. I'll have to rearrange these four posts and largely rewrite them so that I can use it as documentation for the engine for the template writers. Later, I'll write some of how a programmer would interface with the template.

template API
by alan on Fri 8th Jun 2007 1:28PM

For the last few days I've been doing a few things other than programming. Monday afternoon I borrowed a book from the town library. A nice little fast action book such as I read when I'm looking to fulfill my desire to read those things. Not that it really fulfills me. I had it finished Wednesday morning.

Last night I had my first violin lesson and I seem to be off to a good start. I've got some ideas now on how to proceed without getting a bunch of bad habits mixed in, and I've got little stickers on the fingerboard to tell me where to put my fingers to make various notes from the first position. I was giggling a bit on my drive home from the lesson. Violin was something that for me wasn't attainable, and now it seems to be within reach.

Here's the simple description of how to use the template engine I've been working on. The programmer might write something like this.

<?php
include_once 'path/to/Ante.inc';

// some data which could as easily come from a database
$datasource = new ArrayDataSource( array(
		'name'=>'John Smith',
		'address' => '17 Avondale Crescent',
		'city' => 'Kingston',
		'province' => 'Ontario'));

Ante::render('m.html', $datasource);
?>

And the web designer would work with a piece of html like so:

<html><head>
		<title>This is some sample text</title>
		<style>
			.bigger { color: red; }
			address { display: inline;}
		</style>
	</head>
	<body>
		Hello <span class="bigger aw_name">Mark</span>.
		<pre>
I hear you live at
<address class="aw_address">123 Straight Street.</address>
<address class="aw_city">Calgary</address>, <address class="aw_province">Alberta</address>
		</pre>
</body></html>

And the name, address, city and province would all be substituted correctly.

I've always avoided Smarty for various reasons since I first learned about it. It looks to me like it's overly complicated and doesn't use templates that are as near as pretty as the final product. Again, one of the deciding factors for me is that a template can be used in a mockup, because what you see is what you would get given the same data....

Anyway, I've now decided to call the template engine Ante. Like antechamber and anterior, since it's all about the front of a website. Letting the webdesigner build what needs to be built to make the front look good, while the programmer worries about how to make things work in the back room. Sort of like a bakery where the front room looks all nice and you can buy the bread or cakes and in the back room is where the bread is made and is completely functional.

The Ante Web Template Engine's web page will start getting some interesting pages on it soon. I'm going to upload what I've got so far so that people can see it. I'm not going to release the source just yet, as it needs to be refactored, badly! To this point I've just been working on getting something that works. This post and the previous 4 will also be ripped up and put together again as part of the documentation for the site.

Anyway, I don't like not having any of my story on the front page, so here goes:

In the morning Dana had Magra help her dress in the most extravagant way possible. The Duke would be pleased with what he saw. If he was not pleased, it certainly was not going to be because Dana had not done her duty. When the two of them arrived for breakfast, Richard, Martug, David and Samuel all stood to their feet and Richard wore a huge smile.

Martug stepped forward, 'My Lady, you are dazzling. Allow me to say, you are a very beautiful woman.' He pulled out her chair and stood behind her as she sat. David pulled out Richard's seat and stood behind him as he sat. Samuel looked around and stood behind his master's empty seat.

The Duke came in a few mintues later and accepted the seat that Samuel pulled out for him. He looked at his son and said, 'You are going to be at the Hall of Records today? Good, I may send David to you to get the document we discussed yesterday.'

'Of course, father,' Richard said. 'I will have it ready.'

The Duke ate his breakfast and then looked at Dana. 'That dress does become you,' he said. 'The King's courtiers will certainly believe you are a duchess. Come it is time to go.'

They left the table and Samuel had the horses ready in an instant. David and Martug assisted them Dana, Magra and the Duke into the carriage and stepped onto the back in their place while Samuel mounted to his place. All were dressed as best as could be. They were going to see the King.

The trip to the Palace was not very long. The Palace was at the center of the city and all the noble's houses were in the ring just outside of its gardens. No ne of the nobles had to go far to reach the Palace and the Duke's house was close by the front entrance. Dana wondered who might have houses near the servants' entrance. Perhaps those who served in the Palace? She probably had learned when she was a child, but that was now seemingly long ago.

The Kings of the city were not like other men. The first King, George, had married a Fey, a princess of the Elves and by her had a son Gareth who reigned for over a hundred years before passing his kingdom on to his eldest son and departing to lands unknown. Each king thereafter had reigned a long time, though shorter than his fathers, and did not have any children until age had caught up to him. Then one King had had only a daughter, his queen dead giving birth. The daughter, Bedua by name, was just reaching adolescence when her father died. Until that time, the kingdom had passed into the hands of the eldest son, never had a daughter, though older than her brother taken the throne. Bedua's cousin, son of her father's next oldest brother, tried to take the throne as doubt crept in.

Bedua, however, had good friends who rescued her from her cousin who desired to marry her to help validate his claim to the throne. They exitted the city by night and built the first of the barracks which in Dana's day encircled the city. To those barracks they gathered all the women they could find and taught them in a few days to fight for their Queen. For they knew that they could never hold as their own anything, if the Queen could not hold the kingdom as her own. After some days, they invaded the city and took the Palace from Bedua's cousin, and she reigned supreme.

When time came for her to marry, she found the eldest child of the eldest child regardless of gender, all the way back to George and Gareth. He happened to be a young unmarried man, a leader of his peers, a captain in the army. She offered to make him King if he would marry her. And so a King and a Queen ruled in the City once again. Their union produced Claivon who had now ruled over the City for three generations of men. He had lived longer than any of his forebears, except perhaps Gareth who had departed. Already in Dana's time, Claivon's eldest child, a daughter, had passed away, leaving a son for Claivon to train as future King. But he in turn had already become old and his daughter in turn was already dying. Her son in turn, now but 50 years of age, it was expected would be the next King, if Claivon remained but 2 decades more.

Dana knew all this, but she did not expect to be met at the gates of the Palace by an old man. The man was not decrepit but stood erect. His wrinkles and hair the main pointers to his advanced age. His clothing was that of any nobleman, and he wore no sign of office.

'My good Duke!' he said to Dana's companion, his voice still quite strong. 'Long have your family served me well. I owe so much to you. And who is this lovely young lady you bring to me? You did not have a daughter the last I heard.'

'No, M'lord, she is not my daughter. She was the granddaughter of the old Duke of Loussington. The daughter of his eldest son. Her father was Duke for a short time, but after he died, her half uncle claimed the seat for himself and she is now here to reclaim her place.'

'Oh, this is a travesty if true. We must hold court and have a judgement on that. Call her half uncle, if he still be alive, and gather what evidence you can. We will get to the bottom of this today.'

'But now come, tell me what other news. How is your son, Richard is it. Has he met this young lady?'

'Indeed, he has. He has asked permission to court her.'

'Excellent, mayhap there will be some babies to come.' Dana felt herself blanche and the King continued. 'Ho hoo, don't be embarrassed my dear. I desperately need more children of the right kind. Our dear Duke's son is definitely the right kind. He does not stand on ceremony on claim that his position as Duke's son entitles him to anything. If even a tenth of my nobles were of that kind, I would not fear so much for my kingdom. As it is, I fear. I fear greatly that my kingdom will soon be an oppressive empire. Already I can see oppresssion on the faces and backs of the peasants. Already I can see men desiring to be nobility so that they might live at ease, and so many nobles living as though they had no responsibilities. It's so sad. If I could be a father to the nobles and the nobles a father to their people, the kingdom would be a wonderful place. But many of the nobles will not allow me to talk with them. I remember your grandfather. He was such a kind hearted soul. It was a pity when he died. And your father, I remember your grandfather's son. He was just such a man as I would desire to lead my people in peace. Sigh, your uncle, however, I have never known. I met him but once. He spurned me as I tried to talk to him. I have not the power to force any of the nobles to my wishes. Your uncle is not the worst of them, but I will not miss him when you take his place. But we must hold court to show the other nobles that he holds his place unjustly.'

Dana was surprised that such an old man could talk so much and seemed not to mind standing for so long. She, herself, was starting to wish he would offer somewhere to sit. She shifted her weight.

'Ahh, my dear, I am so rude. Come let us sit on a bench not far from here. I see you are tired and your patron here and I have so much to discuss.'

He led them to a bench in his gardens. Dana noticed that there were various people walking about, doing various garden chores, and realised they were all dressed similar to the King. Were the King's gardners all nobles?

The Duke noticed her gaze. 'Each one of the people in this garden (aside from Samuel, Martug and David here) is at least a Baron,' he said. 'I myself worked here when I was younger. My son, however, has proved himself more able at working in the Hall of Records. It is the privilege of the nobility to serve the King.'

'Yes, there's another thing that's missing. Some are refusing to serve. There are Barons, Counts, even a few Dukes who refuse to come and serve me. Those who refuse to talk to me but are willing to serve are just as bad. They are expecting me to give them orders and don't understand suggestions, nor are they willing to give me their creative gifts. You see this garden? I only keep here those who are willing to talk to me. I trust these men and women. The children of those who once talked with me, or my forefathers, but who themselves won't talk with me, I keep close but not this close. They are given tasks that don't require any creativity, or hard labour. I try when I can, to talk with them, but they refuse. Anyway, my good friend. I need to discuss the political situation on the frontier with you.'

The conversation suddenly became much less interesting to Dana. She did not understand much at all any more.

That seems like a good place to leave it.

pool party
by alan on Sat 16th Jun 2007 10:48PM

My wife had a pool party today with a bunch of her friends from one of her houses when she was in university. I think she's having a lot of fun with them. For the most part I'm leaving them alone since they're a bunch of women talking together. I bbq'ed up some burgers, but they got a little burnt. :(

This week I had my second violin lesson and my teacher is impressed that I'm progressing as well as I am. All I can say is that I'm having a lot of fun with it. I had always assumed that knowing where to put your fingers to set which note would be difficult. That's actually something that I seem to be picking up quite well. What I'm stuggling with right now is proper bowing technique. More often than I like I end up hearing a high pitched whistle, or something else that doesn't sound nice. My teacher says it's just a matter of practice. Time will tell.

Well, I'm going to go practice.

Chores
by alan on Mon 25th Jun 2007 3:44PM

It's been a bit of a fun run. Last week I was still playing Starcraft for part of the week and I read another book from the library. I think if I get another book from the library it'll probably be a little more "literary", if it's soon, though I guess I have plenty to read here that might count that way. There's a Rudyard Kipling collection on my self which I have never been able to get myself into, and a Shakespeare collection that I started once. Unfortuantely, I started with Titus Andronicus which I found too disturbing and couldn't get back into reading the rest of the collection, though I have read other parts.

My violin lessons continue. My wife has stopped teasing me about killing the cat, and complains of droning instead. I think that's a considerable improvement. :-)

The Ante Web Template Engine is now released. I haven't put copyright on any of the files. I'm still trying to decide on the license. My main desire is for it to get used. If it gets used best in public domain, that might be the way to go. I guess releasing GPL might help it get noticed, though.

Unfortunately, I'm not feeling like story today.

Newer Posts Older Posts