如下:
instance View EditView where
html EditView { ... } = [hsx|
<nav>
<ol class="breadcrumb"/span>>
<li class="breadcrumb-item"><a href={PostsAction}>Posts< /a> </li>
<li class="breadcrumb-item active">Edit Post< /li>
</ol>
</nav>
<h1>Edit Post</h1>
{renderForm post}。
|]
是來自IHP指南的創建你的第一個專案部分中的Blog專案的代碼。
我在這里把它從HSX轉換為blaze-html:
instance View EditView where
html EditView { ... } =do
H.nav $ do.
H.ol ! A.class_ "breadcrumb" $ do !
H.li ! A.class_ "breadcrumb-item" $ {span class="hljs-keyword">do
H.a ! A.href "PostAction" $ do !
"Post"H.li ! A.class_ "breadcrumb-item active" $ do
"編輯帖子"
H.h1 "Edit Post"
renderForm post
我想知道的最后一點是:
<a href={PostsAction}>Posts< /a>
如果我做了以下作業:
H.a ! A.href PostsAction $ do!
"Post"/span>
我得到這個訊息:
- Couldn't match expected type 'H. AttributeValue'。
與實際的type 'PostsController'。
- In第一個引數of'A.href',即'PostsAction'
在第二個引數的'(!)',即'A.href PostSction'
In運算式。H.a ! A.href PostsActiontypecheck的表達方式。
PostsAction。
Defined at /home/dharmatech/Dropbox/Documents/ihp-log/blog/Web/Types. hs:13:7。
有什么好的方法可以將PostsAction傳遞給A.href?
(如果有其他方法可以使blaze-html的表達方式更成文,也歡迎推薦。 :-))
更新1
。當我按照Willem在他下面的回答中所建議的思路,使用下面的方法時:
instance View EditView where
html EditView { ... } =do
H.nav $ do.
H.ol ! A.class_ "breadcrumb" $ do !
H.li ! A.class_ "breadcrumb-item" $ {span class="hljs-keyword">do
H.a ! A.href (fromString (show PostsAction)) $ do
"Post"/span>
H.li ! A.class_ "breadcrumb-item active" $ do
"編輯帖子"
H.h1 "Edit Post"
renderForm post
我得到的結果是:
- Couldn't匹配type 'Text' with '[Char]'/span>
Expected type: String。
Actual type: Text
- In第一個引數of'fromString',即
'(顯示PostsAction)'
In第一個引數of'A.href',即
'(fromString (顯示PostsAction))
在第二個引數的'(!)',即
'A.href (fromString (show PostsAction))'typecheck
也許我需要匯入一個Text版本的fromString?
更新2
我已經添加了以下匯入:import Data.String(IsString(fromString))
但是,錯誤資訊是一樣的:
- Couldn't匹配type 'Text' with '[Char]'/span>
Expected type: String。
Actual type: Text
- In第一個引數of'fromString',即
'(顯示PostsAction)'
In第一個引數of'A.href',即
'(fromString (顯示PostsAction))
在第二個引數的'(!)',即
'A.href (fromString (show PostsAction))'typecheck
Update 3
如果我把滑鼠懸停在show上,就會顯示以下簽名:
show :: forall a. Show a => a -> Text
而對于fromString:
fromString :: forall a. IsString a => String -> a
因此我相信這就是不匹配的地方。
更新4
這是一個典型的IHP視圖檔案,我在頂部有以下內容用于匯入:這是個典型的IHP視圖檔案。
module Web.View.Post.Edit where?
import Web.View.Prelude
import qualified Text.Blaze.Html5 as H
import qualified Text.Blaze.Html5.Attributesas A
uj5u.com熱心網友回復:
正如關于Inline Haskell的檔案所說:
如果變數是另一個 HSX 運算式、一個 blaze HTML 元素、一個文本或字串:它就會像你所期望的那樣被包含。如果變數是任何其他自定義的Haskell資料結構:它將首先通過呼叫
show將其轉換為字串表示。你可以添加一個自定義的ToHtml(從IHP.HSX.ToHtml中匯入)實體,以自定義渲染資料結構。
因此,除非在[hsx|...]準繩中為{PostAction}有一個ToHTML,因此你應該應用show,接下來我們呼叫fromString :: IsString a => String -> aAttributeValue。
因此,該代碼相當于:
instance View EditView where
html EditView { ... } =do
H.nav $ do.
H.ol ! A.class_ "breadcrumb" $ do !
H.li ! A.class_ "breadcrumb-item" $ {span class="hljs-keyword">do
H.a ! A.href (fromString (show PostsAction)) $ do
"Post"/span>
H.li ! A.class_ "breadcrumb-item active" $ do
"編輯帖子"
H.h1 "Edit Post"
renderForm post
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/316946.html
標籤:
