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

compacted variables V can be Attribute or Element #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Skyfold
Copy link

@Skyfold Skyfold commented Jul 25, 2022

I wanted to use compact HTML for a form, but could not find a way to set the checked attribute dynamically. Attribute that take an argument already work nicely with V, but CheckedA and other boolean Attribute don't have an argument to fill dynamically.

In other words, you are left with trying to write:

form = Input :@ V @"checked"

This will not type check, nor should it type check. That V @"checked" could be anything with a Convert instance which may not result in valid html.

Still, I wanted to use compact HTML and I wanted to ensure V @"checked" was a valid Attribute for Input. So, I changed:

data Put (n :: Symbol) = forall a. Convert a => Put a
data V (name :: Symbol) = V

Into:

data Put (n :: Symbol) (a :: Type) = Convert a => Put a
data V (name :: Symbol) (t :: Type) = V

and added a new rule for the Lawful type family

  Lawful relation x (V _ t) = Lawful relation x t

Now I could write

form = Input :@ (V @"checked" @(Maybe CheckedA))

and be sure that CheckedA was a valid attribute in this context.

At the same time, I am now sure that when rendering only a Maybe CheckedA gets put in that variable:

renderCompactString (compactHTML form) (Put @"checked" (Just CheckedA))

One nice addition to this implementation is that you do not have to specify the type:

renderCompactString (compactHTML $ Input :@ (V @"test")) (Put @"test" (Just CheckedA))

will happily type check. In other words, you may not need to fix existing code.


Also, I added type synonyms for all Attribute and Element to make specifying the type cleaner.

@Skyfold
Copy link
Author

Skyfold commented Jul 25, 2022

TODO:

  1. Needs update to README to explain how compact HTML has changed
  2. Needs examples added to Haddock (would adding doctest be ok?)
  3. Needs a new performance check to see how inserting blocks of html affects speed
  4. Could use a few tests

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

Successfully merging this pull request may close these issues.

1 participant