<apex:composition> を使用したテンプレートの定義
<apex:composition> を使用して定義したすべてのテンプレートには、1 つ以上の子の <apex:insert> タグが必要です。<apex:insert> タグは、そのテンプレートをインポートするページに対し、そのセクションで定義が必要であることを示します。<apex:composition> を使用してテンプレートをインポートする Visualforce ページでは、<apex:define> を使用してテンプレートの各 <apex:insert> セクションのコンテンツを指定する必要があります。
スケルトンテンプレートを作成すると、それ以降の Visualforce ページが同じ標準構造内で異なるコンテンツを実装できます。そのためには、<apex:composition> タグを使用してテンプレートページを作成します。
次の例は、<apex:composition>、<apex:insert>、および <apex:define> を使用してスケルトンテンプレートを実装する方法を示します。
最初に、compositionExample というコントローラを使用する myFormComposition という空のページを作成します。
1<apex:page controller="compositionExample">
2
3</apex:page>ページを保存すると、compositionExample の作成を要求するメッセージが表示されます。次のコードを使用して、そのカスタムコントローラを定義します。
1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class compositionExample{
18
19 String name;
20 Integer age;
21 String meal;
22 String color;
23
24 Boolean showGreeting = false;
25
26 public PageReference save() {
27 showGreeting = true;
28 return null;
29 }
30
31 public void setNameField(String nameField) {
32 name = nameField;
33 }
34
35 public String getNameField() {
36 return name;
37 }
38
39 public void setAgeField(Integer ageField) {
40 age= ageField;
41 }
42
43 public Integer getAgeField() {
44 return age;
45 }
46
47 public void setMealField(String mealField) {
48 meal= mealField;
49 }
50
51 public String getMealField() {
52 return meal;
53 }
54
55 public void setColorField(String colorField) {
56 color = colorField;
57 }
58
59 public String getColorField() {
60 return color;
61 }
62
63 public Boolean getShowGreeting() {
64 return showGreeting;
65 }
66}次に、myFormComposition に戻り、スケルトンテンプレートを作成します。
2 つの <apex:insert> 項目が age と meal のコンテンツを必要としています。これらの項目のマークアップは、この構成テンプレートをコールするすべてのページに定義されます。
1swfobject.registerObject("clippy.codeblock-2", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<apex:page controller="compositionExample">
18 <apex:form >
19 <apex:outputLabel value="Enter your name: " for="nameField"/>
20 <apex:inputText id="nameField" value="{!nameField}"/>
21 <br />
22 <apex:insert name="age" />
23 <br />
24 <apex:insert name="meal" />
25 <br />
26 <p>That's everything, right?</p>
27 <apex:commandButton action="{!save}" value="Save" id="saveButton"/>
28 </apex:form>
29</apex:page>次に、myFullForm というページを作成し、そこで myFormComposition に <apex:insert> タグを定義します。
1swfobject.registerObject("clippy.codeblock-3", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<apex:page controller="compositionExample">
18 <apex:messages/>
19 <apex:composition template="myFormComposition">
20
21 <apex:define name="meal">
22 <apex:outputLabel value="Enter your favorite meal: " for="mealField"/>
23 <apex:inputText id="mealField" value="{!mealField}"/>
24 </apex:define>
25
26 <apex:define name="age">
27 <apex:outputLabel value="Enter your age: " for="ageField"/>
28 <apex:inputText id="ageField" value="{!ageField}"/>
29 </apex:define>
30
31 <apex:outputLabel value="Enter your favorite color: " for="colorField"/>
32 <apex:inputText id="colorField" value="{!colorField}"/>
33
34 </apex:composition>
35
36 <apex:outputText id="greeting" rendered="{!showGreeting}" value="Hello {!nameField}.
37 You look {!ageField} years old. Would you like some {!colorField} {!mealField}?"/>
38</apex:page>このマークアップでは、次の点に留意してください。
- myFullForm を保存すると、前に定義した <apex:inputText> タグと [保存] ボタンが表示されます。
- 構成ページには age 項目および meal 項目が必要であるため、myFullForm はこれらをテキスト入力項目として定義します。項目がページに表示されている��序は重要ではありません。myFormComposition は age 項目が常に meal 項目よりも前に表示されるように指定します。
- 一致する <apex:define> 項目がない場合でも、name 項目はインポートされます。
- 項目のコントローラコードが存在する場合でも、color 項目は無視されます。これは、構成テンプレートが color という名前の項目を必要としないためです。
- age および meal 項目は、テキスト入力である必要はありません。<apex:define> タグ内のコンポーネントは、任意の有効な Visualforce タグにすることができます。
<apex:define> タグで有効な Visualforce を使用する方法の例として、myAgelessForm という新規 Visualforce ページを作成して次のマークアップを使用します。
構成テンプレートで必要なのは、<apex:define> タグが存在することだけです。この例では、age がテキストして定義されています。
1swfobject.registerObject("clippy.codeblock-4", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<apex:page controller="compositionExample">
18 <apex:messages/>
19 <apex:composition template="myFormComposition">
20
21 <apex:define name="meal">
22 <apex:outputLabel value="Enter your favorite meal: " for="mealField"/>
23 <apex:inputText id="mealField" value="{!mealField}"/>
24 </apex:define>
25
26 <apex:define name="age">
27 <p>You look great for your age!</p>
28 </apex:define>
29
30 </apex:composition>
31
32 <apex:outputText id="greeting" rendered="{!showGreeting}" value="Hello {!nameField}.
33 Would you like some delicious {!mealField}?"/>
34</apex:page>動的テンプレート
PageReference を介してテンプレートを割り当てるには、動的テンプレートを使用します。テンプレート名は、使用するテンプレートが含まれる PageReference を返すコントローラメソッドに割り当てられます。
たとえば、スケルトンテンプレートを定義する myAppliedTemplate というページを作成します。
1<apex:page>
2 <apex:insert name="name" />
3</apex:page>次に、このページへの参照を返すメソッドを使用する dynamicComposition というコントローラを作成します。
1public class dynamicComposition {
2 public PageReference getmyTemplate() {
3 return Page.myAppliedTemplate;
4 }
5}最後に、このコントローラと動的テンプレートを実装する myDynamicComposition というページを作成します。
1<apex:page controller="dynamicComposition">
2 <apex:composition template="{!myTemplate}">
3 <apex:define name="name">
4 Hello {!$User.FirstName}, you look quite well.
5 </apex:define>
6 </apex:composition>
7</apex:page>