Francis's Octopress Blog

A blogging framework for hackers.

Posttest

Liquid template engine

Introduction

Liquid is a template engine which was written with very specific requirements:

  • It has to have beautiful and simple markup. Template engines which don’t produce good looking markup are no fun to use.
  • It needs to be non evaling and secure. Liquid templates are made so that users can edit them. You don’t want to run code on your server which your users wrote.
  • It has to be stateless. Compile and render steps have to be seperate so that the expensive parsing and compiling can be done once and later on you can just render it passing in a hash with local variables and objects.

Why you should use Liquid

  • You want to allow your users to edit the appearance of your application but don’t want them to run insecure code on your server.
  • You want to render templates directly from the database
  • You like smarty (PHP) style template engines
  • You need a template engine which does HTML just as well as emails
  • You don’t like the markup of your current templating engine

What does it look like?

Howto use Liquid

Liquid supports a very simple API based around the Liquid::Template class. For standard use you can just pass it the content of a file and call render with a parameters hash.

1
2
@template = Liquid::Template.parse("hi ") # Parses and compiles the template
@template.render('name' => 'tobi')                # => "hi tobi"

Build Status

Template Data

Jekyll traverses your site looking for files to process. Any files with [[YAML Front Matter]] are subject to processing. For each of these files, Jekyll makes a variety of data available to the pages via the “ templating system”:http://wiki.github.com/shopify//-for-designers. The following is a reference of the available data.

h2. Global

| Variable | Description | | @site@ | Sitewide information + Configuration settings from @_config.yml@ | | @page@ | This is just the [[YAML Front Matter]] with 2 additions: @url@ and @content@. | | @content@ | In layout files, this contains the content of the subview(s). This is the variable used to insert the rendered content into the layout. This is not used in post files or page files. | | @paginator@| When the @paginate@ configuration option is set, this variable becomes available for use. |

h2. Site

| Variable | Description | | @site.time@ | The current Time (when you run the jekyll command). | | @site.posts@ | A reverse chronological list of all Posts. | | @site.related_posts@ |If the page being processed is a Post, this contains a list of up to ten related Posts. By default, these are low quality but fast to compute. For high quality but slow to compute results, run the jekyll command with the @—lsi@ (latent semantic indexing) option. | | @site.categories.CATEGORY@ | The list of all Posts in category @CATEGORY@. | | @site.tags.TAG@ | The list of all Posts with tag @TAG@. | | @site.[CONFIGURATION_DATA]@ | As of 0.5.2, all data inside of your @config.yml@ is now available through the @site@ variable. So for example, if you have @url: http://mysite.com@ in your configuration file, then in your posts and pages it can be used like so: http://jhjguxin.github.io. Jekyll does not parse a changed @config.yml@ in @auto@ mode, you have to restart jekyll. |

h2. Page

| Variable | Description | | @page.url@ | The URL of the Page without the domain. e.g. @/es/index.html@ | | @page.content@ | The un-rendered content of the Page. |

Note: Any custom front matter that you specify will be available under @page@. For example, if you specify @custom_css: true@ in a page’s front matter, that value will be available in templates as @page.custom_css@

h2. Post

| Variable | Description | | @post.title@ | The title of the Post. | | @post.url@ | The URL of the Post without the domain. e.g. @/2008/12/14/my-post.html@ | | @post.date@ | The Date assigned to the Post. This can be overridden in a post’s front matter by specifying a new date/time in the format @YYYY-MM-DD HH:MM:SS@ | | @post.id@ | An identifier unique to the Post (useful in RSS feeds). e.g. @/2008/12/14/my-post@ | | @post.categories@ | The list of categories to which this post belongs. Categories are derived from the directory structure above the ==posts== directory. For example, a post at @/work/code/posts/2008-12-24-closures.textile@ would have this field set to @[‘work’, ‘code’]@. These can also be specified in the [[YAML Front Matter]] | | @post.tags@ | The list of tags to which this post belongs. These can be specified in the [[YAML Front Matter]] | | @post.content@ | The rendered content of the Post. |

h2. Paginator

note: only available in index files, can be in subdirectory /blog/index.html

| Variable | Description | | @paginator.per_page@ | Number of posts per page. | | @paginator.posts@ | Posts available for that page. | | @paginator.total_posts@ | Total number of posts. | | @paginator.total_pages@ | Total number of pages. | | @paginator.page@ | The number of the current page. | | @paginator.previous_page@ | The number of the previous page. | | @paginator.next_page@ | The number of the next page. |