Haskell's Yesod Technology Daquan

Source: Internet
Author: User

Don't say much nonsense, just open up.
Please refer to other articles for environment construction. Here is an empty framework to illustrate how to render simple data on a page

A: After defaultlayout , you can simply use where to bring the data structure you need, which can be used in the [| |] Use functions and data to render the values you need.
[whamlet| |] Can be stacked arbitrarily, but only with one where. This conforms to Haskell's standard syntax.
Because do is the standard syntax for Haskell, you can use Monad in do to extract the data you need (general data is packaged with return or pure)
For example, you can use a file handle to extract something like a local picture or a configuration file.

Cases:

    Man <-return $ person "Jack"
    settitle "Minimal multifile"
    [whamlet|
        <p>
            <a href=@{addr 5 7}>html addition
        <p>
            <a href=@{addr 5 7}?_accept=application/ Json>json addition
        <p>
            <a href=@{helpr}>help Page
        <b> #{name person} is  #{ Age person} Old


    |]
    [Whamlet|
        #{name man} was  #{age man} old
    |]
    where person
        = person "Brant" 123
Q: What is the difference between Hamlet,whamlet,shamlet

A:whamlet says it can include Html,css,js's unified Hamlet,
Shamlet is a shorthand for simple hamlet. For now, when the page structure is simple enough, you can use Shamlet
Both Shamlet and Hamlet can use Towidget to convert to Whamlet. You can use the do structure after converting to Whamlet

Cases:

Gethomer = defaultlayout $ do
    mans <-Pure $ person "Jack"
    settitle "Minimal multifile"
    [whamlet|
        #{name man} was  #{age man} old

    |]
    Towidget [Shamlet|
        <p>
            <strong>this is shamlet message  #{name person}
    |]
    Towidget [Hamlet|
            <p>
                <strong>this is Hamlet message #{name person}
        |]
    where
        myself = Just $ Person ' myself ' 456 person
        = person ' brant ' 123
        Person1 = Person ' father '
        per Son2 = person "mother"
        people = [Person1,person2]
Q: Are there other functions to insert strings or variables directly into the stack of Whamlet

Answer: You can use the following

Addscriptremote "Https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"
Towidgethead [Hamlet| < Meta name=keywords content= "Some sample keywords" >|]
Towidgetbody [julius| alert ("This was included in the body itself"); |]
Addcassiusmedia, Addluciusmedia
addstylesheet
addstylesheetremote
addscript
addscriptremote
Q: What exactly is a widget?

A: The job of a widget is-to-hold onto these disparate components and
Apply proper logic for combining different widgets together.
This consists of things like taking the first title set and ignoring others,
Filtering duplicates from the list of external scripts and stylesheets,
and concatenating head and body content. Q: How to get a random value

A: In do, use tempvalue <-newident to generate a random value in the compilation scope.
Note that this random value is not a run-time random value, that is, when the compile-time is random, the runtime is deterministic Q: How to consider stacking business

A: Turn the business into Q with Hamlet,lucius, and then use towidget to turn into [whamlet|..|]
Finally stacking these up in do Q: How to write your own business

A: The business is a normal function, but the last type is the Widget
Of course use [hamlet| |] and [shamlet| |] Or you can. It's just that I'm not
Example:

GETHELPR  MYP = defaultlayout $ do
                [whamlet| This is the help Page #{myp}|]
                Mynewhelp 4444

mynewhelp:: Int, Widget
mynewhelp n = [whamlet| You give me #{n}|]
Q: Common tags in Hamlet

Answer: @{.} Url
#{.} variable
^{..} Embed need to be aware when using ^{...} tags, embed come in Hamlet need and container is a type, by Towidget to convert the type is not possible
Cases:

foot = [whamlet|^{page}|]
   page = [whamlet|123|]

Or

Foot =[hamlet|^{page}|]

Rephtml is a data type containing some raw HTML output ready to being sent over the wire Q: How to replace the base template

Answer: Redefine defaultlayout Q: How to change the base domain name:

Answer: Heavy approot
Example

    AppRoot = approotstatic "Http://static.example.com/wiki"
Q: How to load a route from a configuration file

Answer: Use
Mkyesoddata "APP" $ (parseroutesfile "config/routes") Q: How to extract the request parameter

A: You can use lookupgetparam,Lookupcookie, languages to operate, if it is post need to run a bit first runrequest
Example:

    http://localhost:3000/?style=aaaa&param=123
    gethomer:: Handler Html
    gethomer = defaultlayout $
        Do Req <-getrequest let
        params = reqgetparams req paramstyle <-
        lookupgetparams "style"
        ... [Whamlet|
            #{head Paramstyle}--be aware that there is a possibility that the head Paramstyle does not have
            #{show params}--You can turn the params into a map to find
Q: How to redirect

A: First of all, define the original business in the route. Then redirect it with redirect or redirectwith in the code.
Example:
GETREDIRECTR:: Handler Html
getredirectr = Redirect HELPR

1. You can carry parameters in the URL according to rest mode
1.1 redirect $ helpr {param}
1.2 HELPR:: Handler HTML variant for HELPR:: Int--Handler HTML
1.3 Modify Route/help Helpr get morph to/help/#Int HELPR get
1.4 parameters that can be used are int and text type. Note that Text can be generated by Data.Text.pack [Char] and will be encode out of the URL.

2. Carry the new parameter in the Param. Q: How to combine various pages

A: If you get [whamlet| |], you can borrow monad, use do to combine multiple [whamlet| |] Q: How to output variables directly on a page

A: When using variables, it is recommended to use #{show param} to output the content Q: If you use a template

Answer: 1. Install the Hledger-web library and add it to the. Cabal file (you can cabal install Hledger or stack install Hledger)
After the installation is complete, the cabal needs to be modified. Cabal,stack needs to be modified
Stack.yaml
Extra-deps: [hledger-web-1.0.1,wai-handler-launch-3.0.2.1]
2.import Settings
3. Create a directory at the same level as SRC templates
4. Add a file in templates, such as Homepage.hamlet
5. Need to open {-# LANGUAGE Templatehaskell #-} in code

Using a file as a renderer

Gethomer = Mylayout $ do
          $ (widgetfile "echo")
          $ (widgetfile "echo") '
use file as Template

' Mylayout:: Widget- > Handler Html
mylayout widget = do
    pc <-widgettopagecontent widget
    giveurlrenderer $ (hamletfile "tem Plates/my-layout.hamlet ")

my-layout.hamlet

$doctype 5
Q: How to get Time

A: You can use the following two ways
"' Import data.time (getcurrenttime)
Now <-Liftio GetCurrentTime

Import Data.unixtime
UT <-liftio $ getunixtime
Let T = formatunixtimegmt webdateformat ut "'

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.