coldfusion nested custom tag 使用方法

 

這個鏈接可以很明確的說明nested custom tag如何使用,舉出了例子,相當好!

http://www.bennadel.com/blog/293-ColdFusion-Basics-Nesting-Custom-Tags.htm

 

下面把這個例子的源代碼區分的發出來,可以學習一下,看了你就很明白的。

按照順序

需要先來看item.cfm文件

 

<!--- Kill extra output. --->
<cfsilent>
 
 <!---
  We only want to associate tag info with the parent in the
  first run of the sub tag. Otherwise, we might end up
  storing the info twice. Also, no need to validate
  attributes twice, the first time is sufficient.
 --->
 <cfif (THISTAG.ExecutionMode EQ "Start")>
 
  <!---
   Associate this tag with the parent tag. We are
   going to override the default value for data
   collection. It is usually AssocAttribs, which is a
   horrible name. We are going to store it in the
   structure "Items". This will allow the attribute data
   of the tag to be stored in parent tag.
 
   In this case, "list.cfm" is our parent tag. We define
   this association via the old school name "cf_list".
  --->
  <cfassociate
   basetag="cf_list"
   datacollection="Items"
   />
 
  <!--- Param tag attributes. --->
  <cfparam
   name="ATTRIBUTES.value"
   type="string"
   default=""
   />
 
  <!---
   Param the trim value flag. We are defaulting this
   to true. In that case, we will trim the value of
   the GENERATED CONTENT (not the Value attribute).
  --->
  <cfparam
   name="ATTRIBUTES.trimvalue"
   type="boolean"
   default="true"
   />
 
 </cfif>
 
 <!---
  Check to see if this tag as a closing tag. If it does,
  then we might be sending the value of the generated
  content instead of the value attribute.
 --->
 <cfif THISTAG.HasEndTag>
 
  <!---
   This tag might just be self closing (which would
   be considered a closing end tag. In that case, we
   won't have any length in our generated content
   (the content between the opening and closing tags.
   Check the length of the generated contet.
  --->
  <cfif Len( THISTAG.GeneratedContent )>
 
   <!---
    Since we have generated content, we are going
    to be using that as our list item value.
    Check to see if we need to trim this.
   --->
   <cfif ATTRIBUTES.trimvalue>
 
    <!---
     Trim value and save it into the attributes.
     We don't need to store into attributes, but
     we already have the value, so why not.
    --->
    <cfset ATTRIBUTES.value = THISTAG.GeneratedContent.Trim() />
 
    <!--- Erase the generated content. --->
    <cfset THISTAG.GeneratedContent = "" />
 
   </cfif>
 
  </cfif>
 
 </cfif>
 
</cfsilent>

 

 

 

 

 

< -------------------------------------------------------------------------------然後來看 list.cfm----------------->

 

 

 

 

 

 

 

 

<cfsilent>
 
 <!---
  Check to see if we are in the start mode of the tag. There
  is no need to param any attributes after the start mode.
 --->
 <cfif (THISTAG.ExecutionMode EQ "Start")>
 
  <!---
   Param the align attribute. This till determine if we
   show the list one after another in-line, or if the
   list should be displayed as block elements.
 
   Possible values:
   - horizontal (default)
   - vertical
  --->
  <cfparam
   name="ATTRIBUTES.align"
   type="string"
   default="horizontal"
   />
 
 </cfif>
 
</cfsilent>
 
<!---
 Since this is a parent tag that is designed to have child
 tags, we can't really do anything until the child tags have
 been defined. Therefore, we can only really work in the
 End mode of execution.
--->
<cfif (THISTAG.ExecutionMode EQ "End")>
 
 <!---
  ASSERT:
  At this point, we should have all the child tags
  associated with this parent tag. As per the child tags,
  all the attribute data should be in a structure:
  THISTAG.Items.
 --->
 
 <!---
  Now, we have to check to see how to display the items.
  Vertically or horizontally?
 --->
 <cfif (ATTRIBUTES.align EQ "vertical")>
 
  <!--- Display veritcally. --->
 
  <!---
   Loop over the Items array. Remember, this array
   contains the attributes of the child tags. Remember
   to use CFOutput tags as custom tags are NOT natural
   CFOutput blocks.
  --->
  <cfoutput>
 
   <cfloop
    index="intI"
    from="1"
    to="#ArrayLen( THISTAG.Items )#"
    step="1">
 
    <!--- Output value. --->
    <p>
     Item #intI#: #THISTAG.Items[ intI ].value#
    </p>
 
   </cfloop>
 
  </cfoutput>
 
 <cfelse>
 
  <!---
   We are going with the default, which is vertical.
   You might think the first CFIF clause should be the
   default statement since the default is probably the
   most often used. By making the default the ELSE
   clause, we don't really have to validate the types
   passed in. But that's not really here nor there.
  --->
 
  <!---
   Loop over the Items array. Remember, this array
   contains the attributes of the child tags. Remember
   to use CFOutput tags as custom tags are NOT natural
   CFOutput blocks.
  --->
  <cfoutput>
 
   <cfloop
    index="intI"
    from="1"
    to="#ArrayLen( THISTAG.Items )#"
    step="1">
 
    <!--- Output value. --->
    Item #intI#: #THISTAG.Items[ intI ].value#
 
   </cfloop>
 
  </cfoutput>
 
 </cfif>
 
</cfif>

 

 

 

<!----最後是 testList.cfm--------------------->

 

 

<!--- Our parent list tag. --->
<cf_list align="horizontal">
 
 <!--- A sub tag: item.cfm. --->
 <cf_item>
  Ashley Thomas
 </cf_item>
 
 <!--- A sub tag: item.cfm. --->
 <cf_item>
  Sarah Vivenzio
 </cf_item>
 
</cf_list>

 

 

運行結果:

Item 1: Ashley Thomas Item 2: Sarah Vivenzio

 

從這裏我們就可以看出來nested custom tag, 它可以使得parent tag 調用到child tag 的屬性。

還有其他的特性,但是在這個例子裏還沒完全展示,通過多的實踐就會體會到的它的power

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章