411 REFERENCE · Wikibooks
Yesod web framework/View
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
View
The Handler monad returns content in one or more of several formats as components of types that implement the HasReps classhttp://hackage.haskell.org/packages/archive/yesod-core/latest/doc/html/Yesod-Content.html#t:HasReps The class HasReps Hackage.haskell.org 2012-10-23 {RepHtml, RepJson, RepXml, RepPlain, the dual RepHtmlJson, a pair or list of pairs [(ContentType, Content)], ..}.http://www.yesodweb.com/book/restful-content RESTful Content Yesodweb.com 2012-10-23http://hackage.haskell.org/packages/archive/yesod-core/latest/doc/html/Yesod-Content.html#t:ToContent The class ToContent Hackage.haskell.org 2012-10-23 Json examples:http://www.yesodweb.com/blog/2012/04/yesod-js-todo More Client Side Yesod: todo sample Yesodweb.com 2012-04-23 2012-10-23http://www.yesodweb.com/book/json-web-service JSON Web Service Yesodweb.com 2012-10-23http://hackage.haskell.org/package/yesod-json The yesod-json package Hackage.haskell.org 2012-10-23
The HasReps default implementation of chooseRep chooses the document representation to be returned according to the preferred content-type list of the client accept header.
Widgetshttp://www.yesodweb.com/book/widgets book - Widgets Yesodweb.com 2012-10-23 are HTML DOM code snippets made by specific commands (e.g. setTitle) or from templates of structure (html) / behaviour (javascript) / style (css), whose types instantiate the classes ToWidget, ToWidgetHead or ToWidgetBody.
A Widget monad,http://hackage.haskell.org/packages/archive/yesod-core/latest/doc/html/Yesod-Widget.html#t:GWidget The widget monad Hackage.haskell.org 2012-10-23 based on a Writerhttp://www.haskell.org/haskellwiki/All_About_Monads#The_Writer_monad The Writer monad Haskell.org 2012-10-23 one and argument to defaultLayout, facilitate to piece the widgets together.
Template interpolation - Shakespearean templates
See ref.http://www.yesodweb.com/book/shakespearean-templates book - Shakesperean templates Yesodweb.com 2012-10-23
These are content view templates that follow a common substitution pattern of code expressions within curly brackets with different character prefix to refer to
template expressions with ^{...} other templates of the same type as ^{template params},
route expressions with @{...} safe (typed) urls as @{HomeR},
message expressions with _{...} i18n message rendering as _{MsgMessage params}
other Haskell expressions with #{...} haskell expression rendering as #{haskell_expression} which type must be convertible
in case of hamlet html templates, the expression type must be an instance of Text.Blaze.ToMarkuphttp://hackage.haskell.org/packages/archive/blaze-markup/latest/doc/html/Text-Blaze.html#t:ToMarkup Class Text.Blaze.ToMarkup Hackage.haskell.org 2012-10-23
in case of css templates, the expression type must be an instance of Text.Cassius.ToCsshttp://hackage.haskell.org/packages/archive/shakespeare/latest/doc/html/Text-Cassius.html#t:ToCss Class Text.Cassius.ToCss Hackage.haskell.org 2012-10-23
in case of javascript templates, the expression type must be an instance of Text.Julius.ToJavascript http://hackage.haskell.org/packages/archive/shakespeare/latest/doc/html/Text-Julius.html#t:ToJavascript Class Text.Julius.ToJavascript Hackage.haskell.org 2012-10-23
in case of i18n message definitions (in "isoLang.msg" files) with parameter interpolations, the expression type must be an instance of Text.Shakespeare.I18N.ToMessage http://hackage.haskell.org/packages/archive/shakespeare/latest/doc/html/Text-Shakespeare-I18N.html#t:ToMessage Class Text.Shakespeare.I18N.ToMessage Hackage.haskell.org 2012-10-24
in case of plain text templates, the expression type must be an instance of Text.Shakespeare.Text.ToText http://hackage.haskell.org/packages/archive/shakespeare/latest/doc/html/Text-Shakespeare-Text.html#t:ToText Class Text.Shakespeare.Text.ToText Hackage.haskell.org 2012-10-24
Using non-English text in expressions requires use of the Unicode-aware type Text, since GHC's show for the type String renders non-ASCII characters as escaped numerical codes.
external file templates: Template content can be loaded from external files using compile time splice calls as $(expr).http://www.haskell.org/ghc/docs/latest/html/users_guide/template-haskell.html Template Haskell haskell.org 2012-11-03
reload mode for external files: See doc.
Localizable (i18n) messages
See ref.http://www.yesodweb.com/book/internationalization book - Internationalization Yesodweb.com 2012-10-23
For every supported language ISO name there should be a file in the messages subfolder as <iso-language>.msg with entries like
ArticleUnexistant param@Int64: unexistant article #{param}
For each entry in en.msg a message constructor is generated, prefixing the message name by "Msg", so the example msg. can be referred as
-- in code
myMsg = MsgArticleUnexistant myArticleId
-- in templates
_{MsgArticleUnexistant myArticleId}
Actual i18n support is missing from the stack app template. You have to add the mkMessage "MyApp" directory isoLangDefault to the "Foundation.hs" file to get the messages instantiated.mkMessage
HTML-like templates
the hamlet quasiquoter (a parser to compile-time Template Haskell code)http://www.haskell.org/haskellwiki/Quasiquotation HaskellWiki - QuasiQuotation Haskell.org 2012-05-26 2012-10-23http://www.haskell.org/ghc/docs/latest/html/users_guide/template-haskell.html#th-quasiquotation Template Haskell Quasi-quotation Haskell.org 2012-11-02 specified in the T.H. Oxford brackets syntax [qq| ... |] introduces an indentation based structured html template with '$' prefixed lines of logic statements (See doc.).https://hackage.haskell.org/package/shakespeare/docs/Text-Hamlet.html The hamlet template module Hackage.haskell.org 2012-10-23 Automatic closing tags are generated only for the tag at line start position.
the whamlet quasiquoter returns a Widget expression. (saves toWidget before [hamlet|..|]).
toWidget [hamlet|
$doctype 5
<html>
<head>
<title>#{pageTitle} - My Site
<link rel=stylesheet href=@{Stylesheet_route}>
<body>
<div>
^{headerTemplate}
<div>
<p><span style="font-weight:bold;">_{MsgArticleListTitle}</span>
$if null articles
<p>_{MsgSorryNoArticles}
$else
<ul>
$forall art <- articles
<li>#{articleNumber art} .- #{articleTitle art}
<div>
^{footerHamletTemplate}
|]
JavaScript templates
the julius quasiquoter: introduces a javascript template.https://hackage.haskell.org/package/shakespeare/docs/Text-Julius.html The Julius template module Hackage.haskell.org 2012-10-23 Javascript variants CoffeeScript and Roy-languagehttp://roy.brianmckenna.org/ Roy language Roy.brianmckenna.org 2012-10-23 have also specific quasiquoters.
toWidgetHead [julius|
var myfunc = function(){document.location = "@{SomeRouteR}";}
^{extraJuliusTemplate}
|]
CSS-like templates
the cassius quasiquoter: introduces a css template with indentation based structuring.https://hackage.haskell.org/package/shakespeare/docs/Text-Cassius.html The Cassius template module Hackage.haskell.org 2012-10-23
toWidget [cassius|
.box
border: 1px solid #{myColor}
background-image: url(@{MyImageR})
^{extraCassiusTemplate}
|]
the lucius quasiquoter: introduces a css template with standard syntax plus shakespeare-template style substitutions.https://hackage.haskell.org/package/shakespeare/docs/Text-Lucius.html The Lucius template module Hackage.haskell.org 2012-10-23
toWidgetHead [lucius|
.box { border: 1px solid #{myColor} ;
background-image: url(@{MyImageR}) ;
}
^{extraLuciusTemplate}
|]
Plain text templates
for e-mail or text/plain http content type.https://hackage.haskell.org/package/shakespeare/docs/Text-Shakespeare-Text.html Shakespeare plain text templates module Hackage.haskell.org 2012-10-24
templates: lt: lazy text, st: strict text
templates for text with a left margin delimiter '|': lbt (lazy), sbt (strict)
[lt| Mr./Mrs. #{fullName} ...
|]
Specific views
Search engines XML Sitemaps,http://hackage.haskell.org/package/yesod-sitemap The yesod-sitemap package Hackage.haskell.org 2012-10-26 where sitemap returns an XML Sitemap as http response, with the routes we want the search engines to crawl, and attributes to instruct the crawler, from a provided list of SitemapUrl records.
Navigation Breadcrumbs.http://hackage.haskell.org/packages/archive/yesod-core/latest/doc/html/Yesod-Core.html#t:YesodBreadcrumbs The YesodBreadcrumbs class Hackage.haskell.org 2012-11-05 You have to provide a YesodBreadcrumbs instance for the site where the generator function breadcrumb should return a title and parent route for each one. Then, the query function breadcrumbs will return the present route title and ancestors' (route, title) pairs.
Web feed views (RSS / Atom).http://hackage.haskell.org/package/yesod-newsfeed The yesod-newsfeed package for RSS / Atom views Hackage.haskell.org 2012-10-26 You have handlers that return RepRss, RepAtom, or dual RepAtomRss content (to be selected on accept headers' preferred content-type list) from a given Feed structure.
References
2
100%
__NOTOC__