Skip to content

Commit 0fd3c6f

Browse files
authored
Merge pull request #284 from yesodweb/minor-docs
book: Minor changes for Yesod 1.6
2 parents 8a54252 + 2bc9be6 commit 0fd3c6f

File tree

5 files changed

+47
-49
lines changed

5 files changed

+47
-49
lines changed

book/asciidoc/basics.asciidoc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ main = warp 3000 HelloWorld
3535
----
3636

3737
If you save that code in +helloworld.hs+ and run it with +runhaskell
38-
helloworld.hs+, you'll get a web server running on port 3000. Note,
38+
helloworld.hs+, you'll get a web server running on port 3000. Note,
3939
if you followed the quick start guide and installed yesod with +stack+
40-
then you will not have +runhaskell+ and will need to run +stack runghc
41-
helloworld.hs+ instead. If you point your browser to http://localhost:3000,
40+
then you will not have +runhaskell+ and will need to run +stack runghc
41+
helloworld.hs+ instead. If you point your browser to http://localhost:3000,
4242
you'll get the following HTML:
4343

4444
[source, html]
@@ -112,7 +112,7 @@ instance YesodDispatch HelloWorld where
112112
"GET" -> getHomeR
113113
_ -> badMethod
114114
115-
type Handler = HandlerT HelloWorld IO
115+
type Handler = HandlerFor HelloWorld
116116
----
117117

118118
NOTE: In addition to using +-ddump-splices+, it can often be useful to generate
@@ -174,11 +174,11 @@ In our example, we pass +[whamlet|Hello World!|]+ to +defaultLayout+.
174174
+whamlet+ is another quasi-quoter. In this case, it converts Hamlet syntax into
175175
a Widget. Hamlet is the default HTML templating engine in Yesod. Together with
176176
its siblings Cassius, Lucius and Julius, you can create HTML, CSS and
177-
Javascript in a fully type-safe and compile-time-checked manner. We'll see much
177+
JavaScript in a fully type-safe and compile-time-checked manner. We'll see much
178178
more about this in the Shakespeare chapter.
179179

180180
Widgets are another cornerstone of Yesod. They allow you to create modular
181-
components of a site consisting of HTML, CSS and Javascript and reuse them
181+
components of a site consisting of HTML, CSS and JavaScript and reuse them
182182
throughout your site. We'll get into more detail on them in the widgets
183183
chapter.
184184

@@ -212,7 +212,7 @@ NOTE: By the way, the word Yesod (יסוד) means _foundation_ in Hebrew.
212212
Once again we mention +HelloWorld+ in our main function. Our foundation
213213
contains all the information we need to route and respond to requests in our
214214
application; now we just need to convert it into something that can run. A
215-
useful function for this in Yesod is +warp+, which runs the Warp webserver with
215+
useful function for this in Yesod is +warp+, which runs the Warp web server with
216216
a number of default settings enabled on the specified port (here, it's 3000).
217217

218218
One of the features of Yesod is that you aren't tied down to a single

book/asciidoc/haskell.asciidoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ which are available for reading online:
1212
* link:http://book.realworldhaskell.org/read[Real World Haskell]
1313

1414
Additionally, there are a number of great articles on
15-
link:https://www.fpcomplete.com/school[School of Haskell].
15+
link:https://tech.fpcomplete.com/haskell/learn/[FP Complete's Haskell's hub].
1616

1717
In order to use Yesod, you're going to have to know at least the basics of
1818
Haskell. Additionally, Yesod uses some features of Haskell that aren't covered
@@ -36,10 +36,10 @@ This is one of the core building blocks for a strongly typed
3636
language like Haskell. Some data types, like +Int+, can be treated as primitive
3737
values, while other data types will build on top of these to create more
3838
complicated values. For example, you might represent a person with:
39-
+
39+
+
4040
[source, haskell]
4141
data Person = Person Text Int
42-
+
42+
+
4343
Here, the +Text+ would give the person's name, and the +Int+ would give the
4444
person's age. Due to its simplicity, this specific example type will recur
4545
throughout the book. There are essentially three ways you can create a new data
@@ -128,7 +128,7 @@ are two other common approaches:
128128
* In your +cabal+ file, add an +default-extensions+ block.
129129

130130
I personally never use the GHC command line argument approach. It's a personal
131-
preference, but I like to have my settings clearly stated in a file. In general
131+
preference, but I like to have my settings clearly stated in a file. In general,
132132
it's recommended to avoid putting extensions in your +cabal+ file; however,
133133
this rule mostly applies when writing publicly available libraries. When you're
134134
writing an application that you and your team will be working on, having all of
@@ -287,7 +287,7 @@ with this].
287287
A nice trick is that TH code is allowed to perform arbitrary +IO+ actions, and
288288
therefore we can place some input in external files and have it parsed at
289289
compile time. One example usage is to have compile-time checked HTML, CSS, and
290-
Javascript templates.
290+
JavaScript templates.
291291

292292
If your Template Haskell code is being used to generate declarations, and is
293293
being placed at the top level of our file, we can leave off the dollar sign and

book/asciidoc/introduction.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Since web programming began, people have been trying to make the development
66
process a more pleasant one. As a community, we have continually pushed new
77
techniques to try and solve some of the lingering difficulties of security
88
threats, the stateless nature of HTTP, the multiple languages (HTML, CSS,
9-
Javascript) necessary to create a powerful web application, and more.
9+
JavaScript) necessary to create a powerful web application, and more.
1010

1111
Yesod attempts to ease the web development process by playing to the strengths
1212
of the Haskell programming language. Haskell’s strong compile-time guarantees

book/asciidoc/shakespearean-templates.asciidoc

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
== Shakespearean Templates
22

33
Yesod uses the Shakespearean family of template languages as its standard
4-
approach to HTML, CSS and Javascript creation. This language family shares some
4+
approach to HTML, CSS and JavaScript creation. This language family shares some
55
common syntax, as well as overarching principles:
66

77
* As little interference to the underlying language as possible, while
@@ -28,7 +28,7 @@ Julius is for Javascript, and Cassius and Lucius are both for CSS. Hamlet and
2828
Cassius are both whitespace-sensitive formats, using indentation to denote
2929
nesting. By contrast, Lucius is a superset of CSS, keeping CSS's braces for
3030
denoting nesting. Julius is a simple passthrough language for producing
31-
Javascript; the only added feature is variable interpolation.
31+
JavaScript; the only added feature is variable interpolation.
3232

3333
NOTE: Cassius is in fact just an alternate syntax for Lucius. They both use the
3434
same processing engine underneath, but Cassius files have indentation converted
@@ -84,7 +84,7 @@ section.blog
8484
background-image: url(@{MyBackgroundR})
8585
----
8686

87-
==== Julius (Javascript)
87+
==== Julius (JavaScript)
8888

8989
String interpolation works slightly differently in Julius. This is important
9090
from a security standpoint — all interpolated values are valid JSON by default
@@ -121,7 +121,7 @@ name. It might look like this:
121121
<p>Hello, my name is #{name}
122122
----
123123

124-
124+
125125
NOTE: +#{...}+ is how we do variable interpolation in Shakespeare.
126126

127127
What should happen to +name+, and what should its datatype be? A naive approach
@@ -189,11 +189,11 @@ _http://example.com/display/time_ displays the current time. And let's say we
189189
want to link from the homepage to the time. I can think of three different ways
190190
of constructing the URL:
191191

192-
. As a relative link: _../display/time_
192+
. As a relative link: _../display/time_
193193

194-
. As an absolute link, without a domain: _/display/time_
194+
. As an absolute link, without a domain: _/display/time_
195195

196-
. As an absolute link, with a domain: _http://example.com/display/time_
196+
. As an absolute link, with a domain: _http://example.com/display/time_
197197

198198
There are problems with each approach: the first will break if either URL
199199
changes. Also, it's not suitable for all use cases; RSS and Atom feeds, for
@@ -230,7 +230,7 @@ renderMyRoute :: MyRoute -> Text
230230
renderMyRoute Home = "http://example.com/profile/home"
231231
renderMyRoute Time = "http://example.com/display/time"
232232
----
233-
233+
234234
[NOTE]
235235
====
236236
URL rendering functions are actually a bit more complicated than this. They
@@ -273,7 +273,7 @@ this would translate roughly into the Haskell code:
273273

274274
All Shakespearean languages share the same interpolation syntax, and are able
275275
to utilize type-safe URLs. They differ in the syntax specific for their target
276-
language (HTML, CSS, or Javascript). Let's explore each language in turn.
276+
language (HTML, CSS, or JavaScript). Let's explore each language in turn.
277277

278278
==== Hamlet Syntax
279279

@@ -635,7 +635,7 @@ $doctype 5
635635
<p>All done.
636636
----
637637

638-
638+
639639
NOTE: There is an older and still supported syntax: three exclamation points
640640
(+!!!+). You may still see this in code out there. We have no plans to remove
641641
support for this, but in general find the +$doctype+ approach easier to read.
@@ -653,7 +653,7 @@ in a few more features.
653653
* You can declare variables in your templates.
654654

655655
* A set of CSS properties can be created as a mixin, and reused in multiple
656-
declarations.
656+
declarations.
657657

658658
Starting with the second point: let's say you want to have some special styling
659659
for some tags within your +article+. In plain ol' CSS, you'd have to write:
@@ -743,12 +743,12 @@ simple example:
743743
==== Julius Syntax
744744

745745
Julius is the simplest of the languages discussed here. In fact, some might
746-
even say it's really just Javascript. Julius allows the three forms of
746+
even say it's really just JavaScript. Julius allows the three forms of
747747
interpolation we've mentioned so far, and otherwise applies no transformations
748748
to your content.
749749

750750
NOTE: If you use Julius with the scaffolded Yesod site, you may notice that
751-
your Javascript is automatically minified. This is not a feature of Julius;
751+
your JavaScript is automatically minified. This is not a feature of Julius;
752752
instead, Yesod uses the hjsmin package to minify Julius output.
753753

754754
=== Calling Shakespeare
@@ -873,7 +873,7 @@ There are a few changes:
873873

874874
* We use a different set of functions, prefixed with an "s". So the quasiquoter
875875
is +shamlet+ and the external file function is +shamletFile+. How we
876-
pronounce those is still up for debate.
876+
pronounce this is still up for debate.
877877

878878
* No URL interpolation is allowed. Doing so will result in a compile-time
879879
error.
@@ -920,7 +920,7 @@ $doctype 5
920920
<p>_{Apples count}
921921
----
922922

923-
923+
924924

925925
This kind of a template now needs some way to turn those values into HTML. So
926926
just like type-safe URLs, we pass in a render function. To represent this, we
@@ -981,7 +981,7 @@ main = putStrLn $ renderHtml
981981

982982
=== Other Shakespeare
983983

984-
In addition to HTML, CSS and Javascript helpers, there is also some more
984+
In addition to HTML, CSS and JavaScript helpers, there is also some more
985985
general-purpose Shakespeare available. shakespeare-text provides a simple way
986986
to create interpolated strings, much like people are accustomed to in scripting
987987
languages like Ruby and Python. This package's utility is definitely not

book/asciidoc/widgets.asciidoc

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
== Widgets
22

33
One of the challenges in web development is that we have to coordinate three
4-
different client-side technologies: HTML, CSS and Javascript. Worse still, we
4+
different client-side technologies: HTML, CSS and JavaScript. Worse still, we
55
have to place these components in different locations on the page: CSS in a
6-
style tag in the head, Javascript in a script tag before the closing body tag, and HTML in the
7-
body. And never mind if you want to put your CSS and Javascript in separate
6+
style tag in the head, JavaScript in a script tag before the closing body tag, and HTML in the
7+
body. And never mind if you want to put your CSS and JavaScript in separate
88
files!
99

1010
In practice, this works out fairly nicely when building a single page, because
11-
we can separate our structure (HTML), style (CSS) and logic (Javascript). But
11+
we can separate our structure (HTML), style (CSS) and logic (JavaScript). But
1212
when we want to build modular pieces of code that can be easily composed, it
1313
can be a headache to coordinate all three pieces separately. Widgets are
1414
Yesod's solution to the problem. They also help with the issue of including
@@ -118,7 +118,7 @@ page. In particular:
118118
* External stylesheets
119119
* External Javascript
120120
* CSS declarations
121-
* Javascript code
121+
* JavaScript code
122122
* Arbitrary +<head>+ content
123123
* Arbitrary +<body>+ content
124124

@@ -148,7 +148,7 @@ style code appear in a separate file. The scaffolded site provides this for you
148148
automatically.
149149

150150
But what if you want to add some +<meta>+ tags, which need to appear in
151-
the head? Or if you want some Javascript to appear in the body instead of the
151+
the head? Or if you want some JavaScript to appear in the body instead of the
152152
head? For these purposes, Yesod provides two additional type classes:
153153
+ToWidgetHead+ and +ToWidgetBody+. These work exactly as they seem they should. One example use case for this is to have fine-grained control of where your +<script>+ tags end up getting inserted.
154154

@@ -207,7 +207,7 @@ referring to files hosted on a CDN, like Google's jQuery.
207207
=== Combining Widgets
208208

209209
The whole idea of widgets is to increase composability. You can take these
210-
individual pieces of HTML, CSS and Javascript, combine them together into
210+
individual pieces of HTML, CSS and JavaScript, combine them together into
211211
something more complicated, and then combine these larger entities into
212212
complete pages. This all works naturally through the +Monad+ instance of
213213
+Widget+, meaning you can use do-notation to compose pieces together.
@@ -322,26 +322,24 @@ answer is that each widget is a value of type +Widget+. But if you look through
322322
the Yesod libraries, you'll find no definition of the +Widget+ type. What
323323
gives?
324324

325-
Yesod defines a very similar type: +data WidgetT site m a+. This data type is a
326-
*monad transformer*. The last two arguments are the underlying monad and the
327-
monadic value, respectively. The site parameter is the specific foundation
328-
type for your individual application. Since this type varies for each and every
329-
site, it's impossible for the libraries to define a single +Widget+ datatype
330-
which would work for every application.
325+
Yesod defines a very similar type: +data WidgetFor site a+. This data
326+
type is a *monad transformer*. The last argument +a+ is the monadic
327+
value. The site parameter is the specific foundation type for your
328+
individual application. Since this type varies for each and every
329+
site, it's impossible for the libraries to define a single +Widget+
330+
datatype which would work for every application.
331331

332332
Instead, the +mkYesod+ Template Haskell function generates this type synonym
333333
for you. Assuming your foundation data type is called +MyApp+, your +Widget+
334334
synonym is defined as:
335335

336336
[source, haskell]
337337
--------
338-
type Widget = WidgetT MyApp IO ()
338+
type Widget = WidgetFor MyApp ()
339339
--------
340340

341341
We set the monadic value to be +()+, since a widget's value will ultimately be
342-
thrown away. +IO+ is the standard base monad, and will be used in almost all
343-
cases. The only exception is when writing a *subsite*. Subsites are a more
344-
advanced topic, and will be covered later in their own chapter.
342+
thrown away.
345343

346344
Once we know about our +Widget+ type synonym, it's easy to add signatures to
347345
our previous code samples:
@@ -372,14 +370,14 @@ page =
372370
----
373371

374372
When we start digging into handler functions some more, we'll encounter a
375-
similar situation with the +HandlerT+ and +Handler+ types.
373+
similar situation with the +HandlerFor+ and +Handler+ types.
376374

377375
=== Using Widgets
378376

379377
It's all well and good that we have these beautiful Widget datatypes, but how
380378
exactly do we turn them into something the user can interact with? The most
381379
commonly used function is +defaultLayout+, which essentially has the type
382-
signature +Widget -> Handler Html+.
380+
signature +WidgetFor -> HandlerFor Html+.
383381

384382
+defaultLayout+ is actually a typeclass method, which can be overridden for
385383
each application. This is how Yesod apps are themed. So we're still left with
@@ -535,7 +533,7 @@ tricks which were previously employed.
535533
=== Summary
536534

537535
The basic building block of each page is a widget. Individual snippets of HTML,
538-
CSS, and Javascript can be turned into widgets via the polymorphic +toWidget+
536+
CSS, and JavaScript can be turned into widgets via the polymorphic +toWidget+
539537
function. Using do-notation, you can combine these individual widgets into
540538
larger widgets, eventually containing all the content of your page.
541539

0 commit comments

Comments
 (0)