Learning UserTalk: Accessing weblogData.root
With my migration to TextPattern delayed, I decided to learn how to write a Radio Userland macro in UserTalk. I've modified macros before, but I wanted to write a new macro from the ground up.
The first thing to learn when looking at existing macros is that the adr prefix is a naming convention used when a variable is an object address rather than an object; and that the postfix ^ operator is used to deference an address. It was obvious after the fact, but it took a while for me to catch on.
The second thing to learn is that your weblog data is stored in weblogData.root; and that programatic access is provided via the radio.weblog.init macro. For example, the following code snippet retrieves the address of weblogData.root, extracts the weblog title, and then returns the title as part of a string.
<% local (adrBlog = radio.weblog.init()); local (title = adrBlog^.prefs.title); return "Weblog title is:" + title; %>
If you wanted to list your weblog categories, then you could use this snippet:
<% local (adrBlog = radio.weblog.init());
local (size = sizeof(adrBlog^.categories));
local (i, text = "");
for i = 1 to size {
text = text + " " + nameof(adrBlog^.categories[i]);
};
return "Weblog categories:" + text;
%>
I find that the easiest way to experiment with these code snippets is to create a .txt file in the www directory of the Radio Userland installation. It will be rendered into the corresponding .html file. For example, the file learn.txt would be rendered as learn.html.
21 Dec: Learning UserTalk: the Current Post
12 Jan: Learning UserTalk: Local Macros and Loops