Learning UserTalk: the Current Post
In a previous post, I demonstrated how to access weblogData.root. In this post, I'll take a look at working with posts. I'll use my Radio Userland draft category to provide an authentic working environment.
I find that the easiest way to experiment with Usertalk is to include the code in .txt files stored in my Radio Userland post hierarchy. In this case, I'll create identical files that represent both a dateless and a dated entry:
- Radio_Root/categories/draft/work.txt
- at http://127.0.0.1:5335/categories/draft/work.html
- Radio_Root/categories/draft/2004/12/11.txt
- at http://127.0.0.1:5335/categories/draft/2004/12/11.html
<%
on myCurrentPost( adrBlog=radio.weblog.init(), catName="" ) {
local (date, adrPost, file = radioResponder.fileBeingRendered);
if radio.weblog.file.getArchiveFileDate( file,\@date ) {
if radio.weblog.getNextPostAfterDate( adrBlog,date,\@adrPost,catName ) {
return adrPost}};
return \@adrBlog^.posts[ sizeOf(adrBlog^.posts) ]};
myCurrentPost()
%>
Line-by-line:
- Declare a procedure named myCurrentPost with three named parameters. Each parameter is also associated with a default value to be used if it is not supplied by the caller.
- Declare three local variables. Initialize the variable file as the current file.
- Determine the date associated with the current file. Note that we use the @ operator to pass the date variable by address. This allows the called procedure to modify the date variable so that it can be accessed by the calling procedure.
- Determine the first post after the current date.
- Return the post from the previous line if available.
- Return the last post otherwise.
- Execute the myCurrentPost procedure and insert results into the rendered html file.
When executed on my system the results are:
- dateless entry …/draft/work.html
- ["Macintosh HD:MyApps:Radio UserLand:Data Files:weblogData.root"].weblogData.posts.["00001065"]
- • Note that this refers to the current post, even though the post has not yet been published. We'll correct this in a subsequent version.
- dated entry …/draft/2004/12/11.html
- ["Macintosh HD:MyApps:Radio UserLand:Data Files:weblogData.root"].weblogData.posts.["00001059"]
- • A quick look at the December index shows that there was no post on 11 December. The next post occurs on 13 December and is post 1059.