2017-02-06 129 views
0
import Data.String.Conversions 
import Data.Maybe (isJust) 
import qualified Heist 
import qualified Heist.Interpreted as I 
import qualified Heist.Compiled as HeistCom 
import Heist.Internal.Types 
import qualified Text.XmlHtml as X 
import Data.List (sortBy) 
import Data.Map.Syntax 
import Data.ByteString.Builder (toLazyByteString) 

renderTemplate :: String -> (HeistState IO -> HeistState IO) -> ActionM() 
renderTemplate fileName hsBinding = do 
    let emptyI = return() :: MapSyntax Text (I.Splice IO) 
    let emptyC = return() :: MapSyntax Text (HeistCom.Splice IO) 
    let emptyA = return() :: MapSyntax Text (AttrSplice IO) 
    let spliceConfig = SpliceConfig emptyI emptyI emptyC emptyA [] (\_ -> False):: SpliceConfig IO 
    heist <- lift $ Heist.initHeist (HeistConfig spliceConfig "" True) 
    case heist of 
    Right heist' -> do 
     rendered <- lift $ I.renderTemplate (hsBinding heist') $ convertString fileName 
     case (rendered) of 
     Just (builder, _) -> do 
      lift $ print $ toLazyByteString builder 
     Nothing -> error "heist error" 
    Left a -> error . convertString $ show a 

我打電話的功能是這樣的:渲染搶劫模板沒有返回

renderTemplate "templates/compareForm" $ I.bindSplice "test" $ I.textSplice "abcxyz" 

我猜它是與配置做。我沒有徹底想通過上述配置。

不幸的是,以上只是產生一個「heist錯誤」(第二行)的錯誤。所以我的問題是爲什麼?我的下一步將是調查Heist.Interpreted.renderTemplate函數。

+0

是否'renderTemplate'工作,沒有拼接一個非常簡單的模板(甚至只是'')和'id'你'(HeistState IO - > HeistState IO) '功能? – Libby

+0

與上述建議相同的結果。 –

回答

1

終於想通了這一點...我需要指定一個模板位置...

renderTemplate :: String -> (HeistState IO -> HeistState IO) -> ActionM() 
renderTemplate fileName hsBinding = do 
    let emptyI = return() :: MapSyntax Text (I.Splice IO) 
    let emptyC = return() :: MapSyntax Text (HeistCom.Splice IO) 
    let emptyA = return() :: MapSyntax Text (AttrSplice IO) 
    let templateLocations = [Heist.loadTemplates "templates/"] 
    let spliceConfig = SpliceConfig emptyI emptyI emptyC emptyA templateLocations (\_ -> False):: SpliceConfig IO 
    heist <- lift $ Heist.initHeist (HeistConfig spliceConfig "" True) 
    case heist of 
    Right heist' -> do 
     rendered <- lift $ I.renderTemplate (hsBinding heist') $ convertString fileName 
     case (rendered) of 
     Just (builder, _) -> do 
      html . convertString $ toLazyByteString builder 
     Nothing -> error "heist error" 
    Left a -> error . convertString $ show a 

,不應該指定渲染路徑的目錄:

renderTemplate "compareForm" $ I.bindSplice "test" $ I.textSplice "abcxyz" 

以上可能不工作編譯模板,可能需​​要修改此行(可能是(\_ -> False)let spliceConfig = SpliceConfig emptyI emptyI emptyC emptyA templateLocations (\_ -> False):: SpliceConfig IO

有與上述潛在的性能問題,請參閱https://github.com/snapframework/heist/issues/102