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

create template haskell function to embed html templates in haskell functions #6

Closed
cdepillabout opened this issue Sep 12, 2016 · 2 comments
Assignees

Comments

@cdepillabout
Copy link
Contributor

cdepillabout commented Sep 12, 2016

We need a template Haskell function to embed the html templates (generated from the .pug files) inside a normal Haskell function.

I imagine this would look very similar to Yesod's widgetFile function (or other shakespeare template Haskell functions).

The function should do the following actions:

  • read the .html template from disk
  • figure out what variable names the template is looking for
  • wrap up all the values available in the function
  • check to make sure the template isn't asking for any variables that don't exist in the function
  • splice in the code for actually rendering the template

It would be best if we could make it so that GHC fails to compile if the template can't be rendered. However, from looking at the documentation of EDE, I'm not sure if it provides the capability to statically determine what variables a given template needs.

For example, imagine we had a template called adminUser_admin_store_create.html that looks like this:

<p>bar: {{ bar }}</p>
<p>foo: {{ foo }}</p>
<p>admin email: {{ email }}</p>

We should be able to write handlers that look like this:

foo :: Int
foo = 3

storeCreate
    :: forall xs n m
     . ( ContainsAdminSession n xs
       , Monad m
       )
    => ActionCtxT (HVect xs) m ()
storeCreate = do
    -- get the admin's email from the session cookie
    (AdminSession email) <- getAdminEmail
    let bar = "hello"
    $(kucipongTemplate "adminUser_admin_store_create")

This would ideally expand to something like this:

storeCreate
    :: forall xs n m
     . ( ContainsAdminSession n xs
       )
    => ActionCtxT (HVect xs) m ()
storeCreate = do
    -- get the admin's email from the session cookie
    (AdminSession email) <- getAdminEmail
    let bar = "hello"
    let (values :: Object) = fromPairs [ "foo" .= foo, "bar" .= bar, "email" .= email ]
        (template :: Template) = -- the following should happen at compile time: loadTemplateFromFile "adminUser_admin_store_create"
    (renderedHtml :: Text) <- unsafeTemplateToHtml $ render template values
    html renderedHtml

@arowM Let me know if you start on this. I might get to it next Sunday or Monday (either 2016/09/18 or 2016/09/19).

@cdepillabout cdepillabout assigned cdepillabout and arowM and unassigned cdepillabout and arowM Sep 12, 2016
@arowM
Copy link
Contributor

arowM commented Sep 12, 2016

@cdepillabout
Thanks for creating the issue!
It'll be 2016/09/18 or 2016/09/25 I get to it. I'll tell you then.

@cdepillabout
Copy link
Contributor Author

@arowM I have some time today while traveling to Nara, so I will start working on this.

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