6

私はオンライン調査用のサイトを書いています。すべてが 1 つの HTML ページに表示される質問のリストがあり、リストの長さは不明です。各質問にはテンプレートに保存されたフォームがqu1.tplあり、ページはqu.tplです。今私はしたい:

  1. qu1.tpl質問ごとにいくつかの名前を置き換えます

  2. qu.tpl一度にいくつかのものを交換する

  3. qu1.tplのすべてのインスタンス化を貼り付けますqu.tpl

<qulist/>チュートリアルで学んだことを使用して、タグを再帰的に<apply template="qu1.tpl"><qulist/>in qu.tplusing localHeistandに置き換えようとしましたbindStringが、qu.tpl既にレンダリングされているため、新しく挿入された適用タグが解決されないため、これは機能しません。

代わりに何をすべきですか?

(これはより一般的な質問だと思います。答えが当てはまる他のアプリケーションを思いつくことができる場合は、検索エンジン用のテキストとタグを追加してください。)

4

3 に答える 3

1

ircでこれを手伝ってくれたmightybyteに感謝します。私が自分自身に答えるのを禁止された8時間後、これが同じ答えの私の変種です:

  1. テンプレート qu1.tpl を読み取り、それをインスタンス化 (つまり、リストに質問の名前と番号を入力) し、それを返すスプライスを構築します。heist 関数の callTemplate がそれを助けます。(このスプライスは、以下の擬似コードでは splicex と呼ばれます。)

  2. splicex を折りたたむ別の splice を作成して、単一の質問ではなく (インスタンス化された) 質問のリストを取得します。(疑似コード内の関数 splice.)

  3. bindString の代わりに bindSplice を使用します。

疑似コード (テスト後に変更し、コンテキストから切り離したもの) -

 ... -> let
          splice :: Monad m => Splice m
          splice = foldM (\ ts (s, i) ->
                             liftM ((++) ts) $ splicex (quName, quNumber))
                         []
                         (zip questionName [0..])

          splicex :: Monad m => (String, Int) -> Splice m
          splicex (quName, quNumber) =
              do
                mt <- callTemplate "qu1"
                        [ ("question-name", Data.Text.pack quName)
                        , ("question-number", Data.Text.pack $ show quNumber)
                        ]

                case mt of
                  Nothing -> error "splice rendering failed."
                  Just (t :: Template) -> return t
         in
           -- fill in the list of (instatiated) questions
           heistLocal (bindSplice "qulist" splice) $
           -- before that, instantiate the overall page
           instantiatePage $
           render "qu"

ところで、私の sourceforge アバターは弱すぎて新しいタグを作成できません。これに「heist」のタグを付けたい人はいますか?

リンク:

http://snapframework.com/

freenode IRC ネットワーク上の #snapframework チャネル。

http://snapframework.com/docs/tutorials/heist

于 2011-07-22T19:50:30.677 に答える
0

テンプレート ループを理解しようとしたとき、この質問に何度も出くわしました。悲しいことに、バージョン 0.5 (またはそれ以下) ではバージョン 0.6 (推測) で runChildrenWith が導入されました。

runChildrenWith を使用する簡単な例は次のとおりです。

list_test_entries.tpl:

<listTestEntries>
  <dt><testEntry/></dt>
  <dd>This is part of the repeated template</dd>
</listTestEntries>

サイト.hs:

listTestEntriesHandler :: Handler App App  ()
listTestEntriesHandler = do
    results <- getData 
    renderWithSplices "list_test_entries"
        ("listTestEntries" ## listTestEntriesSplice results)

getData :: Handler App App [String]
getData = return ["1", "2", "3"]

listTestEntriesSplice  :: [String] -> I.Splice AppHandler
listTestEntriesSplice = I.mapSplices (I.runChildrenWith . listTestEntrySplice)

listTestEntrySplice :: Monad m => String -> Splices (HeistT n m Template)
listTestEntrySplice dataEntry = do
  "testEntry" ## I.textSplice (T.pack $ "data: " ++ dataEntry)

実行中のデモについては、https://github.com/michaxm/heist-template-loop-demoを参照してください。

于 2014-07-20T15:17:33.503 に答える