播放框架2:呈现父模板内容的正确方法是什么? [英] Play framework 2 : What is a proper way to render the contents of parent template?

查看:77
本文介绍了播放框架2:呈现父模板内容的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,当模板使用继承时,可以呈现父模板的内容.例如,在Twig中:

Normally, when a template uses inheritance, it's possible to render the contents of parent template. For example, in Twig:

{% extends "base.html" %}

{% block sidebar %}
    {{ parent() }}
    ...
{% endblock %}

这意味着,如果有时我不需要父模板的内容,则只需删除parent()函数.如何在Scala模板中实现此目标?

It means that if I sometimes don't need the contents of parent template, I just simply remove the parent() function. How can I achieve this in Scala template?

推荐答案

您可以先在 child 视图中创建HTML块,然后将其传递给父级( 布局 ):

You can create a HTML block in child view first and then pass it to parent (layout):

@sidebar = {
    <div>MySidebar</div>
}

@parent(sidebar){
    <div>Main content of the child view</div>
}

因此在parent.scala.html布局中,您将像使用

So in parent.scala.html layout you will use it just like

@(sidebar: Html = null)(content: Html)

<div id="parent-wrap">
    @sidebar
    @content
</div>

当然,如果您要对许多子页面使用相同的HTML代码,那么如果您使用标签,而不是在每个视图中声明@sidebar块,则效果会更好.请记住,标记仅是scala视图(模板),您可以将其作为任何其他视图包含在内.只需在app/views/tags中创建一个普通的视图,即:sidebar.scala.html具有必需的HTML块,因此您以后可以在类似的地方使用它:

Of course if you gonna to use the same HTML code for many subpages you'll do better if you use a tag instead of declaring @sidebar block in each view. Remember that tag is nothing more then just a scala view (template) and you can include it as any other view. Just create a normal view in app/views/tags i.e.: sidebar.scala.html with required HTML block, so you can later use it somewhere like:

<div class="span3">@tags.sidebar("content to pass")</div>

或作为传递到更高层布局的块,等等:

or as a block passed to higher level layout, etc :

@parent(tags.sidebar(additionalContent)){
    <div>Main content of the child view</div>
}

TBH我从不知道包含视图和标签之间有什么区别;)

TBH I never knew what's the difference between including views and tags ;)

这篇关于播放框架2:呈现父模板内容的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆