开发者

Is it possible to split doLayout up in a template calling page?

开发者 https://www.devze.com 2023-03-11 11:45 出处:网络
In the play framework views you can perform the major import of code into a base template file using the doLayout tag.

In the play framework views you can perform the major import of code into a base template file using the doLayout tag.

However say I have in my base file a few areas that are completely seperated e.g

<html>
<head></head>
<body>
<div1>doLayout</div1>
<div2>some text in here thats standard across all views...</div2>
<div3>doLayout..again</div> 
</body>
</html>

So in my views开发者_StackOverflow中文版 that are using this base html file I may want to link in a photograph in div1 and div 2 place in some text but I want the doLayouts to be separated and take into account the content(div2) separating them.


Play supports a powerful #{set /} tag that enables you to supply a fragment of code and assign it to a placeholder in your HTML page. For example, you might see this in some code samples (don't remember which one though).

In layout.html:

<head>
    <link href="skin.css'}" rel="stylesheet" type="text/css" media="all" />
    #{get 'moreStyles' /}
</head>

In any_page.html:

<div id='normal_dolayout_body'>
    <p>This is the doLayout body</[p>
</div>

#{set 'moreStyles'}
   <link href="extra.css" rel='stylesheet' type='text/css' media='all' />
#{/set}

Resulting page will have both CSS declaractions. Now for creating complex layouts, you use both #set and #include tags like this:

in layout.html:

<!--div.container start -->
<div class="container">  
<div class="center">
<div id="layout">
        <div id='content'>
    #{doLayout/}
</div>
<div id='sidebar'>
    #{get 'sidebar'/}
    </div>
</div>
</div>
<!--div.container end -->

<!--div.footer start -->
<div class="footer">
    #{get 'promo' /}
    #{get 'footerlinks' /}
</div>
<!--div.footer end -->

in any_page.html:

<div id='normal_body'>Will be included by doLayout</div>

<!-- You may have any number of #set tags which will be matched with any #get declared in your main layout. -->
#{set 'promo'}
   #{include 'my_custom_promo.html'/}
#{/set}

#{set 'sidebar'}
    #{if user.isHappy }
        #{include '/Commons/happy_user_sidebar.html' /}
    #{/if}
    #{else}
        #{include '/Commons/default_sidebar.html' /}
    #{/else}
#{/set}

The promo and sidebar blocks will be injected at the associated #get placeholder in the template.

As you can we, with the #set and include tags, you may apply DRY principles to your page assembly, with a block-like approach to assembling custom page content. You may even define your own custom tag to hide complex logic in a nice #{sidebar user:user /} tag if you want to make things easier for your designers.


Using the doLayout tag, then no, you can't achieve what you are asking. The doLayout uses the extends tag in tandem to figure out where the layout is injected into the master layout, but it takes no arguments. Therefore, if you tried to call doLayout multiple times, then how would it know which portion is being injected?

I see that you have two options.

The first option you have, is to use the includes tag instead. This would allow you to do something like

#{include 'head.html' /}
your top layout code
#{include 'middle-common.html' /}
your bottom layout code
#{include 'foot.html' /}

More information on the difference between include and doLayout can be found here - What is the difference between doLayout and include in a template in 'Java Play!'?

Your second option is to use CSS to perform the layouts properly. In theory, you should be able to put your in the header, and simply use CSS to position it correctly.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号