blob: 9c49da12080cfc79a1c44bf66e923106ab672b33 [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<meta name="Date-Revision-yyyymmdd" content="20140918"/>
<meta http-equiv="Content-Language" content="en"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Tag Developers Guide</title>
<link href="//fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,400italic,600italic,700italic" rel="stylesheet" type="text/css">
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
<link href="/css/main.css" rel="stylesheet">
<link href="/css/custom.css" rel="stylesheet">
<link href="/highlighter/github-theme.css" rel="stylesheet">
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="/bootstrap/js/bootstrap.js"></script>
<script type="text/javascript" src="/js/community.js"></script>
</head>
<body>
<a href="http://github.com/apache/struts" class="github-ribbon">
<img style="position: absolute; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub">
</a>
<header>
<nav>
<div role="navigation" class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" data-toggle="collapse" data-target="#struts-menu" class="navbar-toggle">
Menu
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a href="/index.html" class="navbar-brand logo"><img src="/img/struts-logo.svg"></a>
</div>
<div id="struts-menu" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="dropdown">
<a data-toggle="dropdown" href="#" class="dropdown-toggle">
Home<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="/index.html">Welcome</a></li>
<li><a href="/download.cgi">Download</a></li>
<li><a href="/releases.html">Releases</a></li>
<li><a href="/announce.html">Announcements</a></li>
<li><a href="http://www.apache.org/licenses/">License</a></li>
<li><a href="https://www.apache.org/foundation/thanks.html">Thanks!</a></li>
<li><a href="https://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li>
</ul>
</li>
<li class="dropdown">
<a data-toggle="dropdown" href="#" class="dropdown-toggle">
Support<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="/mail.html">User Mailing List</a></li>
<li><a href="https://issues.apache.org/jira/browse/WW">Issue Tracker</a></li>
<li><a href="/security.html">Reporting Security Issues</a></li>
<li class="divider"></li>
<li><a href="https://cwiki.apache.org/confluence/display/WW/Migration+Guide">Version Notes</a></li>
<li><a href="https://cwiki.apache.org/confluence/display/WW/Security+Bulletins">Security Bulletins</a></li>
<li class="divider"></li>
<li><a href="/maven/project-info.html">Maven Project Info</a></li>
<li><a href="/maven/struts2-core/dependencies.html">Struts Core Dependencies</a></li>
<li><a href="/maven/struts2-plugins/modules.html">Plugin Dependencies</a></li>
</ul>
</li>
<li class="dropdown">
<a data-toggle="dropdown" href="#" class="dropdown-toggle">
Documentation<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="/birdseye.html">Birds Eye</a></li>
<li><a href="/primer.html">Key Technologies</a></li>
<li><a href="/kickstart.html">Kickstart FAQ</a></li>
<li><a href="https://cwiki.apache.org/confluence/display/WW/Home">Wiki</a></li>
<li class="divider"></li>
<li><a href="/getting-started/">Getting Started</a></li>
<li><a href="/security/">Security Guide</a></li>
<li><a href="/core-developers/">Core Developers Guide</a></li>
<li><a href="/tag-developers/">Tag Developers Guide</a></li>
<li><a href="/maven-archetypes/">Maven Archetypes</a></li>
<li><a href="/plugins/">Plugins</a></li>
<li><a href="/maven/struts2-core/apidocs/index.html">Struts Core API</a></li>
<li><a href="/tag-developers/tag-reference.html">Tag reference</a></li>
<li><a href="https://cwiki.apache.org/confluence/display/WW/FAQs">FAQs</a></li>
<li><a href="http://cwiki.apache.org/S2PLUGINS/home.html">Plugin registry</a></li>
</ul>
</li>
<li class="dropdown">
<a data-toggle="dropdown" href="#" class="dropdown-toggle">
Contributing<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="/youatstruts.html">You at Struts</a></li>
<li><a href="/helping.html">How to Help FAQ</a></li>
<li><a href="/dev-mail.html">Development Lists</a></li>
<li><a href="/contributors/">Contributors Guide</a></li>
<li class="divider"></li>
<li><a href="/submitting-patches.html">Submitting patches</a></li>
<li><a href="/builds.html">Source Code and Builds</a></li>
<li><a href="/coding-standards.html">Coding standards</a></li>
<li><a href="https://cwiki.apache.org/confluence/display/WW/Contributors+Guide">Contributors Guide</a></li>
<li class="divider"></li>
<li><a href="/release-guidelines.html">Release Guidelines</a></li>
<li><a href="/bylaws.html">PMC Charter</a></li>
<li><a href="/volunteers.html">Volunteers</a></li>
<li><a href="https://gitbox.apache.org/repos/asf?p=struts.git">Source Repository</a></li>
<li><a href="/updating-website.html">Updating the website</a></li>
</ul>
</li>
<li class="apache"><a href="http://www.apache.org/"><img src="/img/apache.png"></a></li>
</ul>
</div>
</div>
</div>
</nav>
</header>
<article class="container">
<section class="col-md-12">
<a class="edit-on-gh" href="https://github.com/apache/struts-site/edit/master/source/tag-developers/extending-themes.md" title="Edit this page on GitHub">Edit on GitHub</a>
<h1 class="no_toc" id="extending-themes">Extending Themes</h1>
<ul id="markdown-toc">
<li><a href="#creating-a-new-theme-from-scratch" id="markdown-toc-creating-a-new-theme-from-scratch">Creating a New Theme from Scratch</a></li>
<li><a href="#wrapping-an-existing-theme" id="markdown-toc-wrapping-an-existing-theme">Wrapping an Existing Theme</a> <ul>
<li><a href="#wrapping-a-control" id="markdown-toc-wrapping-a-control">Wrapping a control</a></li>
<li><a href="#extending-an-existing-theme" id="markdown-toc-extending-an-existing-theme">Extending an Existing Theme</a></li>
</ul>
</li>
<li><a href="#special-parameters" id="markdown-toc-special-parameters">Special parameters</a></li>
</ul>
<p>Most often, an application may just need to override a template (see <a href="template-loading.html">Template Loading</a>) so that
a certain control renders differently. Or, an application may need to add a new template to an existing theme. Other
times, you might want to create an entirely new theme, perhaps because you are building a rich set of unique and reusable
templates for your organization.</p>
<p>There are three ways to create new themes:</p>
<ul>
<li>Create a new theme from scratch (<strong>hard!</strong>)</li>
<li>Wrap an existing theme</li>
<li>Extend an existing theme</li>
</ul>
<h2 id="creating-a-new-theme-from-scratch">Creating a New Theme from Scratch</h2>
<p>It’s probably never a good idea to create a new theme from scratch. Instead, use the <a href="simple-theme.html">simple theme</a>
as a starting point. The simple theme provides just enough foundation to make it easy to create new controls by extending
or wrapping the basic controls. Before starting a new theme, be sure to review the source templates for all of the provided
themes. The existing themes are your best guide to creating new themes.</p>
<h2 id="wrapping-an-existing-theme">Wrapping an Existing Theme</h2>
<p>The <a href="xhtml-theme.html">xhtml theme</a> provides several good examples of the “wrapping” technique.
The <a href="simple-theme.html">simple theme</a> renders the basic control. The xhtml theme “dresses up” many of the controls
by adding a header and footer.</p>
<h3 id="wrapping-a-control">Wrapping a control</h3>
<pre><code class="language-ftl">&lt;#include "/${parameters.templateDir}/${parameters.expandTheme}/controlheader.ftl" /&gt;
&lt;#include "/${parameters.templateDir}/simple/xxx.ftl" /&gt;
&lt;#include "/${parameters.templateDir}/${parameters.expandTheme}/controlfooter.ftl" /&gt;
</code></pre>
<p>Wrapping is a great way to augment the basic HTML elements provided by the simple theme.</p>
<h3 id="extending-an-existing-theme">Extending an Existing Theme</h3>
<p>One benefit of object-orientated programming is that it lets us “design by difference.” We can extend an object and code
only the behaviour that changes. Themes provide a similar capability. The subdirectory that hosts a theme can contain
a <code class="highlighter-rouge">theme.properties</code> file. A <code class="highlighter-rouge">parent</code> entry can be added to the property file to designate a theme to extend.
The <a href="ajax-theme.html">ajax theme</a> extends the <a href="xhtml-theme.html">xhtml theme</a> using this technique.</p>
<p><strong>/template/ajax/theme.properties</strong></p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>parent = xhtml
</code></pre></div></div>
<p>An extended theme does not need to implement every single template that the <a href="struts-tags.html">Struts Tags</a> expect.
It only needs to implement the templates that change. The other templates are loaded from the parent template.</p>
<h2 id="special-parameters">Special parameters</h2>
<p><a href="http://struts.apache.org/maven/struts2-core/apidocs/org/apache/struts2/components/UIBean.html">UIBean</a> provides few
special parameters which can be used to build a new template (they are already used in <code class="highlighter-rouge">xhtml</code> and <code class="highlighter-rouge">css_xhtml</code> theme):</p>
<ul>
<li><code class="highlighter-rouge">templateDir</code> - current value of templateDir parameter, see <a href="selecting-template-directory.html">Selecting Template Directory</a></li>
<li><code class="highlighter-rouge">theme</code> - used theme, see <a href="selecting-themes.html">Selecting Themes</a></li>
<li><code class="highlighter-rouge">template</code> - name of the template file to use (i.e. text)</li>
<li><code class="highlighter-rouge">themeExpansionToken</code> - a special token used to indicate to search for a template also in parent theme (when used with
<code class="highlighter-rouge">&lt;#include /&gt;</code> directive)</li>
<li><code class="highlighter-rouge">expandTheme</code> - tells internal template loader mechanism to try load template from current theme and then from parent
theme (and parent theme, and so on)</li>
</ul>
<p>Using <code class="highlighter-rouge">expandTheme</code> parameter allows to override only some parts of the theme’s templates, e.g. <code class="highlighter-rouge">css.ftl</code>. You can
define a new theme (set <code class="highlighter-rouge">theme.properties</code>) and override just single file.</p>
<p><code class="highlighter-rouge">${parameters.expandTheme}</code> is a recurrence which tells <code class="highlighter-rouge">ThemeManager</code> to load template from current theme and then
from parent theme (defined in <code class="highlighter-rouge">theme.properties</code>) and so on.</p>
<p>Please also notice that the <code class="highlighter-rouge">ThemeManager</code> builds list of possible templates based on current theme and inherited
themes (<code class="highlighter-rouge">/template/custom/textarea.ftl</code>, <code class="highlighter-rouge">/template/xhtml/textarea.ftl</code>, <code class="highlighter-rouge">/template/simple/textarea.ftl</code>). This is also
true for templates which are loaded via <code class="highlighter-rouge">${parameters.expandTheme}</code>.</p>
<blockquote>
<p>See also example <a href="">Struts 2 Themes</a> or <a href="http://www.vitarara.org/cms/struts_2_cookbook/creating_a_theme">Creating a Theme in Struts 2</a> (Mark Menard)</p>
</blockquote>
</section>
</article>
<footer class="container">
<div class="col-md-12">
Copyright &copy; 2000-2018 <a href="http://www.apache.org/">The Apache Software Foundation </a>.
All Rights Reserved.
</div>
<div class="col-md-12">
Apache Struts, Struts, Apache, the Apache feather logo, and the Apache Struts project logos are
trademarks of The Apache Software Foundation.
</div>
<div class="col-md-12">Logo and website design donated by <a href="https://softwaremill.com/">SoftwareMill</a>.</div>
</footer>
<script>!function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (!d.getElementById(id)) {
js = d.createElement(s);
js.id = id;
js.src = "//platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js, fjs);
}
}(document, "script", "twitter-wjs");</script>
<script src="https://apis.google.com/js/platform.js" async="async" defer="defer"></script>
<div id="fb-root"></div>
<script>(function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s);
js.id = id;
js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
</body>
</html>