qr-code

Functions

Basically any elisp function could be used within an o-blog template, as long as they are known when exporting. Meanwhile some functions are specifically made to be used within templates. These functions are prefixed by ob:.

Descriptions are taken from function docstrings.

Function: (ob:get-posts &optional PREDICATE COUNT SORTFUNC COLLECT)

Return posts (from POSTS as defined in org-publish-blog) matching PREDICATE. Limit to COUNT results if defined and sorted using SORTFUNC.

PREDICATE is a function that runs for each post, with the post itself as argument. If PREDICATE is nil, no filtering will be done on posts.

SORTFUNC uses a sort PREDICATE.

If COLLECT is defined, only returns the COLLECT field of a ob:post structure.

Examples:

Getting the last 10 posts:

(ob:get-posts nil 10)

Getting posts from January 2012:

(ob:get-posts
   (lambda (x)
      (and (= 2012 (ob:post-year x))
           (= 1 (ob:post-month x)))))

Getting all categories:

(ob:get-posts nil nil nil 'category)

Template usages

For archive navigation:

<nav id="archives">
  <h1>Archives</h1>
  <ul>
    <lisp>
      (loop for p in (ob:get-posts nil 10)
            do (insert (format "<li><a href=\"%s/%s\">%s</a></li> "
                               (ob:path-to-root)
                               (ob:post-htmlfile p)
                               (ob:post-title p))))
    </lisp>
  </ul>
</nav>

Function: (ob:get-post-by-id ID)

Return post whose id is ID.

Template usages

Posts navigation, setting up links to previous and next post:

<nav class="articles-nav">
  <ul>
    <lisp>
      (progn
        ;; Get previous post
        (let ((ppost (ob:get-post-by-id (1+ (ob:post-id POST)))))
          (if ppost
              (insert (format "<li class=\"prev\"><a href=\"%s/%s\">%s</a></li>"
                              (ob:path-to-root)
                              (ob:post-htmlfile ppost)
                              (ob:post-title ppost)))
            (insert "<li>&nbsp;</li>")))
        ;; Get next post
        (let ((npost (ob:get-post-by-id (1- (ob:post-id POST)))))
          (if npost
              (insert (format "<li class=\"next\"><a href=\"%s/%s\">%s</a></li>"
                              (ob:path-to-root)
                              (ob:post-htmlfile npost)
                              (ob:post-title npost)))
            (insert "<li>&nbsp;</li>"))))
    </lisp>
  </ul>
</nav>

Function: (ob:get-snippet NAME)

Get first snippet matching NAME.

Template usages

Insert the About section in page footer:

<h1>About</h1>
<address>
  <lisp>(ob:post-content-html (ob:get-snippet "About"))</lisp>
</address>

Function: (ob:get-header HEADER &optional ALL)

Get HEADER from blog buffer as defined in BLOG global context variable.

Returns only the first match, unless ALL is defined.

Template usages

Get the last updated header for RSS export:

<updated><lisp>(ob:format-date (ob:get-header "DATE"))</lisp></updated>

Function: (ob:insert-template TEMPLATE)

Insert TEMPLATE in current buffer.

Template usages

Insert html header:

<lisp>(ob:insert-template "page_header.html")</lisp>

Function: (ob:format-date DATE &optional FORMAT LOCALE)

Format DATE using FORMAT and LOCALE.

DATE can either be a string suitable for parse-time-string or a list of integers using current-time format.

FORMAT is a format-time-string compatible definition. If not set, ISO8601 %Y-%m-%dT%TZ format will be used.

Template usages

Add a human-readable timestamp for a post:

Posted on <time datetime="<lisp> (ob:format-date (ob:post-timestamp POST)) </lisp>">
  <lisp> (ob:format-date (ob:post-timestamp POST) "%A %B, %d %Y at %H:%M:%S") </lisp>
</time>.