plzwrk: A front-end framework

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

Please see the README on GitHub at https://github.com/meeshkan/plzwrk#readme


[Skip to Readme]

Properties

Versions 0.0.0.0, 0.0.0.1, 0.0.0.2, 0.0.0.3, 0.0.0.4, 0.0.0.5, 0.0.0.6, 0.0.0.7, 0.0.0.8, 0.0.0.9, 0.0.0.10, 0.0.0.10
Change log ChangeLog.md
Dependencies aeson (>=1.4.7 && <1.5), base (>=4.7 && <5), bytestring (>=0.10.10 && <0.11), containers (>=0.6.2 && <0.7), hashable (>=1.3.0 && <1.4), haskell-src-meta (>=0.8.5 && <0.9), mtl (>=2.2.2 && <2.3), parsec (>=3.1.14 && <3.2), split (>=0.2.3 && <0.3), template-haskell (>=2.14.0 && <2.16), text (>=1.2.3 && <1.3), transformers (>=0.5.6 && <0.6), unordered-containers (>=0.2.10 && <0.3) [details]
License BSD-3-Clause
Copyright 2020 Mike Solomon
Author Mike Solomon
Maintainer mike@meeshkan.com
Category Web
Home page https://github.com/meeshkan/plzwrk#readme
Bug tracker https://github.com/meeshkan/plzwrk/issues
Source repo head: git clone https://github.com/meeshkan/plzwrk
Uploaded by mikesol at 2020-05-22T17:31:14Z

Modules

[Index] [Quick Jump]

Flags

Manual Flags

NameDescriptionDefault
plzwrk-enable-asterius

Enable asterius

Disabled

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for plzwrk-0.0.0.10

[back to package description]

plzwrk

Chat on Gitter

A Haskell front-end framework.

Available as a Hackage package: plzwrk

📖 Looking for an overview? Read our announcement blog post.

In this document:

When to use plzwrk

plzwrk may be a good fit if you enjoy the benefits of programming in Haskell and you'd like to create a web app.

⚠️ Warning: plzwrk is experimental. It is unfit for production and the syntax will change frequently, often in non-backward-compatible ways. We will try to document all of these changes in the changelog.

Some alternatives to plzwrk:

Examples using plzwrk

Hello world

An example web page that says 'Hello world!'

{-# LANGUAGE QuasiQuotes #-}

import Web.Framework.Plzwrk
import Web.Framework.Plzwrk.Asterius

main :: IO ()
main = do
  browser <- asteriusBrowser
  plzwrk'_ [pwx|<p>Hello world!</p>|] browser

See the Hello World example live.

Aphorism machine

An Aphorism Machine that spits out and hides universal truths on demand.

Check out the source code in the kitchen-sink directory. Or see the Aphorism Machine live.

Making a webpage

plzwrk uses Asterius as its backend for web development.

A minimal flow is shown below. It assumes that you have a file called Main.hs in the present working directory with a function main :: IO () inside of it, not unlike in our hello world example.

username@hostname:~/my-dir$ docker run --rm -it -v $(pwd):/project -w /project meeshkan/plzwrk
asterius@hostname:/project$ ahc-link --input-hs Main.hs --browser --bundle

If you're using ahc-cabal, compiling an application using plzwrk is no different than compiling an application as described in the Asterius documentation with one caveat. You must use --constraint "plzwrk +plzwrk-enable-asterius" when running ahc-cabal.

Documentation

The main documentation for plzwrk is on Hackage.

The four importable modules are:

Design of plzwrk

plzwrk is inspired by Redux for its state management. The main idea is that you have an HTML-creation function that is composed, via <*>, with getters from a state.

-- State
data MyState = MkMyState { _name :: Text, age :: Int, _tags :: [Text] }

-- Function hydrating a DOM with elementse from the state
makeP = (\name age ->
  [pwx'|<p>#t{concat [name, " is the name and ", show age, " is my age."]}#</p>|])
    <$> _name
    <*> _age

-- The same function using functional tags instead of pwx
makeP = (\name age ->
    p'__ concat [name, " is the name and ", show age, " is my age."])
      <$> _name
      <*> _age

HTML-creation functions can be nested, allowing for powerful abstractions:

nested = div_ (take 10 $ repeat makeP)

PWX

pwx is similar to jsx. The main difference is that instead of only using {}, pwx uses four different varieties of #{}#:

Hydrating with a state

HTML-creation functions use an apostrophe after the tag name (ie div') if they accept arguments from a state and no apostrophe (ie div) if they don't. The same is true of pwx, ie [pwx|<br />|] versus (s -> [pwx'|<br />|]).

Additionally, HTML-creation functions for tags that don't have any attributes (class, style, etc) are marked with a trailing underscore (ie div_ [p__ "hello"]), and tags that only accept text are marked with two trailing underscores (ie p__ "hello").

Event handlers

Event handlers take two arguments - an opaque pointer to the event and the current state. Then, it returns a new state (which could also be the original state) in the IO monad.

For example, if the state is an integer, a valid event handler could be:

eh :: opq -> Int -> IO Int
eh _ i = pure $ i + 1
dom = [pwx|<button click=#c{eh}#>Click here</button>|]

To handle events, you can use one of the functions exported by Web.Framework.Plzwrk. This could be useful to extract values from input events, for instance. Please see the Hackage documentation for more information.

Server-side rendering

plzwrk supports server-side rendering. To do this, you have to compile your site twice:

When compiling using ahc-cabal, make sure to use the plzwrkSSR family of functions. These functions will look for pre-existing elements in the DOM and attach event listeners to them instead of creating elements from scratch.

There may also be times that the static website needs to be initialized with data (ie using the result of an HTTP response made on the server). In this case, you'll need to pass these values dynamically to the function that calls plzwrkSSR. You can do this using the foreign export syntax as described in the Asterius documentation.

When compiling with cabal, you'll likely be using it to output an HTML document or build a server that serves your website as text/html. Regardless of the approach, you should use toHTML to create the part of the initial DOM controlled by plzwrk. In your HTML, make sure to include a link to the script(s) produced by ahc-dist. Also, if needed, make sure to call your exported functions.

Testing your code

plzwrk comes with a mock browser that can act as a drop-in replacement for your browser.

You can use this in your tests:

import Web.Framework.Plzwrk.MockJSVal

main :: IO ()
    browser <- makeMockBrowser
    print "Now I'm using the mock browser."

Contributing

Thanks for your interest in contributing! If you have a bug or feature request, please file an issue. Or if you'd like to hack at the code base, open a pull request.

Please note that this project is governed by the Meeshkan Community Code of Conduct. By participating, you agree to abide by its terms.

Local development

  1. Clone this repository: git clone https://github.com/meeshkan/plzwrk.git
  2. Move into the directory: cd plzwrk
  3. Set up your local environment: You can use this guide from The Haskell Tool Stack for reference.