001: package org.apache.turbine.modules.layouts;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: import org.apache.ecs.ConcreteElement;
026:
027: import org.apache.turbine.TurbineConstants;
028: import org.apache.turbine.modules.Layout;
029: import org.apache.turbine.modules.ScreenLoader;
030: import org.apache.turbine.services.velocity.TurbineVelocity;
031: import org.apache.turbine.util.RunData;
032: import org.apache.turbine.util.template.TemplateNavigation;
033:
034: import org.apache.velocity.context.Context;
035:
036: /**
037: * This Layout module allows Velocity templates to be used as layouts.
038: * Since dynamic content is supposed to be primarily located in
039: * screens and navigations there should be relatively few reasons to
040: * subclass this Layout.
041: *
042: * To get the same functionality as with VelocityECSLayout, you can use two
043: * supplied VelocityMacros, TurbineHtmlHead and TurbineHtmlBodyAttributes
044: * in your templates. These are used to put HtmlPageAttributes into a page
045: * before rendering.
046: *
047: * Use these macros should be used in the Layout template like this:
048: *
049: * ... set things like style sheets, scripts here.
050: * <html>
051: * #TurbineHtmlHead()
052: * <body #TurbineHtmlBodyAttributes() >
053: * .... your body information
054: * </body>
055: * </html>
056: *
057: * As the layout template is rendered _after_ the screen template, you
058: * can of course, add information to the $page tool in your screen template.
059: * This will be added correctly to the <head>...</head> and
060: * <body> tags.
061: *
062: * @author <a href="mailto:john.mcnally@clearink.com">John D. McNally</a>
063: * @author <a href="mailto:mbryson@mont.mindspring.com">Dave Bryson</a>
064: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
065: * @version $Id: VelocityOnlyLayout.java 534527 2007-05-02 16:10:59Z tv $
066: */
067: public class VelocityOnlyLayout extends Layout {
068: /** Logging */
069: private static Log log = LogFactory
070: .getLog(VelocityOnlyLayout.class);
071:
072: /** The prefix for lookup up layout pages */
073: private String prefix = TurbineConstants.LAYOUT_PREFIX + "/";
074:
075: /**
076: * Build the layout. Also sets the ContentType and Locale headers
077: * of the HttpServletResponse object.
078: *
079: * @param data Turbine information.
080: * @exception Exception a generic exception.
081: */
082: public void doBuild(RunData data) throws Exception {
083: // Get the context needed by Velocity.
084: Context context = TurbineVelocity.getContext(data);
085:
086: String screenName = data.getScreen();
087:
088: log.debug("Loading Screen " + screenName);
089:
090: // First, generate the screen and put it in the context so
091: // we can grab it the layout template.
092: ConcreteElement results = ScreenLoader.getInstance().eval(data,
093: screenName);
094:
095: String returnValue = (results == null) ? "" : results
096: .toString();
097:
098: // variable for the screen in the layout template
099: context.put(TurbineConstants.SCREEN_PLACEHOLDER, returnValue);
100:
101: // variable to reference the navigation screen in the layout template
102: context.put(TurbineConstants.NAVIGATION_PLACEHOLDER,
103: new TemplateNavigation(data));
104:
105: // Grab the layout template set in the VelocityPage.
106: // If null, then use the default layout template
107: // (done by the TemplateInfo object)
108: String templateName = data.getTemplateInfo()
109: .getLayoutTemplate();
110:
111: // Set the locale and content type
112: data.getResponse().setLocale(data.getLocale());
113: data.getResponse().setContentType(data.getContentType());
114:
115: log.debug("Now trying to render layout " + templateName);
116:
117: // Finally, generate the layout template and send it to the browser
118: data.getOut().print(
119: TurbineVelocity.handleRequest(context, prefix
120: + templateName));
121: }
122: }
|