我是百老汇的新手,我正在尝试创建一个网络组件。我想要的是这样的东西:
<components:panel><div>hi!</div></components:panel>
翻译成这样的东西:
<div class="panel"><div class="contents"><div>hi!</div></div></div>
我一直在尝试扩展AbstractElementTagProcessor,但我似乎无法弄清楚如何在已处理的代码中获取代码:
public class PanelTagProcessor extends AbstractElementTagProcessor {
private static final String ELEMENT_NAME = "panel";
private static final int PRECEDENCE = 10000;
public PanelTagProcessor(final String dialectPrefix) {
super(TemplateMode.HTML, "components", ELEMENT_NAME, true, null, false, PRECEDENCE);
}
@Override
protected void doProcess(ITemplateContext context, IProcessableElementTag tag,
IElementTagStructureHandler structureHandler) {
// How could I surround contents inside the tag with custom nodes?
}
}
如果有人可以帮助我,我将不胜感激:)
此致 路易斯
答案 0 :(得分:2)
首先,我认为只使用AbstractElementTagProcessor可以做到这一点。根据{{3}},这些处理器&#34;仅在开放/独立标签事件上执行(没有处理器可以应用于关闭标签),并且没有(直接)访问元素主体。&#34 ;
我设法让它工作了AbstractElementModelProcessor
(&#34;以IModel对象和#34的形式执行完整元素,包括它们的主体;),所以这对我有用。< / p>
public class PanelTagProcessor extends AbstractElementModelProcessor {
private static final String TAG_NAME = "panel";
private static final int PRECEDENCE = 10000;
public PanelTagProcessor(String dialectPrefix) {
super(TemplateMode.HTML, dialectPrefix, TAG_NAME, true, null, false, PRECEDENCE);
}
@Override
protected void doProcess(ITemplateContext itc, IModel imodel, IElementModelStructureHandler iemsh) {
IModelFactory modelFactory = itc.getModelFactory();
imodel.replace(0, modelFactory.createOpenElementTag("div", "class", "panel"));
imodel.insert(1, modelFactory.createOpenElementTag("div", "class", "contents"));
imodel.insert(imodel.size() - 2, modelFactory.createCloseElementTag("div"));
imodel.replace(imodel.size() - 1, modelFactory.createCloseElementTag("div"));
}
}