S2 (programming language)

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
DeveloperBrad Fitzpatrick, Martin Atkins
First appeared1999 (1999)

S2 (Style System 2) is an object-oriented programming language developed in the late 1990s by Brad Fitzpatrick, Martin "Mart" Atkins, and others for the online journaling service LiveJournal in order to allow users full control over the appearance of their pages. S2 source code is compiled into Perl, which the webserver can then execute directly for individual web page requests.

The S2 system is, at its heart, completely general and can be used for almost any web application; however there exists no documentation for the implementation of S2 within other applications, which ties it relatively closely to LiveJournal.

This article will make use of LiveJournal's implementation of S2 for examples. A link to detailed documentation about this implementation can be found at the bottom.

Language features[edit]

S2's structure closely resembles that of most imperative programming languages, and includes basic instructions such as variable assignments, arithmetic operations, conditional flow control and for loops over finite sets (however, it distinctly lacks while loops). Being object-oriented, S2 allows the declaration of classes with members and methods, but global (non-class-specific) functions can also be defined. Recursion is allowed, but nesting functions is not.

Layers and properties[edit]

A distinguishing feature of S2 is that source code is organised into individual layers, of which there are six different types (core, i18nc, layout, i18n, theme and user). These layers form a tree-like structure.

At the root lies the core layer, a layer which specifies all the classes and their methods which are specific to the website on which S2 is being used. In the case of LiveJournal, these classes represent individual journal views (Recent Entries, Archive, etc.) and site-related objects (users, journal entries, etc.). It provides simple default implementations of all the methods, but also global functions that are of general use. Some of the functions are specified as being "built-in" functions and are implemented in Perl in the S2 subsystem; all other functions are implemented in S2 itself and are compiled into Perl code by the S2 compiler.

The S2 language introduces a concept of properties — these are variables that are not specific to a particular object or class, but to a layer. Such a property can be a textual string, an integer number, a hexadecimal colour code, etc. The core layer, for instance, defines textual string properties to hold user-visible text in the English language.

Subordinate to the core layer are the i18nc ("core internationalisation") layers, which allow for the textual string properties in the core layer to be overridden with translations into other languages.

Also subordinate to the core layer are the layout layers. A layout layer specifies a particular site layout (journal layout in the case of LiveJournal), and it may provide new properties that influence the look and behaviour of the layout in various ways. A layout layer normally overrides almost all of the methods defined in the core layer, and it may introduce new ones to perform certain layout-specific tasks. If a core-defined method is called which is not overridden in the layout layer, but the same method is overridden for a superordinate class, then this superordinate method is called, rather than the class-specific default implementation in the core layer.

The remaining three types of layers — i18n layers, theme layers and user layers — are all subordinate to a specific layout layer. In general, all three of them can override both properties and functions/methods from the layout layer, but their intended purpose varies. i18n are provided to internationalise the layout; this is to allow the layout to be used by speakers of other languages, but not primarily to provide a way to customise the user-visible text. theme layers can be used to provide suggested sets of values for the properties of a layout. A common application of this is to create aesthetically consistent colour themes, but in principle non-colour properties (fonts, sizes, etc.) can also be overridden in a theme layer. user layers, finally, are meant to store a particular user's preferences with regard to the values of the properties. The user layer ultimately has the "final say" on the values of the properties.


As mentioned in the previous section, i18nc layers are used to translate the core layer, while i18n layers are used to translate a layout. In its simplest form, such an internationalisation layer overrides textual string properties in order to replace English text with a translation into another language.

However, often the intricacies of languages require more sophisticated mechanisms, which is why the internationalisation layers sometimes override entire functions. For example, a plural-mapping function is provided which specifies how many and which strings a language uses following an integer number.


S2 incorporates the idea of providing documentation for a class, method, function or property directly within the source code, separate (machine-distinguishable) from conventional comments. Small strings of explanatory text can be added after the header of a class, method, function or property, and they can be used to generate documentation.

At the time of this writing, however, none of these strings are actually used, except for those associated with properties, which appear in the customisation wizard; see below. An i18n layer can override these documentary strings for properties in order to allow for speakers of other languages to be presented with explanations of the available options in their language.

The customization wizard[edit]

The S2 web user interface allows users to change the values of the properties of a layout without requiring them to write actual S2 source code. This interface is termed the "customization wizard", and it creates a user layer which stores the user's preferred values of the properties.

Unintended uses of S2[edit]

S2 was entirely designed to be a web layout programming language. It had not originally been anticipated that it would soon be used to create "geeky" applications.

In particular, at the time of this writing, four games have been written in S2: Towers of Hanoi [1], two versions of Tic Tac Toe, and a game called "Pawns" [2]. One of the Tic Tac Toe versions has been embedded into a journal style as a box in a side-bar.

See also[edit]

External links[edit]