| Copyright | (c) digitally induced GmbH 2020 |
|---|---|
| Safe Haskell | None |
| Language | Haskell2010 |
IHP.ViewSupport
Contents
Description
Synopsis
- type HtmlWithContext context = (?context :: context) => Html
- type Layout = Html -> Html
- type Html = HtmlWithContext ControllerContext
- class View theView where
- currentViewId :: (?view :: view, Typeable view) => Text
- forEach :: (MonoFoldable mono, Applicative m) => mono -> (Element mono -> m ()) -> m ()
- isActivePath :: (?context :: ControllerContext, PathString controller) => controller -> Bool
- isActivePathOrSub :: (?context :: ControllerContext, PathString controller) => controller -> Bool
- css :: QuasiQuoter
- onClick :: AttributeValue -> Attribute
- onLoad :: AttributeValue -> Attribute
- theRequest :: (?context :: ControllerContext) => Request
- viewContext :: (?context :: ControllerContext) => ControllerContext
- addStyle :: ConvertibleStrings string Text => string -> Markup
- class ViewFetchHelpMessage
- param :: ViewParamHelpMessage => a
- fetch :: ViewFetchHelpMessage => a
- query :: ViewFetchHelpMessage => a
- isActiveController :: (?context :: ControllerContext, Typeable controller) => Bool
- isActiveAction :: (?context :: ControllerContext, HasPath controllerAction) => controllerAction -> Bool
- nl2br :: (Textual text, ToHtml text) => text -> Html
- stripTags :: Text -> Text
- theCSSFramework :: (?context :: ControllerContext) => CSSFramework
- fromCSSFramework :: forall (field :: Symbol) appliedFunction. (?context :: ControllerContext, KnownSymbol field, HasField field CSSFramework (CSSFramework -> appliedFunction)) => Proxy field -> appliedFunction
- liveReloadWebsocketUrl :: (?context :: ControllerContext) => Text
Documentation
type HtmlWithContext context = (?context :: context) => Html Source #
type Layout = Html -> Html Source #
A layout is just a function taking a view and returning a new view.
Example: A very basic html layout.
myLayout :: Layout
myLayout view = [hsx|
<html>
<body>
{view}
</body>
</html>
|]type Html = HtmlWithContext ControllerContext Source #
class View theView where Source #
Minimal complete definition
Methods
beforeRender :: theView -> IO () Source #
Hook which is called before the render is called
html :: theView -> Html Source #
json :: theView -> Value Source #
Renders the view to a JSON
Instances
| View EmptyView Source # | |
| View HtmlView Source # | |
| View SomeView Source # | Since the only constructor for |
| View a => View [a] Source # | Define how to render a list of views as a view. Just concatenate them together! |
currentViewId :: (?view :: view, Typeable view) => Text Source #
Returns a string to be used as a html id attribute for the current view.
E.g. when calling currentViewId while rendering the view Web.View.Projects.Show, this will return "projects-show"
Useful to automatically scope certain css rules to a specific view. Example:
module Web.View.Projects.Show where
render = [hsx|<div id={currentViewId}>Hello World!</div>|]This will render id="projects-show"Hello World!/div
forEach :: (MonoFoldable mono, Applicative m) => mono -> (Element mono -> m ()) -> m () Source #
Example:
forEach users \user -> putStrLn (tshow user)
Example: Within HSX
renderUser :: User -> Html
renderUser user = [hsx|<div>User: {user.name}</div>|]
render = [hsx|{forEach users renderUser}|]isActivePath :: (?context :: ControllerContext, PathString controller) => controller -> Bool Source #
Returns True when the current request path matches the path given as argument. Takes into account the search query: ?name=value
Example: The browser has requested the url /Projects.
>>>isActivePath "/Projects"True
Returns True because "/Projects" is the current requested path.
>>>isActivePath "/Users"False
Returns false because "/Users" is not "/Projects"
Example: The browser has requested the url /Projects.
>>>isActivePath "/Projects/1"False
This function returns False when a sub-path is request. Use isActivePathOrSub if you want this example to return True.
isActivePathOrSub :: (?context :: ControllerContext, PathString controller) => controller -> Bool Source #
Returns True when the current request path starts with the path given as argument.
Example: The browser has requested the url /Projects/1.
>>>isActivePathOrSub "/Projects"True
Example: The browser has requested the url /Projects.
>>>isActivePathOrSub "/Projects"True
Also see isActivePath.
css :: QuasiQuoter Source #
onClick :: AttributeValue -> Attribute Source #
onLoad :: AttributeValue -> Attribute Source #
theRequest :: (?context :: ControllerContext) => Request Source #
Returns the current request
viewContext :: (?context :: ControllerContext) => ControllerContext Source #
Alias for ?context
addStyle :: ConvertibleStrings string Text => string -> Markup Source #
Adds an inline style element to the html.
This helps to work around the issue, that our HSX parser cannot deal with CSS yet.
Example:
myStyle = addStyle "#my-div { color: blue; }"
[hsx|{myStyle}<div id="my-div">Hello World</div>|]This will render like:
<style>
#my-div { color: blue; }
</style>
<div id="my-div">Hello World</div>class ViewFetchHelpMessage Source #
This class provides helpful compile-time error messages when you use common controller functions inside of your views.
Instances
| (TypeError ('Text "\8216fetch\8216 or \8216query\8216 can only be used inside your controller actions. You have to call it from your controller action and then pass the result to the view.") :: Constraint) => ViewFetchHelpMessage Source # | |
fetch :: ViewFetchHelpMessage => a Source #
query :: ViewFetchHelpMessage => a Source #
isActiveController :: (?context :: ControllerContext, Typeable controller) => Bool Source #
Returns True when the given type matches the type of the currently executed controller action
Example: The browser has requested /Posts and the Posts action of the PostsController is called.
>>>isActiveController @PostsControllerTrue
Returns True because the current action is part of the PostsController
isActiveAction :: (?context :: ControllerContext, HasPath controllerAction) => controllerAction -> Bool Source #
Returns True when the given action matches the path of the currently executed action
Example: The browser has requested /PostsAction.
>>>isActiveAction PostsActionTrue
nl2br :: (Textual text, ToHtml text) => text -> Html Source #
Replaces all newline characters with a br tag. Useful for displaying preformatted text.
>>>nl2br "Hello\nWorld!"[hsx|Hello<br/>World!|]
stripTags :: Text -> Text Source #
Removes all html tags from a given html text
>>>stripTags "This is <b>Bold</b>""This is Bold"
theCSSFramework :: (?context :: ControllerContext) => CSSFramework Source #
fromCSSFramework :: forall (field :: Symbol) appliedFunction. (?context :: ControllerContext, KnownSymbol field, HasField field CSSFramework (CSSFramework -> appliedFunction)) => Proxy field -> appliedFunction Source #
liveReloadWebsocketUrl :: (?context :: ControllerContext) => Text Source #
The URL for the dev-mode live reload server. Typically "ws://localhost:8001"
Orphan instances
| InputValue (PrimaryKey table) => ApplyAttribute (Id' table) Source # | |
| (TypeError (('Text "Looks like you forgot to pass a " ':<>: 'ShowType (GetModelByTableName record)) ':<>: 'Text " id to this data constructor.") :: Constraint) => Eq (Id' record -> controller) Source # | |