这个问题的完整重现案例在我的 GitHub repository 中.我只会在这里重现必要的部分。
假设我使用了一些自定义标签:
<!--- testCfcTags.cfm --->
<cfimport taglib="cfcBasedTags" prefix="t">
Text before tags<br>
<t:grandparent gp:attr="set in grandparent">
Text in grandparent, before parent<br>
<t:parent p:attr="set in parent">
Text in parent, before child<br>
<t:child c:attr="set in child">
Text in child<br>
</t:child>
Text in parent, after child<br>
</t:parent>
Text in grandparent, after parent<br>
</t:grandparent>
Text after tags<br>
如果我使用的是基于 CFM 的自定义标签,并且我想将 child
标签实现中的数据与 grandparent
标签相关联,我会简单地说:
<!--- child.cfm --->
<cfif thistag.executionMode eq "end">
<cfassociate basetag="cf_grandparent" datacollection="childAttributesForGrandparent"><!--- this line --->
<cfassociate basetag="cf_parent" datacollection="childAttributesForParent">
</cfif>
注意我可以直接关联到祖 parent 标签。
我不知道如何使用 Lucee 基于 CFC 的自定义标签干净地做到这一点。
这是我能想到的最好的:
// Child.cfc
component {
function init(hasEndTag, parent){
this.parent = arguments.parent;
}
function onEndTag(attributes, caller, generatedContent){
writeOutput(generatedContent);
this.parent.childattributesForParent = attributes;
this.parent.parent.childattributesForGrandparent = attributes;
return false;
}
}
在 Parent.cfc 中我有这个:
// Parent.cfc
component {
function init(hasEndTag, parent){
this.parent = arguments.parent;
}
function onEndTag(attributes, caller, generatedContent){
writeOutput(generatedContent);
this.parent.parentattributesForGrandparent = attributes;
writeDump(var=this.childAttributesForParent, label="Parent childAttributesForParent");
return false;
}
}
累积(错误)使用父级和祖父级的 this
范围意味着我可以从子级通过 this.parent.parent
将内容直接塞入祖父级。
然而,这有点“Heath Robinson”。鉴于 Lucee 的其他基于 CFC 的自定义标记实现非常巧妙,我确信我只是遗漏了一些东西。我真的不认为我应该通过 Parent 挖洞才能到达 Grandparent。这也意味着对于 Child 直接在 Grandparent 中的情况,代码需要有所不同。我真正需要的是在 CFC 之间传递一些标记层次结构,而不仅仅是父级。
我用谷歌搜索了一下,但大部分内容都是我写的(这又是基于最初为 Railo 的实现而写的博客文章——这是 Lucee 实现的基础)。
我已经阅读过的文档没有帮助:
最佳答案
根据 Railo 博客:
http://blog.getrailo.com/post.cfm/cfc-based-custom-tags-by-example-part-1
You can use the tag cfassociate and the function GetBaseTagList and >GetBaseTagData the same way as for regular CFML based custom tags.
所以你可以这样做(在 cfscript 中):
cfassociate(basetag="cf_grandparent", datacollection="childAttributesForGrandparent");
我已经将要点和一些示例放在一起 - 我已经测试并验证了它在 Lucee 4.5.1 上的工作: https://gist.github.com/dajester2013/183e862915972d51279f
编辑:选项 2,基本标记方法:
根据我的评论,这是一种通过基本标记的潜在方法 - 它至少掩盖了不太漂亮的方面:
基础标签.cfc
component accessors=true {
property name="tagName";
property name="parent";
property name="hasEndTag";
public BaseTag function init() {
structAppend(variables, arguments);
tagName = "cf_" & lcase(listLast(getMetaData(this).fullname,"."));
return this;
}
public any function getParent(string tagName, ancestors=1) {
if (!isNull(tagName)) {
var data = getBaseTagData(tagName, ancestors);
if (structKeyExists(data,"thisTag")) {
return data.thisTag;
// getBaseTagData returns the variables scope for CFC tags...
} else if (structKeyExists(data, "this")) {
return data.this;
}
} else if (!isNull(variables.parent)) {
return variables.parent;
}
}
private void function associate(required string tagName, string dataCollection=this.getTagName()) {
cfassociate(basetag=tagname, dataCollection=dataCollection);
}
}
测试子.cfc
component extends=BaseTag {
public function onStartTag() {
attributes._childId = randrange(1000,9000);
associate("cf_testtag", "testchildren");
writedump(var=this.getParent(),label='immediateparent');
writedump(var=this.getParent("cf_testtag"), label='testtag');
abort;
}
}
关于cfml - 使用基于 CFC 的自定义标签将子标签数据与祖 parent 标签相关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28578628/