Kapparino
Kapparino

Reputation: 988

Include Specific Content For Some Pages

I have a layout.tml, where I defined common things I wish for my each page to have. However, now I have a problem, when I want to include specific content (marketing for example) for some page. I guess this is wrong idea, but inside my layout.tml I created marketingBlock. I want this, to be hidden until I call it somewhere, like in case page2.tml, where I want that page to include this block. Problem is, it does not show up.

So how do I do this?

<t:block id="marketingBlock">
    <div class="row marketing">
       <h4>Marketing Name</h4>
       <img />
        <p></p>
    </div>
</t:block>

Layout.tml

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" >
    <head>
        <title>${title}</title>
        ...
    </head>

    <body>
        <div class="container">
            <div class="header">
                <nav>
                    ...
                </nav>
                <h3 class="text-muted">Site Name</h3>
            </div>

            <t:body />

            <!-- i want this portion to be included for some specific pages only -->
            <t:block id="marketingBlock">
                <div class="row marketing">
                    <h4>Marketing Name</h4>
                    <img />
                    <p></p>
                </div>
            </t:block>

            <footer class="footer">
                <p>&copy; Company 2015</p>
            </footer>
        </div>
    </body>  
</html>

Page2.tml

<html t:type="layout" title="TapestryTest Index"
      t:sidebarTitle="Framework Version"
      xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"
      xmlns:p="tapestry:parameter">

      <body>
            <div class="content">
                ...
            </div>

      <t:block id="marketingBlock" />

</html>

Upvotes: 2

Views: 118

Answers (1)

Bob Harner
Bob Harner

Reputation: 754

Think of it the other way around. Your Layout class is a component, and like any Tapestry component it can have parameters, and a parameter can be a block of HTML from the parent page. So if you want each page to provide different marketing content to the Layout component, the approach that you want is to pass a block from from the containing page to the Layout component via the Layout component's "marketing" parameter.

See the example at the bottom of http://tapestry.apache.org/layout-component.html -- in that example a block of CSS is being passed into the Layout component, but it could just as easily be a block of HTML.

So your Page2.tml would look like this:

<html t:type="layout" title="TapestryTest Index"
      xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"
      xmlns:p="tapestry:parameter">
<p:marketing>
    <div class="row marketing">
        <h4>Marketing Name</h4>
        <img />
        <p></p>
    </div>
</p:marketing>
<body>
    <div class="content">
                ...
    </div>
</body>

</html>

Alternatively, if you want multiple pages to have the same marketing content but allow some pages to have no marketing content at all, then you should put the marketing div inside your Layout template (as in your example), and have each parent page just pass in a boolean parameter ("showMarketing") that controls whether that div should appear. Then you can just put a component in your Layout that tests that boolean.

So your Layout template would have this:

<t:if test="showMarketing">
    <div class="row marketing">
        <h4>Marketing Name</h4>
        <img />
        <p></p>
    </div>
</t:if>

and each page would have a "showMarketing" parameter like this at the top, set to either "true" or "false":

<html t:type="layout" showMarketing="true" title="TapestryTest Index"
  xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"
  xmlns:p="tapestry:parameter">

Upvotes: 1

Related Questions