这个问题的全部复制案例在我的GitHub存储库中。我只会在这里复制必要的部分。
假设我使用了一些自定义标记:
<!--- 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>注意,我可以直接与祖父母标记关联。
我想不出如何使用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直接将东西注入祖父母。
然而,这一切都有点"希思·罗宾逊“。考虑到Lucee的其他基于CFC的定制标记实现非常灵活,我相信我只是遗漏了一些东西。我真的不认为我应该去挖洞的父母,以达到祖父母。此外,这也意味着,在孩子直接在祖父母的范围内的情况下,代码需要不同。我真正需要的是在氟氯化碳之间传递某种标签层次结构,而不仅仅是父母。
我已经在谷歌上搜索过了,但是大部分的内容都是我写的(这也是基于最初为Railo的实现而写的博客文章--这就是Lucee实现所基于的)。
我已经读过的文档,这些都没有帮助:
发布于 2015-03-01 02:09:05
据铁道博客称:
http://blog.getrailo.com/post.cfm/cfc-based-custom-tags-by-example-part-1
您可以使用标记cfassociate和函数GetBaseTagList和>GetBaseTagData的方式与基于CFML的常规定制标记使用相同的方式。
所以您可以这样做(在cfscript中):
cfassociate(basetag="cf_grandparent", datacollection="childAttributesForGrandparent"); 我收集了一个gist和一些示例-我已经测试并验证了它在Lucee4.5.1:https://gist.github.com/dajester2013/183e862915972d51279f上的工作原理
编辑:选项2,基本标记方法:
根据我的评论,这里有一种通过基本标记的潜在方法--它至少掩盖了不那么漂亮的方面:
BaseTag.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);
}
}TestChild.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;
}
}https://stackoverflow.com/questions/28578628
复制相似问题