Skip to content

Lesson 1: Hello, world!

James Cheney edited this page Jun 7, 2017 · 8 revisions

Lesson 1: Hello, world

Let's start with the simplest possible program: one that just prints "Hello, world" (albeit on a Web page):

fun mainPage () {
  page 
    <html>
    <body>
    <h1>Hello world!</h1>
    </body>
    </html>
}

serveThis(mainPage)

This is a tiny bit more complicated than you might expect. Let's go through the main components of the program:

The mainPage function defines what to do to render the main page of the program. The keyword fun starts a function definition, and we write () to indicate that there are no arguments. The body of the function is enclosed in curly braces.

The body of the function defines the return value. In Links, the body of a function is evaluated to a value, which is returned. In this case, the return value is a page, defined using the page keyword. Pages can be defined using XML literals; for example, here we write <html> and <body> tags, then <h1>Hello world!</h1>, then the appropriate closing tags. The difference between a page and an XML value is that a page has additional structure needed for Links to render the page as the result of a web request (for example to handle any forms embedded in the page).

The serveThis(mainPage) function call starts the Links web server and tells it to respond to any requests with the main page.

Exercises

  1. Change the program by modifying the content of the HTML body, or adding content (such as a page title) under the <head> tag. Does this work? What happens if you add HTML with unbalanced tags, e.g. <p> test <b> bold </p>?

In Links, HTML or XML literals have to have balanced tags, even in cases where HTML would infer a closing tag.

  1. If you are familiar with HTML forms, what happens if you include an HTML form in the page?

Plain HTML forms are allowed, but Links has special features to handle form submission within a single program, described in the next lesson.

  1. If you are familiar with CSS or JavaScript, what happens if you include a <style> or <script> tag in the page content?

CSS content can be included in a <style> tag as usual. Including JavaScript is a little more problematic, since Links translates some of its own code to JavaScript and loads some JavaScript libraries. In general it is best to avoid including JavaScript code in your Links program unless you know what you are doing.

Clone this wiki locally