【cfinclude】CFML内にColdFusionページへのリファレンスを埋め込む
cfincludeタグを使って、以下のプログラムを作成しました。
【test1.cfm】 実行
【test2.cfm】
これはtest3.cfmと同じです。
【test3.cfm】 実行
実はcffunctionで作成した関数を使うこともできます。
【test4.cfm】 実行
でも、この使い方はよくない例です。通常は関数内ではローカル変数宣言をしておきます。varをつけます。
【test5.cfm】 実行
これだとローカル変数になったので、最初のa,bの値は違ってきました。
数値を計算して返すなら、こちらの書き方がよいです。
【test6.cfm】 実行
cfincludeのドキュメントはこちらです(cf7)。
※2016年1月19日、urlをutalab.bizに修正
【test1.cfm】 実行
01: <cfset a = 1>
02: <cfset b = 2>
03:
04: <cfinclude template="test2.cfm">
05:
06: <cfset b = a + b>
07:
08: <cfinclude template="test2.cfm">
09:
10: <cfoutput>
11: a = #a#<br>
12: b = #b#<br>
13: </cfoutput>
14:
15: <cfinclude template="footer.cfm">
【test2.cfm】
01: <cfset a = a + 1>
これはtest3.cfmと同じです。
【test3.cfm】 実行
01: <cfset a = 1>
02: <cfset b = 2>
03:
04: <cfset a = a + 1>
05:
06: <cfset b = a + b>
07:
08: <cfset a = a + 1>
09:
10: <cfoutput>
11: a = #a#<br>
12: b = #b#<br>
13: </cfoutput>
14:
15: <cfinclude template="footer.cfm">
実はcffunctionで作成した関数を使うこともできます。
【test4.cfm】 実行
01: <cfset a = 1>
02: <cfset b = 2>
03:
04: <cfset test()>
05:
06: <cfset b = a + b>
07:
08: <cfset test()>
09:
10: <cfoutput>
11: a = #a#<br>
12: b = #b#<br>
13: </cfoutput>
14:
15: <cffunction name="test" returntype="void">
16: <cfset a = a + 1>
17: </cffunction>
18:
19: <cfinclude template="footer.cfm">
でも、この使い方はよくない例です。通常は関数内ではローカル変数宣言をしておきます。varをつけます。
【test5.cfm】 実行
01: <cfset a = 1>
02: <cfset b = 2>
03:
04: <cfset test()>
05:
06: <cfset b = a + b>
07:
08: <cfset test()>
09:
10: <cfoutput>
11: a = #a#<br>
12: b = #b#<br>
13: </cfoutput>
14:
15: <cffunction name="test" returntype="void">
16: <cfset var a = a + 1>
17: </cffunction>
18:
19: <cfinclude template="footer.cfm">
これだとローカル変数になったので、最初のa,bの値は違ってきました。
数値を計算して返すなら、こちらの書き方がよいです。
【test6.cfm】 実行
01: <cfset a = 1>
02: <cfset b = 2>
03:
04: <cfset a = test(a)>
05:
06: <cfset b = a + b>
07:
08: <cfset a = test(a)>
09:
10: <cfoutput>
11: a = #a#<br>
12: b = #b#<br>
13: </cfoutput>
14:
15: <cffunction name="test" returntype="numeric">
16: <cfargument name="data" type="numeric">
17: <cfreturn data + 1>
18: </cffunction>
19:
20: <cfinclude template="footer.cfm">
cfincludeのドキュメントはこちらです(cf7)。
※2016年1月19日、urlをutalab.bizに修正