Let's continue with the first component we created here. This component has two text fields
Title and Description. By default these values are not set.
To set the values on include of a component follow the given steps:
1.Create a node of type"nt:unstructured" named "cq:template" inside your component.
2. We will add the default value with same name key on "cq:template" node.
Note: The values will be pre-filled only on include of the component.
In scenarios, where the value gets stored in sub node(For example multifield stores the values in tree structure when composite is set to TRUE), create same structure under the "cq:template" node.
Lets take another example to understand this scenario:
Component created in this post stores data in following structure.
So, the "cq:template: node for this component will look like this:
Wouldn't it be repetitive to add this node in each component? "cq:templatePath" can be used refer the properties saved at different location.
Follow the below steps for the same:
Create a node of type "sling:Folder" anywhere in the apps folder.
Under this folder create same structure we created before. You can name the node anything instead of "cq:template"
3. Copy the absolute location of the node.
4. Create property named "cq:templatePath" on cq:Component node of the component. and paste the copied path here.
Thats it!, you can now reuse this path in multiple components.
Note: If you use cq:templatePath in your component, then "cq:template" node created under your component will be disregarded.
Difference between setting default value for each field and cq:template:
So now a question might arise that why do we use cq:template when we have values field for each field in the dialog.
Answer to this is content authored in "value" property will be displayed only when you open the component for the first time after dragging it to the page, and the value will be saved to the content node only on click of save.
Whereas in case of "cq:template" the values are stored to the content node as soon as you add the component to the page.