1
1
mirror of https://github.com/srid/ema.git synced 2024-12-01 15:13:36 +03:00

Add most trivial example, Ex00_Hello.hs

This commit is contained in:
Sridhar Ratnakumar 2022-06-05 21:46:53 -04:00
parent c03a64acf7
commit 90f6a05257
4 changed files with 41 additions and 6 deletions

View File

@ -9,20 +9,29 @@ Make sure that you have have followed [the previous section](start.md) in order
```haskell
module Main where
import qualified Ema
import Ema
import Generics.SOP qualified as SOP
data Route = Route_Index
deriving stock (Show, Eq, Ord)
deriveGeneric ''Route
deriving anyclass instance IsRoute Route
instance EmaSite Route where
siteOutput _enc _m Route_Index =
Ema.AssetGenerated Ema.Html "<b>Hello</b>, Ema"
main :: IO ()
main = do
let speaker :: Text = "Ema"
Ema.runEmaPure $ \_ ->
encodeUtf8 $ "<b>Hello</b>, from " <> speaker
main = void $ Ema.runSite @Route ()
```
This is the *minimum* amount of code necessary to run an Ema site. Notice that as you replace and save this file, your browser (which is at <http://locahost:9001>) will [hot reload](concepts/hot-reload.md) to display "Hello, Ema". Congratulations, you just created your first website!
TODO: change below.
## Expanding on Hello World
Okay, but that's just *one* page. But we want to add a second page. And might as well add more content than "Hello, Ema". Let's do that next. The first step is define the [route](guide/routes.md) type that corresponds to our site's pages. Add the following:
Okay, but that's just *one* page. But we want to add a second page. And we might as well add more content than "Hello, Ema". Let's do that next. The first step is define the [route](guide/routes.md) type that corresponds to our site's pages. Add the following:
```haskell
data Route

View File

@ -141,6 +141,7 @@ library
if flag(with-examples)
other-modules:
Ema.Example.Common
Ema.Example.Ex00_Hello
Ema.Example.Ex01_Basic
Ema.Example.Ex02_Clock
Ema.Example.Ex03_Store

View File

@ -0,0 +1,20 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE TemplateHaskell #-}
-- | Most trivial Ema program
module Ema.Example.Ex00_Hello where
import Ema
import Generics.SOP.TH (deriveGeneric)
data Route = Route_Index
deriving stock (Show, Eq, Ord)
deriveGeneric ''Route
deriving anyclass instance IsRoute Route
instance EmaSite Route where
siteOutput _enc _m Route_Index =
Ema.AssetGenerated Ema.Html "<b>Hello</b>, Ema"
main :: IO ()
main = void $ Ema.runSite @Route ()

View File

@ -9,6 +9,7 @@ module Ema.Example.Ex04_Multi where
import Data.Generics.Sum.Any (AsAny (_As))
import Ema
import Ema.Example.Common (tailwindLayout)
import Ema.Example.Ex00_Hello qualified as Ex00
import Ema.Example.Ex01_Basic qualified as Ex01
import Ema.Example.Ex02_Clock qualified as Ex02
import Ema.Example.Ex03_Store qualified as Ex03
@ -22,6 +23,7 @@ import Prelude hiding (Generic)
data R
= R_Index
| R_Hello Ex00.Route
| R_Basic Ex01.Route
| R_Clock Ex02.Route
| R_Store Ex03.Route
@ -48,6 +50,8 @@ instance EmaSite R where
R_Index ->
Ema.AssetGenerated Ema.Html $ renderIndex enc m
-- TODO: Can all of these be generalized? (constructor with 1 encoder; delegate)
R_Hello r ->
siteOutput (innerRouteEncoder (_As @"R_Hello") enc) (innerModel m) r
R_Basic r ->
siteOutput (innerRouteEncoder (_As @"R_Basic") enc) (innerModel m) r
R_Store r ->
@ -61,6 +65,7 @@ renderIndex enc m@(I clockTime :* I _store :* Nil) =
H.div ! A.class_ "container mx-auto text-center mt-8 p-2" $ do
H.p "You can compose Ema sites. Here are three sites composed to produce one:"
H.ul ! A.class_ "flex flex-col justify-center .items-center mt-4 space-y-4" $ do
H.li $ routeElem (R_Hello Ex00.Route_Index) "Ex00_Hello"
H.li $ routeElem (R_Basic Ex01.Route_Index) "Ex01_Basic"
H.li $ routeElem (R_Clock Ex02.Route_Index) "Ex02_Clock"
H.li $ routeElem (R_Store Ex03.Route_Index) "Ex03_Store"