Friday, 8 June 2012

Coding different page layouts in a single rtf template and the importance of Contexts in BI Publisher

The requirement was to generate an output in statement page size or in legal page size, depending on a parameter value. Since the parameter was passed from Siebel and since I did not know the method to conditionally call different rtf files from Siebel so I had to code everything in 1 single rtf file

I spent almost a day to code this using the sub templates in the same rtf file. I also tried using import statements and even those didn’t help. Finally one of my colleagues tried using an ‘if’ statement with section context and it worked. Below are the code and the reason why it worked

Let me repeat the requirement: We have to conditionally display some content in different page sizes. In this example we will display the content in Legal (8.5 X 14’’) and Statement (5.5 X 8.5’’) page sizes based on the values in ‘page_display’ variable

The Solution:

  1. Open MSWord and save the blank file as a .rtf file.Set it's page size to Legal
  1. Declare the 'page_display' variable. We will code this template such that it will display the output in a legal page size if the value of page_display variable is ‘L’ and will display the output in Statement page if the value of page_display variable is ‘S’. We are initializing this variable to ‘L’.
<?param@begin:page_display;'"L"'?>
  1. Write the following statements on this page
<?if@section:$page_display =’L’?>
Hello everyone. Welcome to the Legal page
<?end if?>
Note the ‘if’ statement. It has an explicit declaration of the context. The context here is ‘section’.  The definition of each context is given in the following BI Publisher documentation


Let me paste the definition of the ‘section’ context
The statement affects the whole section including the header and footer. For example, a for-each@section context command creates a new section for each occurrence - with restarted page numbering and header and footer.
Note that you can retain continuous page numbering across sections by using the <?initial-page-number:'auto'?> command.

The fact that the section context affects header and footer is the key in this solution. This basically means that if the ‘if’ condition is not satisfied then the header and footer will also disappear. Hence we can remove the entire page from the output if we are using the ‘if’ statement with the context 'section'.

The default context of the ‘if’ statement is ‘block’ and the following is the definition of the ‘block’ context
The statement will affect multiple complete fo:blocks (RTF paragraphs). This context is typically used for if and for-each statements. It can also be used to apply formatting to a paragraph or a table cell.

So if we use a normal if statement then its default context will be block. Hence everything inside the block will disappear if the condition is not satisfied but this context does not affect the header and footer. So if everything inside the page is encapsulated inside the ‘if’ statement, which does not have a section context, then a blank page will be displayed because the engine has to display the header and the footer.
  1. Go to ‘Page Layout’ tab in MS word and click on the arrow on the ‘Size’ icon. Then click on ‘More paper sizes’
  1. Change the paper size to Statement and select ‘This point forward’ in the ‘Apply to’ dropdown
  1. Click on the paragraph marks button in the Home tab. It displays the formatting in the document.
  1. You should be able to see a section break mark on this page
So we see that we have an if which applies to a section and this section ends right after the <?end if?> statement.
  1. Put the following piece of code in the next page which has a statement page size
<?if@section:$page_display =’S’?>
Hello everyone. Welcome to the Statement page
<?end if?>
  1. Your final code will look like the following
  1. Now load any XML data source. You should be able to see the output in Legal format when you initialize the value of page_display variable to ‘L’ and you should be able to see the output in statement format when you set this variable to ‘S’

Till next time

No comments: