Search This Blog

Showing posts with label Using APEX Template Application with Parameters. Show all posts
Showing posts with label Using APEX Template Application with Parameters. Show all posts

Sunday, July 13, 2025

Using APEX Template Application with Parameters, Directives, and Functions

Introduction
Oracle APEX Template Components allow you to build reusable UI structures with dynamic content by leveraging template directives, substitution parameters, and template functions. These tools offer a powerful way to separate design from logic while keeping your application modular and maintainable. In this blog post, we’ll explore how to use APEX templates with parameters, control flow directives, and functions to build highly customizable and dynamic UI components.

Using APEX Template Application with Parameters, Directives, and Functions

  1. Template Parameters
    Template parameters are defined as placeholders within your template text using the syntax {parameter_name}. These parameters are substituted at runtime with values passed from your APEX components like Cards, Regions, or Lists.

    Example:

    <div class="user-info">
      <strong>{USERNAME}</strong>
      <span>{EMAIL}</span>
    </div>
    
    • In your region/component settings, map USERNAME and EMAIL to the appropriate column or static values.

  2. Directives
    Directives control the flow and logic of the template. These include:

    • {if}, {elseif}, {else}, {endif}

    • {case}, {when}, {otherwise}, {endcase}

    • {loop}, {endloop}

    Example:

    {if STATUS = 'ACTIVE'}
      <span class="badge badge-success">Active</span>
    {else}
      <span class="badge badge-secondary">Inactive</span>
    {endif}
    
  3. Template Functions
    Functions like apex_escape.html() or apex_util.get_session_state() can be used inside template logic to perform transformations or retrieve runtime data.

    Example:

    <div>{apex_escape.html(NAME)}</div>
    
  4. Combining All Three
    You can combine parameters, functions, and directives to build logic-heavy templates:

    <div>
      {if SHOW_IMAGE = 'Y'}
        <img src="{IMAGE_URL}" alt="{apex_escape.html(USERNAME)}">
      {endif}
      <p>{USERNAME}</p>
    </div>
    

Best Practices

  • Keep your template logic simple—move complex decisions into SQL or PL/SQL when possible.

  • Use apex_escape functions to avoid XSS vulnerabilities.

  • Name parameters clearly and document expected values.

  • Test templates in isolation to verify conditional logic renders correctly.

  • Reuse template components across multiple pages for consistency and reduced maintenance.

Oracle APEX Documentation
You can find more details in the official Oracle documentation:
APEX Template Component Reference

1. applyTemplate Function

Purpose:
The applyTemplate function allows you to process a template string with placeholders and directives, substituting them with values or expressions.

Example:

You can create a template where placeholders like &P1_PROFILE_IMAGE_FILE are dynamically replaced with actual data from the page items or objects.

var options = { placeholders: { MESSAGE: "All is well." } };


apex.jQuery("#notification").html(

    apex.util.applyTemplate("<div>#MESSAGE#</div>", options)

);

This will render the message "All is well." inside a <div> element with the id #notification.

Use case:

  • Dynamically updating parts of the page (like messages or images) based on page items or variables.

2. arrayEqual Function

Purpose:
Compares two arrays and returns true if the arrays have the same number of elements and each element is strictly equal.

Example:

var result1 = apex.util.arrayEqual([1, "two", 3], [1, "two", 3]); // true

var result2 = apex.util.arrayEqual([1, "two", 3], [1, "two", "3"]); // false

Use case:

  • Compare arrays to check if the data has not changed.

  • Useful when validating if two arrays in a form are equal.

3. debounce Function

Purpose:
Returns a debounced version of a function. It delays execution of the function until after a certain amount of time has passed since the last call.

Example:

function formatValue() {

    var value = $v("P1_PHONE_NUMBER");

    $s("P1_PHONE_NUMBER_DISPLAY", value);

}


apex.jQuery("#P1_PHONE_NUMBER").on("keypress", apex.util.debounce(formatValue, 100));

Here, formatValue is called only after the user has stopped typing for 100 milliseconds, which reduces unnecessary function calls.

Use case:

  • To optimize handling of user input events like typing in form fields.

  • Avoid unnecessary server calls or updates when typing.

4. escapeCSS Function

Purpose:
Escapes CSS meta-characters in a string, ensuring that it can be safely used as part of a CSS selector.

Example:

apex.jQuery("#" + apex.util.escapeCSS("my.id"));

Use case:

  • Dynamically generating CSS selectors when element IDs or class names may contain special characters like periods (.), which could interfere with CSS selectors.

5. escapeHTML Function

Purpose:
Escapes special HTML characters to prevent XSS (Cross-Site Scripting) attacks when inserting untrusted data into the DOM.

Example:

apex.jQuery("#show_user").append(apex.util.escapeHTML($v("P1_UNTRUSTED_NAME")));

Use case:

  • When inserting user-generated content or external data into HTML, always escape it to prevent potential security vulnerabilities.

6. escapeHTMLAttr Function

Purpose:
Escapes special HTML characters in attribute values to avoid XSS attacks.

Example:

apex.jQuery("#show_user").attr("title", apex.util.escapeHTMLAttr($v("P1_UNTRUSTED_NAME")));

Use case:

  • Safely injecting user data into HTML attributes like title, alt, etc., to prevent XSS vulnerabilities.

7. getDateFromISO8601String Function

Purpose:
Converts an ISO 8601 date string into a JavaScript Date object.

Example:

var date1 = apex.util.getDateFromISO8601String("1987-01-23T13:05:09.040Z");

Use case:

  • Convert ISO 8601 date strings into JavaScript Date objects for manipulation or formatting.

  • Useful for dealing with date strings returned from APIs or databases.

8. getNestedObject Function

Purpose:
Returns a nested object at a specific path within a complex object structure.

Example:

var options = {

    views: {

        grid: {

            features: {

                cellRangeActions: true

            }

        }

    }

};


var o = apex.util.getNestedObject(options, "views.grid.features");

o.cellRangeActions = false; // now options.views.grid.features.cellRangeActions === false

Use case:

  • Used when you need to manipulate deeply nested properties within an object, ensuring that missing properties are created dynamically.

9. getScrollbarSize Function

Purpose:
Returns the size of the system scrollbar (if present).

Example:

var size = apex.util.getScrollbarSize();

console.log(size); // { width: 17, height: 17 }

Use case:

  • Helps in layout adjustments when adding or removing scrollbars dynamically.

10. htmlBuilder Function

Purpose:
Returns an htmlBuilder interface, which allows you to build HTML dynamically.

Example:

var builder = apex.util.htmlBuilder();

builder.div().content("Hello World").end();

var html = builder.toString();

apex.jQuery("#container").html(html);

Use case:

  • Dynamically constructing HTML elements in a more programmatic and reusable manner.

11. invokeAfterPaint and cancelInvokeAfterPaint Functions

Purpose:

  • invokeAfterPaint: Executes a function before the next browser paint (reflow/repaint).

  • cancelInvokeAfterPaint: Cancels the previously scheduled function call.

Example:

var id = apex.util.invokeAfterPaint(function() {

    console.log("This will be executed before the next repaint.");

});


// Optionally cancel the execution before the paint happens

apex.util.cancelInvokeAfterPaint(id);

Use case:

  • Perform tasks like animations or layout adjustments before the page is visually updated.

12. showSpinner Function

Purpose:
Displays a loading spinner on the page, indicating that some processing is taking place.

Example:

var lSpinner$ = apex.util.showSpinner($("#container_id"));

lSpinner$.remove(); // Removes the spinner once processing is complete.

Use case:

  • Display a spinner while processing a form or making an AJAX request to give users feedback that something is happening in the background.

13. stripHTML Function

Purpose:
Removes all HTML tags from a string.

Example:

var text = "Please <a href='www.example.com/ad'>click here</a>";

var strippedText = apex.util.stripHTML(text);

console.log(strippedText); // "Please click here"

Use case:

  • Strip unwanted HTML tags from user input or data that will be displayed as plain text.

14. toArray Function

Purpose:
Converts a value into an array. If the value is a string, it splits it based on a separator. If it's already an array or jQuery object, it converts it into a true JavaScript array.

Example:

var products = apex.util.toArray("Bags:Shoes:Shirts", ":");

console.log(products); // ["Bags", "Shoes", "Shirts"]

Use case:

  • Convert values into arrays for easier processing, such as when working with lists of items or elements on the page.


EXAMPLE:

Table – Table name: Requests

Column Name

Value Type

ID

Number

DisplayName

Varchar2

UserDisplayName

Varchar2

Title

Varchar2

MainPhone

Varchar2

MobilePhone

Varchar2

Email

Varchar2

ServiceRequest_Test1A

Varchar2

ServiceRequest_Test2A

Varchar2

ServiceRequest_Test3A

Varchar2

MaintenanceRequest_Test1B

Varchar2

MaintenanceRequest_Test2B

Varchar2

MaintenanceRequest_Test3B

Varchar2

OtherRequest_Test1C

Varchar2

OtherRequest_Test2C

Varchar2

OtherRequest_Test3C

Varchar2


A screenshot of a computer

AI-generated content may be incorrect.

Code:

  CREATE TABLE "CARDTEST" 

   ( "ID" NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER  NOCYCLE  NOKEEP  NOSCALE  NOT NULL ENABLE, 

"DISPLAYTHENAME" VARCHAR2(200 CHAR), 

"USERNAME" VARCHAR2(200 CHAR), 

"TITLE" VARCHAR2(200 CHAR), 

"MAINPHONE" VARCHAR2(200 CHAR), 

"MOBILEPHONE" VARCHAR2(200 CHAR), 

"EMAIL" VARCHAR2(200 CHAR), 

"SERVICEREQUEST_TEST1A" VARCHAR2(200 CHAR), 

"SERVICEREQUEST_TEST2A" VARCHAR2(200 CHAR), 

"SERVICEREQUEST_TEST3A" VARCHAR2(200 CHAR), 

"MAINTENANCEREQUEST_TEST1B" VARCHAR2(200 CHAR), 

"MAINTENANCEREQUEST_TEST2B" VARCHAR2(200 CHAR), 

"MAINTENANCEREQUEST_TEST3B" VARCHAR2(200 CHAR), 

"OTHERREQUEST_TEST1C" VARCHAR2(200 CHAR), 

"OTHERREQUEST_TEST2C" VARCHAR2(200 CHAR), 

"OTHERREQUEST_TEST3C" VARCHAR2(200 CHAR), 

CONSTRAINT "CARDTEST_PK" PRIMARY KEY ("ID")

  USING INDEX  ENABLE

   ) ;


Add the Data

A screenshot of a computer

AI-generated content may be incorrect.


ID

DISPLAYTHENAME

USERNAME

TITLE

MAINPHONE

MOBILEPHONE

EMAIL

SERVICEREQUEST_TEST1A

SERVICEREQUEST_TEST2A

SERVICEREQUEST_TEST3A

MAINTENANCEREQUEST_TEST1B

MAINTENANCEREQUEST_TEST2B

MAINTENANCEREQUEST_TEST3B

OTHERREQUEST_TEST1C

OTHERREQUEST_TEST2C

OTHERREQUEST_TEST3C

1

Y

Chuck, Doe

Manager

555-555-1212

555-555-7272

Chuck.Doe@someemail.com

Y

N

N

Y

Y

N

Y

N

Y

2

N

 

 

 

 

 

Y

N

Y

Y

Y

Y

N

Y

N


















Column Name

Value Type

Row 1 Data

Row 2 Data

ID

Number

1

2

DisplayTheName

Varchar2

Y

N

UserName

Varchar2

Chuck Doe

null

Title

Varchar2

MANAGER

null

MainPhone

Varchar2

555-555-1212

null

MobilePhone

Varchar2

555-555-7272

null

Email

Varchar2

Chuck.Doe@someemail.com

null

ServiceRequest_Test1A

Varchar2

Y

Y

ServiceRequest_Test2A

Varchar2

N

N

ServiceRequest_Test3A

Varchar2

N

Y

MaintenanceRequest_Test1B

Varchar2

Y

Y

MaintenanceRequest_Test2B

Varchar2

Y

Y

MaintenanceRequest_Test3B

Varchar2

N

Y

OtherRequest_Test1C

Varchar2

Y

N

OtherRequest_Test2C

Varchar2

N

Y

OtherRequest_Test3C

Varchar2

Y

N


We’ll be looking to display to cards (one for each data row)

Report Card-1 (row 1 data)


Display Name

Job Title

Business Phone

Mobile Phone

Email

Doe, Chuck

MANAGER

555-555-1212

555-555-7272

Chuck.Doe@someemail.com


Service Request

Maintenance Request

Other

TEST1A:   Y

TEST1B:   Y

TEST1C:    Y

TEST2A:   N

TEST2B:   N

TEST2C:    N

TEST3A:   N

TEST3B:   N

TEST3C:    N







Report Card-2 (row 2 data)


Service Request

Maintenance Request

Other

TEST1A:   Y

TEST1B:   Y

TEST1C:    N

TEST2A:   N

TEST2B:   Y

TEST2C:    Y

TEST3A:   Y

TEST3B:   N

TEST3C:    N




Create a “CARDS” Page

Step 1

A screenshot of a computer

AI-generated content may be incorrect.



A screenshot of a computer

AI-generated content may be incorrect.


A screenshot of a computer

AI-generated content may be incorrect.


Display of the web page with two cards, one for each data row.

A screenshot of a phone

AI-generated content may be incorrect.

We want to make the data display something like this:


Display Name

Job Title

Business Phone

Mobile Phone

Email

Doe, Chuck

MANAGER

555-555-1212

555-555-7272

Chuck.Doe@someemail.com


Service Request

Maintenance Request

Other

TEST1A:   Y

TEST1B:   Y

TEST1C:    Y

TEST2A:   N

TEST2B:   N

TEST2C:    N

TEST3A:   N

TEST3B:   N

TEST3C:    N



Lets set up the name header area:

A close-up of a phone number

AI-generated content may be incorrect.

Notes:

  • We want this area to display ONLY when DisplayTheName =Y

  • Any othe value will display a blank area.

  • Place this code in a Notepad for easier use and edit

To implement this logic we have to use the following:

#{if <condition>} 

    <content>

#{else} 

    <alternative_content>

#{/if}


In the code we will enter the following:

<div class="t-Report t-Report--stretch "  style="width:100%">


{case DisplayTheName /}

{when Y/}

<!--- Code for the table goes here when the value is “Y” -->

{when N/}

<!--- Code for the table goes here when the value is “N” -->

{otherwise/}

<!--- Code for the table goes here when the value is neither “Y”  or “N”-->

{endcase/}


</div>


Then we will add the HTML code for the table where the value is “Y”:

<table class="t-Report-report  u-textCenter">

<thead class="t-Report-report u-textCenter">


<tr>

    <th class="t-Report-colHead u-bold  ">Display Name</th>

    <th class="t-Report-colHead u-bold  ">Job Title </th>

    <th class="t-Report-colHead u-bold  ">Business Phone </th>

    <th class="t-Report-colHead u-bold  ">Mobile Phone </th>

    <th class="t-Report-colHead u-bold  ">Email </th>

</tr>

</thead>

<tbody>

<tr>

      <td class="t-Report-cell">  &USERNAME.  </td>

    <td class="t-Report-cell"> &TITLE.  </td>

    <td class="t-Report-cell">  &PRIMARYPHONE. </td>

    <td class="t-Report-cell">  &MOBILEPHONE.  </td>

    <td class="t-Report-cell">  &EMAIL. </td>

</tr>

 

</tbody>

</table>


Here is the code inserted into the “IF” loop

<div class="t-Report t-Report--stretch "  style="width:100%">


{case DisplayTheName /}

{when Y/}

<!--- Code for the table goes here when the value is “Y” -->

<table class="t-Report-report  u-textCenter">

<thead class="t-Report-report u-textCenter">


<tr>

    <th class="t-Report-colHead u-bold  ">Display Name</th>

    <th class="t-Report-colHead u-bold  ">Job Title </th>

    <th class="t-Report-colHead u-bold  ">Business Phone </th>

    <th class="t-Report-colHead u-bold  ">Mobile Phone </th>

    <th class="t-Report-colHead u-bold  ">Email </th>

</tr>

</thead>

<tbody>

<tr>

      <td class="t-Report-cell">  &USERNAME.  </td>

    <td class="t-Report-cell"> &TITLE.  </td>

    <td class="t-Report-cell">  &PRIMARYPHONE. </td>

    <td class="t-Report-cell">  &MOBILEPHONE.  </td>

    <td class="t-Report-cell">  &EMAIL. </td>

</tr>

 

</tbody>

</table>


{when N/}

<!--- Code for the table goes here when the value is “N” -->

{otherwise/}

<!--- Code for the table goes here when the value is neither “Y”  or “N”-->

{endcase/}


</div>


Now, where go to the page in order to add the new code.

Select the Report in the page

A screenshot of a computer

AI-generated content may be incorrect.


Select the attribute:

A black rectangular object with a black border

AI-generated content may be incorrect.


Navigate to the “Body” region on the right hand side, below the “Attributes” section and turn on the “Advanced Formating”

A black rectangular object with red lines

AI-generated content may be incorrect.


Select the Arrow button

A black rectangular object with a black stripe

AI-generated content may be incorrect.

Add the code in the window:

A screenshot of a computer

AI-generated content may be incorrect.

Your page should now look something like this:

A screen shot of a computer

AI-generated content may be incorrect.



If you run the page you will see that nothing has changed:

A white background with black lines

AI-generated content may be incorrect.

So what went wrong?

We need to make the search criteria upper case, so we will change the code 

  • from {case DisplayTheName /}

  • to {case DISPLAYTHENAME /}

A screen shot of a computer

AI-generated content may be incorrect.

Now save and run the page and we get the following result:

A screenshot of a computer

AI-generated content may be incorrect.


Next, we will add the rest of the HTML code, making sure that all of the replacement variables

  • Have a “&” at the start of the variable name.

  • Are in upper case.

  • Have a “.” Period at the end of the name.

  • They should look something like this “&XXXXXXXXXXX.”

<br>

<!-- second line-->

<div class="t-Report t-Report--stretch"  style="width:100%">

<table class="t-Report-report u-textCenter">

<thead class="t-Report-report u-textCenter">

<tr>

<th class="t-Report-colHead u-bold  ">Service  Request</th>

<th class="t-Report-colHead u-bold  ">Maintenance Request </th>

<th class="t-Report-colHead u-bold  ">Other </th>

</tr>

</thead>

<tbody>

<tr>

    <td class="t-Report-cell"> Test1A:&nbsp;&nbsp; &SERVICEREQUEST_TEST1A. </td>

    <td class="t-Report-cell"> Test1B: &nbsp;&nbsp; &MAINTENANCEREQUEST_TEST1B. </td>

    <td class="t-Report-cell"> Test1C: &nbsp;&nbsp; &OTHERREQUEST_TEST1C.</td>

</tr>

<tr>

    <td class="t-Report-cell"> Test2A:&nbsp;&nbsp;  & SERVICEREQUEST_TEST 2A.</td>

    <td class="t-Report-cell"> Test2B:&nbsp;&nbsp;  & MAINTENANCEREQUEST_TEST2B. </td>

    <td class="t-Report-cell"> Test2C:&nbsp;&nbsp;   & OTHERREQUEST_TEST 2C. </td>

</tr>



<tr>

    <td class="t-Report-cell"> Test3A:&nbsp;&nbsp;  & SERVICEREQUEST_TEST 3A.</td>

    <td class="t-Report-cell"> Test3B: & MAINTENANCEREQUEST_TEST3B. </td>

    <td class="t-Report-cell"> Test3C:&nbsp;&nbsp;  & OTHERREQUEST_TEST 3C.   </td>

</tr>

</tbody>

</table>

</div>


Make sure that you add the code outside of the “IF” loop

A screen shot of a computer

AI-generated content may be incorrect.



<br>

<!-- second line-->


<div class="t-Report t-Report--stretch"  style="width:100%">

<table class="t-Report-report u-textCenter">


<thead class="t-Report-report u-textCenter">


<tr>

<th class="t-Report-colHead u-bold  ">Service  Request</th>

<th class="t-Report-colHead u-bold  ">Maintenance Request </th>

<th class="t-Report-colHead u-bold  ">Other </th>

</tr>

</thead>

<tbody>

<tr>

    <td class="t-Report-cell">TEST1A:&nbsp;&nbsp; &SERVICEREQUEST_TEST1A. </td>

    <td class="t-Report-cell"> TEST1B: &nbsp;&nbsp; &MAINTENANCEREQUEST_TEST1B. </td>

    <td class="t-Report-cell"> TEST1C: &nbsp;&nbsp; &OTHERREQUEST_TEST1C.</td>

</tr>

<tr>

    <td class="t-Report-cell"> TEST2A:&nbsp;&nbsp;  &SERVICEREQUEST_TEST2A.</td>

    <td class="t-Report-cell"> TEST2B:&nbsp;&nbsp; &MAINTENANCEREQUEST_TEST2B. </td>

    <td class="t-Report-cell"> TEST2C:&nbsp;&nbsp;   &OTHERREQUEST_TEST2C. </td>

</tr>

<tr>

    <td class="t-Report-cell"> TEST3A:&nbsp;&nbsp;  &SERVICEREQUEST_TEST3A.</td>

    <td class="t-Report-cell"> TEST3B:&nbsp;&nbsp; &MAINTENANCEREQUEST_TEST3B. </td>

    <td class="t-Report-cell"> TESTC:&nbsp;&nbsp;  &OTHERREQUEST_TEST3C.   </td>

</tr>

</tbody>

</table>


A screenshot of a computer program

AI-generated content may be incorrect.


Here are the results:

When the “DISPLAYTHENAME” value is “Y” we display the header, otherwise nothing displays.

A screenshot of a computer

AI-generated content may be incorrect.


Conclusion
Using APEX template parameters, directives, and functions effectively enables you to design flexible, reusable components that respond dynamically to data and user input. These features reduce redundancy and increase the maintainability of your Oracle APEX applications. By following best practices and utilizing the available directives, you can streamline UI development and deliver consistent, scalable results.

 

How Do I Make a Faceted Search Map Page in Oracle APEX

Combining faceted search with a map region in Oracle APEX enables users to filter data visually and spatially at the same time. This design ...