[opencms-dev] Trying to include a file and have template applied

M Butcher mbutcher at grcomputing.net
Mon Nov 10 20:26:00 CET 2003


Jim,

How are you including the template? Does the _content_ reference the 
template or does the _jsp_ reference the template.

If the _content_ (the body) references the template, then you will not 
see the template get rendered at all, because cms.include doesn't apply 
templates, it just includes the body.

If the _jsp_ references the template, then the output you get ought to be:

<h2>This is from the template!!!</h2>

    <html>
    <head>
    <title>Test Page</title>
    </head>
    <body leftMargin="0" topMargin="0" marginheight="0" marginwidth="0">
    <h1>This is myJSP.jsp</h1>
     
    <h1>including myPage.html.</h1>
    <h3>This is the body content</h3>

</body>
</html>

Note that the heading is printed before the <html> tag because the 
template gets applied before the JSP is written out. In browsers that 
auto-repair HTML, the header may get dropped off, since it is overtly 
illegal HTML.

If I might suggest something, I think you are approaching the problem 
the wrong way. The template should have the layout information, the JSP 
should have the logic, and the page should have content. I would suggest 
structuring things this way:

The template should include the JSP _and_ the content, OR the template 
should render the JSP, which then includes the content. All of the main 
layout info (<html><head/><body/></html>) should be in the template, not 
the JSP.

So, in the JSP's properties, it should reference the template. If the 
JSP is going to include the content of myPage, then it doesn't matter 
what the template of myPage is set to, as it will be ignored by the JSP 
anyway.

This way, the output will be constructed like this:

render template -> include JSP -> include content

Hope I didn't misunderstand you. :)

Matt

Polizzi, Jim wrote:

> Let me restate the problem - with much more definition.
>  
> I'm writing a jsp page and trying to include a file (.html - or 
> whatever) that has a template defined for it (thus allow the user to 
> create a body - but I define the formatting, etc.).  I call 
> "cms.include("myPage.html");".  The results that I get are the BODY of 
> the included file but not the Template stuff (my formatting stuff - 
> tables, etc.)
>  
> I've created a very simple example (below) that shows exactly what I mean.
>  
>  
> Let's say that I have the following template: (myTemplate.jsp)
>
>     <%@ page session="false"
>         import="com.opencms.flex.jsp.*"
>         import="com.opencms.flex.util.CmsMessages"
>         import="com.opencms.file.*"
>         import="java.lang.*"
>         import="java.util.*"
>     %>
>     <%
>      
>     // initialise Cms Action Element
>     CmsJspActionElement cms = new CmsJspActionElement(pageContext,
>     request, response);
>      
>     out.println("<h2>This is from the template!!!</h2>");
>
>     cms.include(null, "body");
>
> Also - let's say that you have a page (myPage.html) that uses 
> myTemplate.jsp - with the following body content:
>
>     <h3>This is the body content</h3>
>
> Good - now - let's say that we have the following JSP file - (myJSP.jsp):
>
>     <%@ page session="false"
>         import="com.opencms.flex.jsp.*"
>         import="com.opencms.flex.util.CmsMessages"
>         import="com.opencms.file.*"
>         import="java.lang.*"
>         import="java.util.*"
>     %>
>     <%
>     // initialise Cms Action Element
>     CmsJspActionElement cms = new CmsJspActionElement(pageContext,
>     request, response);
>     %>
>      
>     <html>
>     <head>
>     <title>Test Page</title>
>     </head>
>     <body leftMargin="0" topMargin="0" marginheight="0" marginwidth="0">
>     <h1>This is myJSP.jsp</h1>
>      
>     <%
>     out.println("<h1>including myPage.html.</h1>");
>     cms.include("myPage.html");
>     %>
>      
>     </body>
>     </html>
>
> Ok - that's all of the files in this example.  When I open myJSP.jsp, 
> the results are:
>
>     <html>
>     <head>
>     <title>Test Page</title>
>     </head>
>     <body leftMargin="0" topMargin="0" marginheight="0" marginwidth="0">
>     <h1>This is myJSP.jsp</h1>
>      
>     <h1>including myPage.html.</h1>
>     <h3>This is the body content</h3>
>
>     </body>
>     </html>
>      
>
> NOTE:  I DON'T get the Template output -->    <h2>This is from the 
> template!!!</h2>
> I don't understand why not - or how I CAN get it.
>  
> Any help in this would be appreciated.
>  
> Jim Polizzi
>
>     -----Original Message-----
>     *From:* Claus Priisholm [mailto:cpr at codedroids.com]
>     *Sent:* Monday, November 10, 2003 5:57 AM
>     *To:* opencms-dev at opencms.org
>     *Subject:* Re: [opencms-dev] Trying to include a file and have
>     template applied
>
>     I'm not sure what kind of include you're using (and what it is
>     that you need the template to do), but I had a problem where an
>     include of a header 'page' would mess up the remaining includes on
>     the page. In order for that to work I would either have to use a
>     Java URLConnection to actually fetch the result or apply this
>     workaround provided by Brett Beaumont:
>
>     http://mail.opencms.org/pipermail/opencms-dev/2003q3/007010.html
>
>     On fredag, nov 7, 2003, at 17:00 Europe/Copenhagen, Polizzi, Jim
>     wrote:
>
>         I am trying to include a file (.html) that has a template
>         defined for it.  The problem is that the file is included -
>         but the template is not applied... I'm ONLY getting the body
>         content - without the template
>         Can anyone shed some light on this?
>
>     -- 
>     Claus Priisholm
>     +45 48 22 46 46
>     cpr (you-know-what) interlet.dk - cpr (you-know-what) codedroids.com
>     http://www.interlet.dk - http://www.codedroids.com
>





More information about the opencms-dev mailing list