Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "flags: {}" to default Elm.Main.init in index.html #1821

Closed
bburdette opened this issue Oct 9, 2018 · 3 comments
Closed

Add "flags: {}" to default Elm.Main.init in index.html #1821

bburdette opened this issue Oct 9, 2018 · 3 comments

Comments

@bburdette
Copy link

TLDR: when elm make src/main.elm generates index.html, have it add in a default flags:{} argument. Then users who wish to use Browser.Document to make a single page with a title can do so by handling an empty flags arg in their init function. If the user chooses Browser.sandbox, the extra flags arg is just ignored.

Motivation:

I want to make a simple self contained one page app - all code in index.html. I'd like to set the title, so that means using Browser.Document.
The problem is that document requires an init function that takes a flag: argument. Index.html does not supply this.
I have the option of editing in an argument in the generated index.html after every compile, or going to a separate index.html and main.js, which I don't want to do for this no-server page.

@rlefevre
Copy link
Member

rlefevre commented Oct 9, 2018

This is not needed, use () (unit type) as your flags type, then no flags will be needed.
For example:

module Main exposing (main)

import Browser
import Html exposing (Html, text)


type alias Model =
    {}


main : Program () Model msg
main =
    Browser.document
        { init = always ( {}, Cmd.none )
        , subscriptions = always Sub.none
        , update = \msg model -> ( model, Cmd.none )
        , view = always { title = "title", body = [ text "hello" ] }
        }

You can compile it to index.html and test it.

There is an example in the guide using it there.

@bburdette
Copy link
Author

Ha, it'd be nice if that was a little clearer in the docs! My solution was this build script:

elm make src/Main.elm 

sed s/Elm.Main.init\({/Elm.Main.init\({flags:\"\",/ index.html > index2.html

mv index2.html index.html

Your solution works just as well and with way more elegance.

@rlefevre
Copy link
Member

rlefevre commented Oct 9, 2018

I agree that it would be nice if it was explicitly described in the flags section of the guide for example.

You could open an issue or PR in the guide instead: https://github.com/evancz/guide.elm-lang.org

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants