How to dynamically set the Layout parameters in a template

From Wiki

Jump to: navigation, search

The layout parameters allows you to customize the rendering of an element without the need to write many different templates. There are some cases in which using the layout parameters set on the element is not enough. Suppose for example to have a power index unit. You want to render all the links of that unit as text, except for the scroll links. If you set the layout parameters for the Link Layout only, these configuration is used for the entire unit and for all links.

To get the desired result you have to write a custom link template. This template must have the same layout parameters as the one you want to apply for the links. You have to write a script Groovy that changes the value of some parameters for some specific links (in this case the scroll links) and then you have to invoke the "main" link template. At the project level you set the custom link template as the default for the power index unit. It's this template that invokes the one that really print links. Let's see how to do this step by step:

  1. create a custom Link template
  2. choose the Project tab in the workarea
  3. click the button next to the Units Configuration field
  4. choose the Units tab in the opening editor
  5. select the custom Link template as the default one for the power index unit
  6. save the changes

When you write the content of the custom Link template you have to:

  1. declare of all the layout parameters of the template you want to call (line 1-10)
  2. retrieve the link layout set for the current link (line 12)
  3. set the templateName to the one you want to invoke (line13)
  4. retrieve the file of the template to invoke (line 14)
  5. override the value of the parameters as you want to get the desired result (line 16-21)
  6. invoke the template passing the new parameters map (line 22)
1:  #?delimiters [%, %], [%=, %]
2:  <wr:LayoutParameter label="Link Style" name="link-style" type="enum" values="normal|button" default="normal"/>
3:  <wr:LayoutParameter label="Show" name="show" type="enum" values="text|icon|icon & text" default="icon"/>
4:  <wr:LayoutParameter label="Icon Folder" name="icon-folder" type="string" default="eKRPResources/images"/>
5:  <wr:LayoutParameter label="Icon Extension" name="icon-extension" type="string" default="png"/>
6:  <wr:LayoutParameter label="Use Alt" name="use-alt" type="boolean" default="false"/>
7:  <wr:LayoutParameter label="New Window Type" name="new-window-type" type="enum" values="normal|javascript" default="normal"/>
8:  <wr:LayoutParameter label="New Window Options" name="new-window-options" type="string" default="width=640,height=480,status=yes"/>
9:  <wr:LayoutParameter label="Use Confirm Dialog" name="confirmation" type="boolean" default="false"/>
10: <wr:LayoutParameter label="Confirm Message Key" name="confirm-message" type="string" default="linkConfirmMessage"/>
11: [% 
12:	def linkLayout = getLinkLayout(link)
13:	def templateName = "WRDefault/Default"
14:	def templateFile = getElementLayoutFile(templateName, ".link.template")
15:	
16:	if (link.selectSingleNode("LinkParameter[@source='sortCriteria']") != null) { 	
17:		linkLayout.parameters["show"] = "icon & text"
18: 	}
19: 	if (link["name"] == (unit["id"] + "block")) { 	
20:		linkLayout.parameters["show"] = "text"
21: 	}
22: 	printRaw(executeTemplate(templateFile.absolutePath, ["params" : linkLayout.parameters, "templateType" : "link"]))
23: %]

When you place the power index unit in the grid you have only to set the layout parameters for the link template, remembering which ones will be override by the link template before calling the "main" one.

Personal tools