You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@click.apache.org by dshah <di...@exit41.com> on 2010/01/25 23:01:17 UTC

Reusing page layout

Hi,
I have a design whereby I am extending several click pages from a basepage.
This base page has some common functionality that all the children need. 
Now, I also have some common rendering that will happen for all the
children.  I wanted to know if what I am thinking would work or if there's a
better way of doing this:

I have the following example:

public class abstract BasePage extends Page { //has a corresponding base.htm
to render this
  /* some common methods, variables etc */
  
  public abstract void doABC() {  //this will be done in the children
classes
     ..
  }
}

public class FirstChildPage extends BasePage { //has a corresponding
first-child.htm to render this
  ...
  public void doABC() { //will have details of what to do for this
    ...
  }
}

public class SecondChildPage extends BasePage { //has a corresponding
second-child.htm to render this
  ...
  public void doABC() { //will have details of what to do for this
    ...
  }
}

Now,  what I was hoping is that I could create a template for base.htm that
can be used in both first-child.htm and second-child.htm.  

What I want to understand is if the only way I can have this common
information (that is in base.htm) be rendered on both first-child.htm and
second-child.htm by including the details as a template ( e.g.
#parse("base.htm")) OR is there some way where it could understand that
since FirstChildPage extends BasePage, it needs to render the base.htm? How
would I achieve the latter without having to include the macro/template in
both first-child.htm and second-child.htm?

Thanks!
Dipita


-- 
View this message in context: http://n2.nabble.com/Reusing-page-layout-tp4457055p4457055.html
Sent from the click-user mailing list archive at Nabble.com.

Re: Reusing page layout

Posted by Frederic Daoud <fd...@proinbox.com>.
> Thanks for the input - I think my case is different from yours in that I
> am
> not calling layout-base.htm to be rendered in either case.. I
> specifically
> want to load first-child.htm if the user does certain things and
> second-child.htm if the user does something else.. 
> In your example, the layout-base.htm would be called no matter what the
> user
> selected right? 

Well, yes and no. If you make a request to NestedPage, the result
will be the combination of nested.htm, layout-nested.htm, and
layout-base.htm. If you create a second page, AnotherPage that
extends NestedLayout, a request to AnotherPage will be rendered
by combining another.htm, layout-nested.htm and layout-base.htm.
So indeed you can load first-child.htm or second-child.htm
depending on what the user did, and have both of them benefit
from a common layout.

Re: Reusing page layout

Posted by dshah <di...@exit41.com>.
Hi Freddy,
Thanks for the input - I think my case is different from yours in that I am
not calling layout-base.htm to be rendered in either case.. I specifically
want to load first-child.htm if the user does certain things and
second-child.htm if the user does something else.. 

In your example, the layout-base.htm would be called no matter what the user
selected right? 
-- 
View this message in context: http://n2.nabble.com/Reusing-page-layout-tp4457055p4461648.html
Sent from the click-user mailing list archive at Nabble.com.

Re: Reusing page layout

Posted by Frederic Daoud <fd...@proinbox.com>.
Hi Dipita,

It sounds like a job for the honorable Panel.

Here's how I like to do it. First, you have your BasePage at the root
of your hierarchy with common functionality and the base structure in
the template. This should be in something else than base.htm, e.g.
layout-base.htm.

Then, for the common functionality between BasePage and the children,
create another page, e.g. NestedLayout, that extends BasePage. Both of
these use Panels to render the children's content. NestedLayout
"decorates" the layout-base.htm in layout-nested.htm, adding content
that will be common to the children.

As Bob said, though, where does your children's content go? In my
example, the content always goes in the same place, and this is
determined by NestedLayout.

Finally, the children, e.g. NestedPage, extend NestedLayout and just
render their specific content in their template. They do not need to
know anything about the parents' templates. The children's final
result will be the combination of the base layout, the nested layout,
and their own content. However the children cannot change where in
the layouts their content gets rendered.

Here is the code:

----------------------------------------------------------------------
BasePage.java

public class BasePage extends Page {
  private Panel body = new Panel("body");

  public BasePage() {
    addControl(body);
  }

  public String getTemplate() {
    return "/layout-base.htm";
  }

  public void onRender() {
    body.setTemplate(getPath());
  }

  protected Panel getBody() {
    return body;
  }
}
----------------------------------------------------------------------
layout-base.htm

<html>
  <head>
    <title>Snippets</title>
    $headElements
  </head>

  <body>
    <h1>Base Layout</h1>
    <p>
      $body
    </p>
  </body>
  $jsElements
</html>
----------------------------------------------------------------------
NestedLayout.java

public class NestedLayout extends BasePage {
  private Panel nestedBody = new Panel("nestedBody");

  public NestedLayout() {
    addControl(nestedBody);
  }

  @Override
  public void onRender() {
    getBody().setTemplate("/layout-nested.htm");
    nestedBody.setTemplate(getPath());
  }
}
----------------------------------------------------------------------
layout-nested.htm

<h2>This is a nested layout</h2>

$nestedBody

<p>Maybe we could even put more content here.</p>
----------------------------------------------------------------------
NestedPage.java

public class NestedPage extends NestedLayout {
}
----------------------------------------------------------------------
nested.htm

<p>This page uses the nested layout.</p>
----------------------------------------------------------------------

Let me know if that helps.

Cheers,
Freddy


On Tue, 26 Jan 2010 16:48:59 +1100, "Bob Schellink" <sa...@gmail.com>
said:
> Hi Dipita,
> 
> That won't be possible, but lets say it was and that the base template
> could automatically be 
> included inside the first-child template. We drop the explicit #parse
> statement so first-child.htm 
> would become:
> 
> first-child.htm:
> <body>
> ...
> </body>
> 
> The question is where would you expect base.htm content to be inserted
> into the first-child.htm 
> template? Before or after the <body> tag? So you would still need to
> indicate (through a Velocity 
> variable) where you want the base template content to be inserted.
> 
> first-child.htm:
> <body>
> $baseContent
> ...
> </body>
> 
> But the above doesn't gain you anything, since you still need to manage
> each child template.
> 
> kind regards
> 
> bob
> 
> 
> On 26/01/2010 02:49 PM, dshah wrote:
> >
> > Hi Bob,
> > Actually, I was trying to achieve something else.. Instead of rendering
> > base.htm (and parsing the children,) I want the children to include base.htm
> > in the children. So I have some common logic that is common among certain
> > pages (which I was putting in basePage). This basePage is an abstract class
> > which expects children to implement certain other required functionality..
> >
> > What I was thinking of doing was putting the render details that is common
> > for all pages (FirstChild and SecondChild) in a template called base.htm.
> > Now the intention was that when first-child.htm is requested, then the
> > details required that are common would be rendered by base.htm.  I can
> > achieve this by included the base.htm template in the first-child.htm and
> > second-child.htm  as specified below. However, I was wondering if there's
> > any way of not having to explicitly include base.htm in both first-child.htm
> > and second-child.htm but instead because of the fact that
> > FirstChildPage.java extends BasePage.java, if somehow it could be
> > interpreted etc?
> >
> > first-child.htm:
> > <body>
> > #parse("base.htm")
> > ..
> > </body>
> >
> > second-child.htm
> > <body>
> > #parse("base.htm")
> > ..
> > </body>
> >
> > I need to be able to sendRedirect to FirstChildPage.java and
> > SecondChildPage.java based on what the user is doing, so I can't always call
> > BasePage.java.
> >
> > Does what I'm doing make any sense and is there way to do this other than
> > how I'm showing it in the above examples?
> >
> > Dipita
> 

Re: Reusing page layout

Posted by dshah <di...@exit41.com>.
Thanks Bob - it makes sense what you are saying - how would click know where
to 'insert' the base template automatically, so the only way to do what I
want to do is kind of how I have it right now..
-- 
View this message in context: http://n2.nabble.com/Reusing-page-layout-tp4457055p4461613.html
Sent from the click-user mailing list archive at Nabble.com.

Re: Reusing page layout

Posted by Bob Schellink <sa...@gmail.com>.
Hi Dipita,

That won't be possible, but lets say it was and that the base template could automatically be 
included inside the first-child template. We drop the explicit #parse statement so first-child.htm 
would become:

first-child.htm:
<body>
...
</body>

The question is where would you expect base.htm content to be inserted into the first-child.htm 
template? Before or after the <body> tag? So you would still need to indicate (through a Velocity 
variable) where you want the base template content to be inserted.

first-child.htm:
<body>
$baseContent
...
</body>

But the above doesn't gain you anything, since you still need to manage each child template.

kind regards

bob


On 26/01/2010 02:49 PM, dshah wrote:
>
> Hi Bob,
> Actually, I was trying to achieve something else.. Instead of rendering
> base.htm (and parsing the children,) I want the children to include base.htm
> in the children. So I have some common logic that is common among certain
> pages (which I was putting in basePage). This basePage is an abstract class
> which expects children to implement certain other required functionality..
>
> What I was thinking of doing was putting the render details that is common
> for all pages (FirstChild and SecondChild) in a template called base.htm.
> Now the intention was that when first-child.htm is requested, then the
> details required that are common would be rendered by base.htm.  I can
> achieve this by included the base.htm template in the first-child.htm and
> second-child.htm  as specified below. However, I was wondering if there's
> any way of not having to explicitly include base.htm in both first-child.htm
> and second-child.htm but instead because of the fact that
> FirstChildPage.java extends BasePage.java, if somehow it could be
> interpreted etc?
>
> first-child.htm:
> <body>
> #parse("base.htm")
> ..
> </body>
>
> second-child.htm
> <body>
> #parse("base.htm")
> ..
> </body>
>
> I need to be able to sendRedirect to FirstChildPage.java and
> SecondChildPage.java based on what the user is doing, so I can't always call
> BasePage.java.
>
> Does what I'm doing make any sense and is there way to do this other than
> how I'm showing it in the above examples?
>
> Dipita


Re: Reusing page layout

Posted by dshah <di...@exit41.com>.
Hi Bob,
Actually, I was trying to achieve something else.. Instead of rendering
base.htm (and parsing the children,) I want the children to include base.htm
in the children. So I have some common logic that is common among certain
pages (which I was putting in basePage). This basePage is an abstract class
which expects children to implement certain other required functionality..

What I was thinking of doing was putting the render details that is common
for all pages (FirstChild and SecondChild) in a template called base.htm. 
Now the intention was that when first-child.htm is requested, then the
details required that are common would be rendered by base.htm.  I can
achieve this by included the base.htm template in the first-child.htm and
second-child.htm  as specified below. However, I was wondering if there's
any way of not having to explicitly include base.htm in both first-child.htm
and second-child.htm but instead because of the fact that
FirstChildPage.java extends BasePage.java, if somehow it could be
interpreted etc?

first-child.htm:
<body>
#parse("base.htm")
..
</body>

second-child.htm
<body>
#parse("base.htm")
..
</body>

I need to be able to sendRedirect to FirstChildPage.java and
SecondChildPage.java based on what the user is doing, so I can't always call
BasePage.java.

Does what I'm doing make any sense and is there way to do this other than
how I'm showing it in the above examples?

Dipita
-- 
View this message in context: http://n2.nabble.com/Reusing-page-layout-tp4457055p4458704.html
Sent from the click-user mailing list archive at Nabble.com.

Re: Reusing page layout

Posted by Bob Schellink <sa...@gmail.com>.
Hi Dipita,

I could be wrong but it sounds as if you are looking to create a BorderPage with an associated 
template that can render child templates:

http://incubator.apache.org/click/docs/user-guide/html/ch02s06.html

Basically the base.htm template will #parse the child templates, instead of the other way around.

base.htm:

   <html>
    <body>
      #parse($path)
    </body>
   </html>


Let me know if this is not what you are after.

kind regards

bob


On 26/01/2010 09:01 AM, dshah wrote:
>
> Hi,
> I have a design whereby I am extending several click pages from a basepage.
> This base page has some common functionality that all the children need.
> Now, I also have some common rendering that will happen for all the
> children.  I wanted to know if what I am thinking would work or if there's a
> better way of doing this:
>
> I have the following example:
>
> public class abstract BasePage extends Page { //has a corresponding base.htm
> to render this
>    /* some common methods, variables etc */
>
>    public abstract void doABC() {  //this will be done in the children
> classes
>       ..
>    }
> }
>
> public class FirstChildPage extends BasePage { //has a corresponding
> first-child.htm to render this
>    ...
>    public void doABC() { //will have details of what to do for this
>      ...
>    }
> }
>
> public class SecondChildPage extends BasePage { //has a corresponding
> second-child.htm to render this
>    ...
>    public void doABC() { //will have details of what to do for this
>      ...
>    }
> }
>
> Now,  what I was hoping is that I could create a template for base.htm that
> can be used in both first-child.htm and second-child.htm.
>
> What I want to understand is if the only way I can have this common
> information (that is in base.htm) be rendered on both first-child.htm and
> second-child.htm by including the details as a template ( e.g.
> #parse("base.htm")) OR is there some way where it could understand that
> since FirstChildPage extends BasePage, it needs to render the base.htm? How
> would I achieve the latter without having to include the macro/template in
> both first-child.htm and second-child.htm?
>
> Thanks!
> Dipita
>
>