7

Play Framework 2 テンプレート言語は非常に優れています。ただし、これは Microsoft の Razor 言語に「触発された」ものですが、1 つの重要な設計上の決定が異なります。それは、HTML に「エスケープ」する方法です。Razor は HTML スタイルのタグを探し、Play 2 はある種のヒューリスティックを使用します。

HTML の複数の「セクション」を取り、ヘッダーと目次を含むページを生成するテンプレートを作成しようとしています。私の「structuredpage.scala.html」は次のようになります。

@(title: String)(sections: Pair[String,Html]*)

@main(title){
    <nav class="page-links">
        @makeTableOfContents(sections)
    </nav>
    @for(section <- sections){
        <section id="@section._1">
            <h2>@section._1</h2>
            @section._2
        </section>
    }
}

その 2 番目のパラメーターは可変数のセクションであることに注意してください。Play テンプレート言語でこれを呼び出す方法はないようです。

Common.section次のようなヘルパー関数を作成しました。

    def section(title: String)(content: Html) = title -> content;

私はこれを試しました:

@()
@import views.Common.section

@structuredpage("Dashboard")(
    section("Latest Requests") {
        <p>Blah</p>
    },
    section("Your Details") {
        <p>Blah blah</p>
    }
)

…これはtype mismatch; found : scala.xml.Elem required: play.api.templates.Html5 行目に表示されます。つまり、<p>Blah</p>テンプレート ドキュメント HTML としてではなく、Scala として解釈されています。

この:

@()
@import views.Common.section

@structuredpage("Dashboard"){
    @section("Latest Requests") {
        <p>Blah</p>
    },
    @section("Your Details") {
        <p>Blah blah</p>
    }
}

…これはtype mismatch; found : play.api.templates.Html required: (String, play.api.templates.Html)3 行目に表示されます。つまり、外側の中かっこブロック全体が、Scala コードではなく、テンプレート ドキュメント HTML として解釈されています!

イライラすることに、それらは Play 2 の公式ドキュメントの一部のコード サンプルと大きく異なるようには見えません

何か案は?Play Framework 2.0.4 を使用しています

4

3 に答える 3

1

これがあなたが探しているかもしれないものです。完全にFPではないけど

構造化ページ.scala.html

@(title: String)(content: scala.collection.mutable.MutableList[Pair[String, Html]] => Unit)

@main(title){
    @defining(new scala.collection.mutable.MutableList[Pair[String,Html]]()) { sections =>
        @content(sections)
        @for(section <- sections){
            <section id="@section._1">
                <h2>@section._1</h2>
                @section._2
            </section>
        }
    }
}

frontpage.scala.html

@()

@import views.Common.section

@structuredpage("Front Page") { implicit sections =>
    @section("Section 1") {
        <h1>stuff</h1>
    }

    @section("Section 2") {
        <h1>more stuff</h1>
    }
}

セクション方法:

def section(title: String)(content: Html)(implicit sections: scala.collection.mutable.MutableList[Pair[String, Html]]) {
    sections += title -> content
}
于 2012-12-14T16:09:47.993 に答える
0

現在、このマシンでこれをテストすることはできませんが、以下は機能するはずです(補助的な方法は必要ありません)。

@()

@structuredpage("Dashboard"){
   ("Latest Requests", {
        <p>Blah</p>
    }),
   ("Your Details", {
        <p>Blah blah</p>
    })
}
于 2012-12-13T15:52:41.697 に答える
0

回避策は次のとおりです。

@import views.Common.section

@sec1 = { <p>Blah</p> }

@sec2 = { <p>Blah blah</p> }

@structuredpage("Dashboard")(
    section("Latest Requests")(sec1),
    section("Your Details")(sec2)
)

以前の試行:

あなたの願いは複雑だと思います。テンプレートはシンプルにする必要があります。これは簡単な代替手段です:

index.scala.html

@structuredpage("Dashboard"){
    @section("Latest Requests") {
        <p>Blah</p>
    }

    @section("Your Details") {
        <p>Blah blah</p>
    }
}

section.scala.html

@(title: String)(content: Html)

<section id="@title">
    <h2>@title</h2>
    @content
</section>

構造化ページ.scala.html

@(title: String)(sections: Html)

@main(title){
    <nav class="page-links">
        table-of-contents goes here
    </nav>
    @sections
}

要点を作成しました: https://gist.github.com/4280577。だから、あなたはそれをチェックして、それで遊ぶことができます.

于 2012-12-13T22:16:56.917 に答える