Search code examples
elm

Set the page title in elm?


How can I set the page title in elm, at program startup?

I found this in the docs: (http://elm-lang.org/docs/syntax)

Elm has some built-in port handlers that automatically take some imperative action:

title sets the page title, ignoring empty strings

However I am still not able to wrap my head completely around ports, nor can I find any examples of this specific port in use. So I don't know, for example, even the port's type signature.

I know I could do this by making my own index.html and embedding the elm program inside of it, but I'd like to figure this out within elm itself, even if for no other reason than to learn how it is done. (And hopefully learn something about ports too...)


Solution

  • The type signature is whatever you define it to be. As an example of a simple app using the title port to set the title:

    import Html exposing (text)
    
    port title : String
    port title = "The page title"
    
    main =
      text "Hello, World!"
    

    As a slightly more complex example you could set the page title to update to the current time every second

    import Html exposing (text)
    import Time exposing (every, second)
    
    port title : Signal Float
    port title = every second
    
    main =
        text "Hello, World!"