Skip to content

Commit

Permalink
Cut and Copy tags. Fixes #18
Browse files Browse the repository at this point in the history
  • Loading branch information
codeconsole committed Nov 12, 2023
1 parent 4880bd2 commit 049c044
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ <h1 class="title"><a href="/" style="float:left;"><img height="40" src="/assets/

<sitemesh:write property="body">Body goes here. Blah blah blah.</sitemesh:write>

<div class="disclaimer">Site disclaimer. This is an example.</div>
<div class="disclaimer">Site disclaimer. Hey <sitemesh:write property="sitemesh.name">you</sitemesh:write>, this is just an example.</div>
<div class="navigation">
<b>Examples:</b>
[<a href="/">Static example</a>]
Expand All @@ -25,5 +25,6 @@ <h1 class="title"><a href="/" style="float:left;"><img height="40" src="/assets/
[<a href="/greetingError">500 example</a>]
</div>

<sitemesh:write property="sitemesh.jsSpecialGreeter"/>
</body>
</html>
15 changes: 14 additions & 1 deletion examples/springboot/src/main/webapp/WEB-INF/jsp/greeting.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</style>
</head>
<body>
<p>Hello, ${name}!</p>
<p>Hello, <sitemesh:copy id="name">${name}</sitemesh:copy>!</p>

<p>This page demonstrates that dynamic content can be decorated in the same way as static content.</p>
<p>This is a simple JSP that shows the date and time on the server is now:</p>
Expand All @@ -17,5 +17,18 @@

<p>Of course, with SiteMesh you are not limited to JSP. Because it's a Servlet Filter, both content and decorators can be
generated by any technology in your ServletEngine, including: <a href="/">static</a> files, <a href="/greeting">JSP</a>, Velocity, <a href="/greeting/ftl">FreeMarker</a>, <a href="/greeting/ttl">Thymeleaf</a>, JSF, MVC frameworks, JRuby.... you get the point.</p>

<form>
What is your name? <input type="text" name="name" />
<input type="submit" value="Go" />
</form>

<sitemesh:cut id="jsSpecialGreeter">
<script type="text/javascript">
if ('${name}'.toLowerCase() == "scott") {
alert("Now that's a cool name!!")
}
</script>
</sitemesh:cut>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public void install(State defaultState, ContentProperty contentProperty, SiteMes
defaultState.addRule("title", new ExportTagToContentRule(siteMeshContext, contentProperty.getChild("title"), false));
defaultState.addRule("body", new ExportTagToContentAndMergeBodyAttributesRule(siteMeshContext, contentProperty.getChild("body"), false));
defaultState.addRule("meta", new MetaTagRule(contentProperty.getChild("meta")));
defaultState.addRule("sitemesh:copy", new SiteMeshCutCopyRule(true, contentProperty));
defaultState.addRule("sitemesh:cut", new SiteMeshCutCopyRule(false, contentProperty));

// Ensure that while in <xml> tag, none of the other rules kick in.
// For example <xml><book><title>hello</title></book></xml> should not affect the title of the page.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2009-2023 SiteMesh authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.sitemesh.content.tagrules.html;

import org.sitemesh.content.ContentProperty;
import org.sitemesh.tagprocessor.BasicBlockRule;
import org.sitemesh.tagprocessor.Tag;

import java.io.IOException;

public class SiteMeshCutCopyRule extends BasicBlockRule<String> {

private final ContentProperty contentProperty;
private final boolean copy;

public SiteMeshCutCopyRule(boolean copy, ContentProperty contentProperty) {
this.copy = copy;
this.contentProperty = contentProperty.getChild("sitemesh");
}

@Override
protected String processStart(Tag tag) throws IOException {
tagProcessorContext.pushBuffer();
return tag.getAttributeValue("id", false);
}

@Override
protected void processEnd(Tag tag, String tagId) throws IOException {
CharSequence contents = tagProcessorContext.currentBufferContents();
contentProperty.getChild(tagId).setValue(contents);
tagProcessorContext.popBuffer();
if (copy) {
tagProcessorContext.currentBuffer().append(contents);
}
}

}

0 comments on commit 049c044

Please sign in to comment.