diff --git a/yesod-examples/.gitignore b/yesod-examples/.gitignore new file mode 100644 index 00000000..47451091 --- /dev/null +++ b/yesod-examples/.gitignore @@ -0,0 +1,2 @@ +client_session_key.aes +dist diff --git a/yesod-examples/LICENSE b/yesod-examples/LICENSE new file mode 100644 index 00000000..b0140c5d --- /dev/null +++ b/yesod-examples/LICENSE @@ -0,0 +1,30 @@ +Copyright Michael Snoyman 2010 + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of Michael Snoyman nor the names of other + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/yesod-examples/README b/yesod-examples/README new file mode 100644 index 00000000..e69de29b diff --git a/yesod-examples/Setup.hs b/yesod-examples/Setup.hs new file mode 100644 index 00000000..cd7dc327 --- /dev/null +++ b/yesod-examples/Setup.hs @@ -0,0 +1,3 @@ +#!/usr/bin/env runhaskell +import Distribution.Simple +main = defaultMain diff --git a/yesod-examples/src/MkToForm2.hs b/yesod-examples/src/MkToForm2.hs new file mode 100644 index 00000000..1cedbeb5 --- /dev/null +++ b/yesod-examples/src/MkToForm2.hs @@ -0,0 +1,15 @@ +{-# LANGUAGE TypeFamilies, QuasiQuotes, GeneralizedNewtypeDeriving #-} +{-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE MultiParamTypeClasses #-} +module MkToForm2 where + +import Yesod +import Data.Time (Day) + +mkPersist [$persist| +Entry + title String + day Day Desc toFormField=YesodJquery.jqueryDayField' + content Html toFormField=YesodNic.nicHtmlField + deriving +|] diff --git a/yesod-examples/src/ajax.lhs b/yesod-examples/src/ajax.lhs new file mode 100644 index 00000000..7bbfaec9 --- /dev/null +++ b/yesod-examples/src/ajax.lhs @@ -0,0 +1,112 @@ +
We're going to write a very simple AJAX application. It will be a simple site with a few pages and a navbar; when you have Javascript, clicking on the links will load the pages via AJAX. Otherwise, it will use static HTML.
+ +We're going to use jQuery for the Javascript, though anything would work just fine. Also, the AJAX responses will be served as JSON. Let's get started.
+ +> {-# LANGUAGE TypeFamilies, QuasiQuotes, TemplateHaskell, MultiParamTypeClasses, OverloadedStrings #-} +> import Yesod +> import Yesod.Helpers.Static +> import Data.Monoid (mempty) + +Like the blog example, we'll define some data first. + +> data Page = Page +> { pageName :: String +> , pageSlug :: String +> , pageContent :: String +> } + +> loadPages :: IO [Page] +> loadPages = return +> [ Page "Page 1" "page-1" "My first page" +> , Page "Page 2" "page-2" "My second page" +> , Page "Page 3" "page-3" "My third page" +> ] + +> data Ajax = Ajax +> { ajaxPages :: [Page] +> , ajaxStatic :: Static +> } +> type Handler = GHandler Ajax Ajax + +Next we'll generate a function for each file in our static folder. This way, we get a compiler warning when trying to using a file which does not exist. + +> staticFiles "static/yesod/ajax" + +Now the routes; we'll have a homepage, a pattern for the pages, and use a static subsite for the Javascript and CSS files. + +> mkYesod "Ajax" [$parseRoutes| +> / HomeR GET +> /page/#String PageR GET +> /static StaticR Static ajaxStatic +> |] + +That third line there is the syntax for a subsite: Static is the datatype for the subsite argument; siteStatic returns the site itself (parse, render and dispatch functions); and ajaxStatic gets the subsite argument from the master argument.
+ +Now, we'll define the Yesod instance. We'll still use a dummy approot value, but we're also going to define a default layout.
+ +> instance Yesod Ajax where +> approot _ = "" +> defaultLayout widget = do +> Ajax pages _ <- getYesod +> content <- widgetToPageContent widget +> hamletToRepHtml [$hamlet| +> \ +> +> +> +>