Home > Web Front-end > HTML Tutorial > Simple extension allows beetl HTML tags to support parent-child nesting_html/css_WEB-ITnose

Simple extension allows beetl HTML tags to support parent-child nesting_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:49:31
Original
1419 people have browsed it

By default, Beetl's html tags do not support parent-child nesting. Just like jsp tags, the parent tag needs to know the information of the child tag, and the child tag also needs to know the information of the parent tag. But beetl only needs a simple extension to support nested tags.

First, let’s take a look at the final usage effect. Implement two html tags table.tag and tr.tag. They can be used like this on the page:

<#table data ="${userList}"><#tr class="3c" name="name"> 名称 </#tr></#table>
Copy after login



Before reading table.tag, tr.tag, first look at how to extend html tags
First, you need to extend HTMLTagSupportWrapper, this class is the html tag implementation Class, we can extend this class to customize the needs, and then re-register the override. Therefore, implement the class

public class HTMLNestTagSupportWrapper extends HTMLTagSupportWrapper{public void render(){....}}
Copy after login



and then configure TAG.htmltag= bingo.util.HTMLNestTagSupportWrapper in the configuration file to take effect.

HTMLNestTagSupportWrapper uses the TagNestContext class. This class is actually a tree structure that records the context of the parent, the current tag information, and the context of the child tag. In this way, each tag can access the parent tag or subtag: The code is as follows

public class TagNestContext {private  Tag tag = null;private TagNestContext parent = null;private List<TagNestContext> children = null;public  Tag getTag() {    return tag;}public void setTag(Tag para) {    this.tag = para;}public TagNestContext getParent() {    return parent;}public void setParent(TagNestContext parent) {    this.parent = parent;}public List<TagNestContext> getChildren() {    if(children==null) children = new ArrayList<TagNestContext>();    return children;}public void setChildren(List<TagNestContext> children) {    this.children = children;}}
Copy after login



Let’s look back at the HTMLNestTagSupportWrapper implementation

public void render(){    HttpServletRequest request = (HttpServletRequest)this.ctx.getGlobal("request");    TagNestContext tnc = (TagNestContext)request.getAttribute("tagContext");    if(tnc==null){        tnc = new TagNestContext();        tnc.setTag(this);        request.setAttribute("tagContext", tnc);        super.render();        request.removeAttribute("tagContext");    }else{        TagNestContext child = new TagNestContext();        child.setParent(tnc);        child.setTag(this);        tnc.getChildren().add(child);        request.setAttribute("tagContext", child);        super.render();        //重新设置        request.setAttribute("tagContext", child.getParent());    }}public String getTagName(){    return (String)this.args[0];}public Object get(String attr){    Map map =  (Map)this.args[1];    return map.get(attr);}
Copy after login



As shown in the above code, but before rendering a certain htmltag (before calling super.render()), you can get the NestContext from the request. If not, generate one new NestContext. if already exists. Then add the current NestContext to the parent NestContext. After rendering, the NestContext needs to be reset.

Finally, let’s take a look at how tr.tag is implemented. tr only implements generating headers

<tr class="${class}">${tagBody}</tr>
Copy after login



table.tag is a bit more troublesome. You need to know how many tr there are, and then output the data. The content is as follows:

<table>${tagBody}<% for(var item in data){%><tr><%var tag = getTagContext();var children = tag.children;for(var tdTagCtx in children){    print("<td>");    var tdTag = tdTagCtx.tag;    var name = tdTag.tagName;    var value = item[name];    print(value);    println("</td>");}%></tr><%} %></table>
Copy after login



etTagContext is a registration method to obtain the current context. You can develop one yourself
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template