Search This Blog

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 to Escape an Open Bracket in Oracle APEX Templates

 

Introduction
Oracle APEX template directives use curly brackets {} as part of their substitution and logic syntax. This can create a challenge when you need to include a literal open bracket { in your template output—for example, when displaying JSON, HTML snippets, or code samples. To prevent Oracle APEX from interpreting it as the beginning of a directive, you need to escape it properly. This blog post explains how to escape an open bracket in Oracle APEX templates, when and why to do it, and offers best practices for clarity and maintenance.

How to Escape an Open Bracket in Oracle APEX Templates
In Oracle APEX, the open bracket { is reserved for use with template directives, such as {if}, {loop}, {apex$} and substitutions like {COLUMN_NAME}. If your output needs to include a literal {, such as in a code block or a display label, you cannot type it directly. Instead, APEX provides an escape sequence.

To display a literal {, use:

{{!

This tells APEX to output a single { character without starting a directive.

Example
If you want to output a JSON snippet inside a card template:

<pre>
  {{!"name": "John", "age": 30}
</pre>

This will render:

{name: "John", "age": 30}

Notes

  • Only the first { needs to be escaped.

  • Closing brackets } generally do not need escaping unless paired with directive logic.

Best Practices

  • Use {{! only when you need literal output and are sure it’s not part of an APEX directive.

  • Keep template content clean—wrap code or JSON examples in <pre> or <code> blocks for readability.

  • Avoid placing escape characters within dynamic SQL or PL/SQL code—this technique is only for APEX template regions.

Oracle APEX Documentation
Refer to the official Oracle APEX documentation on template directives and escaping rules:
Oracle APEX Template Directive Reference

In Oracle APEX, when working with templates, it is possible to encounter a situation where the open bracket { can be confused for the start of a directive (e.g., {if} or {loop}). This issue arises when the open bracket appears in the middle of the code, followed by a directive, causing a potential conflict. To avoid this confusion, you can escape the open bracket using the syntax {{/}.

In this tutorial, we will walk you through how and when to use the escape sequence {{/} in APEX templates, and provide examples to demonstrate its usage.


1. Why Do You Need to Escape the Open Bracket {?

The APEX templating engine uses brackets { and } to define directives, such as {if}, {loop}, {case}, etc. In some cases, when an open bracket is placed before these directives on the same line, it might be misinterpreted as the start of a directive, causing a parsing error.

Escape Sequence:

  • {{/}: The escape sequence to use when you need to display an open bracket { in the template text without it being interpreted as the start of a directive.


2. When Do You Need to Escape the Open Bracket {?

You need to escape the { when it appears before a directive in a line, causing the template engine to interpret it as part of a directive. For example:

{if VAL/}&VAL.{else/}unknown{endif/}

In the above case, if you want to include { literally (e.g., in a mathematical expression), the APEX engine will interpret it as the start of a directive. To avoid this, use the escape sequence {{/} to display the { symbol literally.


3. Example 1: Escaping the Open Bracket in a Mathematical Expression

Scenario:

You want to display coordinates such as {c, d}, but without the template engine interpreting { as the start of a directive.

Without escaping:

<span>The coordinates {c, d} = {if VAL/}&VAL.{else/}unknown{endif/}</span>

This could cause an error because the APEX engine might think {c, d} is part of a directive. To prevent this issue, you should escape the { by using the escape sequence {{/}.

Escaped version:

<span>The coordinates {{/}c, d} = {if VAL/}&VAL.{else/}unknown{endif/}</span>

Explanation:

  • {{/}c, d}: This ensures the { character is displayed literally and not treated as part of a directive.

  • The rest of the expression {if VAL/}&VAL.{else/}unknown{endif/} works as expected and shows either the value of VAL or unknown.

Expected Output:

If VAL is set to 10, the output will be:

The coordinates {c, d} = 10

If VAL is not set, it will display:

The coordinates {c, d} = unknown


4. Example 2: No Need to Escape Brackets in Simple Cases

In many cases, you won’t need to escape the open bracket {. If the { is simply part of your content and not followed by a directive on the same line, it will be treated as a regular character.

Scenario:

You want to display {c, d} in a regular expression or a descriptive sentence.

<span>The coordinates { c, d } = {if VAL/}&VAL.{else/}unknown{endif/}</span>

Explanation:

  • Here, the { c, d } part is simply text and doesn’t conflict with any directives.

  • The rest of the template will work as usual, displaying either the value of VAL or unknown.

Expected Output:

If VAL is set to 20, the output will be:

The coordinates { c, d } = 20

If VAL is not set, it will display:

The coordinates { c, d } = unknown


5. Example 3: Escaping Brackets When Using Directives

In some situations, the bracket { might appear in combination with a directive, and you need to escape it to prevent conflicts. Here's an example where you need to escape the open bracket before using an if directive.

Scenario:

You want to display an expression such as {c, d} = 10 only if a certain condition is true.

Without escaping:

<span>{c, d} = {if VAL/}&VAL.{else/}unknown{endif/}</span>

This could cause a parsing error, so you should use {{/} to escape the {.

Escaped version:

<span>{{/}c, d} = {if VAL/}&VAL.{else/}unknown{endif/}</span>

Expected Output:

If VAL is set to 10, the output will be:

{c, d} = 10

If VAL is not set, it will display:

{c, d} = unknown


6. Example 4: No Need to Escape Inside Block Directives

If you are using a block directive, such as {if} or {loop}, where the brackets {} appear inside the block, you don’t need to escape them. For instance:

{if VAL/}

    <span>The coordinates {c, d} = {VAL}</span>

{else/}

    <span>The coordinates {c, d} = unknown</span>

{endif/}

This will work perfectly fine because the {c, d} part is outside of the directive, and APEX doesn’t interpret the brackets as part of the directive.


7. Summary of When to Escape the Open Bracket

  • Escape Bracket: Use {{/} when you want to display { literally in a context where the APEX engine might interpret it as the start of a directive. This typically happens when an open bracket { is immediately followed by a directive, such as {if}, {loop}, or others.

  • No Escape Needed: You don't need to escape { when it’s simply part of the content (i.e., not immediately followed by a directive).


8. Best Practices

  • Be aware of directive boundaries: If you’re working with template expressions or custom formatting, always ensure that the { is not interpreted as part of a directive. If it’s in a context where it might be, use the escape sequence.

  • Use escaping sparingly: Only escape { when necessary. Overusing it can make your templates harder to read.

 

Conclusion
Escaping an open bracket in Oracle APEX templates is crucial when working with content that includes code, JSON, or any text requiring literal { characters. By using the {{! syntax, developers can safely display these characters without interfering with template rendering logic. Applying this technique with care ensures your templates behave as expected while remaining easy to maintain and understand.

How to Use APEX Comments with Syntax {!/}

Introduction
When working with Oracle APEX templates, maintaining clarity and structure is essential for long-term maintainability. APEX supports inline comment syntax using {!<comment-text>/} to help developers add helpful notes or disable parts of the template code during development. These comments are not rendered in the final HTML output, making them ideal for documentation or temporary code exclusion without impacting the UI.

How to Use APEX Comments with Syntax {!<comment-text>/}
Oracle APEX provides a simple and effective way to insert comments directly into template text using the {! ... /} syntax. These comments are processed at the APEX rendering layer and are not passed to the browser or client-side.

Example Use Case
Suppose you're designing a custom card template and want to mark where a new feature will be added later:

<div class="card-header">
  <h3>{TITLE}</h3>
  {!This is where we may insert an icon later/}
</div>

This comment will be ignored during rendering and will not appear in the page source. It’s useful for leaving developer notes without cluttering the UI or exposing implementation notes to end users.

Commenting Out a Directive Block
You can also comment out portions of APEX directives to temporarily disable them without removing them from the template:

{!{if STATUS = 'NEW'}
  <span class="badge">New</span>
{endif}/}

This allows you to preserve logic while disabling it during testing or iterative design.

Best Practices

  • Use {!.../} comments to document complex logic in template code for better team collaboration.

  • Avoid overusing comments in production templates—clean and readable code is better than excessive commentary.

  • Do not use HTML-style comments (<!-- -->) in APEX template directives, as these may not behave consistently across components.

  • Use comments to separate logical blocks inside loops or conditionals for clarity.

Oracle APEX Documentation
You can find official documentation on template directive syntax, including comments, in the Oracle APEX documentation here:
Oracle APEX Template Directives Guide

In Oracle APEX templates, comments are a powerful tool to help organize your code, provide explanations, or temporarily disable sections of your template without affecting the execution of your APEX application. The APEX comment syntax {!<comment-text>/} allows you to embed comments in your template code.

In this tutorial, we'll explore how to create and use comments in Oracle APEX templates using the {!<comment-text>/} syntax.


1. Basic Syntax of APEX Comments

The syntax for adding a comment in APEX is as follows:

{!<comment-text>/}

Where:

  • {!: Marks the beginning of the comment.

  • <comment-text>: The text inside the comment, which can be any descriptive content, explanation, or even temporarily disabled code.

  • /}: Marks the end of the comment.

The content inside {! and /} is not processed or displayed by APEX, making it useful for adding comments without affecting the rendering of the template.


2. Examples of Using APEX Comments

Example 1: Basic Comment

In a template, you can add a simple comment to explain a section of code:

{!This is a comment explaining the next section of the code/}

<h1>Welcome to My APEX Application</h1>

Explanation:

  • The comment will not appear in the browser output.

  • The <h1> tag and its content will still be rendered as normal on the page.

Output:

The output on the web page will just display:

<h1>Welcome to My APEX Application</h1>

Example 2: Commenting Out a Section of Code

You can use comments to temporarily disable a section of code without deleting it. For example:

{!<div class="old-feature">This section is no longer used.</div>/}

<h2>Current Feature Section</h2>

<p>New content goes here.</p>

Explanation:

  • The <div> element will not be rendered, effectively disabling the "old feature" section.

  • The <h2> and <p> tags will be rendered as normal.

Output:

<h2>Current Feature Section</h2>

<p>New content goes here.</p>

Example 3: Commenting a Complex Template with Multiple Sections

In a more complex APEX template, you can use comments to organize different sections of your code:

{!Header Section - This will be displayed at the top of the page/}

<header>

    <h1>Page Title</h1>

    <p>Subtitle or description</p>

</header>


{!Content Section - Main body content starts here/}

<div>

    <p>Here is the main content of the page.</p>

</div>


{!Footer Section - Footer will be displayed at the bottom/}

<footer>

    <p>Footer content goes here.</p>

</footer>

Explanation:

  • Each section is commented to indicate its purpose, making the code easier to read and maintain.

  • The browser will render all the HTML elements (header, content, and footer), but the comments are only for developers.

Output:

<header>

    <h1>Page Title</h1>

    <p>Subtitle or description</p>

</header>


<div>

    <p>Here is the main content of the page.</p>

</div>


<footer>

    <p>Footer content goes here.</p>

</footer>

The comments are not visible in the browser output, but they help developers understand the structure and purpose of each section.


3. Using Comments for Debugging and Development

Sometimes, during the development process, you might want to disable a specific part of the template for testing or debugging. Here's an example:

Example 4: Disabling Code Temporarily

{!<div class="debug-section">This section is temporarily disabled for debugging purposes.</div>/}

<p>Here is some visible content that will be displayed while the debugging section is disabled.</p>

Explanation:

  • The <div class="debug-section"> will not be rendered because it is commented out.

  • The <p> tag with content will be rendered as normal.

Output:

<p>Here is some visible content that will be displayed while the debugging section is disabled.</p>


4. Nested Comments (Not Supported)

It’s important to note that nested comments are not supported in APEX templates. That means you cannot have a comment inside another comment. For example:

{! This is a comment {! Nested comment here /}! /}

This will result in a syntax error and should be avoided. Always ensure your comments are properly closed before starting a new comment.


5. Benefits of Using APEX Comments

  • Documentation: Adding comments helps document your templates, making it easier for other developers to understand the code structure.

  • Disabling Code: You can use comments to temporarily disable parts of the template during development or testing, which can speed up troubleshooting.

  • Code Organization: Using comments to mark sections (e.g., header, content, footer) makes the code easier to navigate and maintain.

  • No Impact on Rendering: Comments are never rendered in the output, meaning they won’t affect the layout or performance of the application.


6. Best Practices for Using Comments

  • Be Clear and Concise: Keep comments simple and direct. Avoid over-commenting or adding unnecessary comments that do not add value.

  • Use Comments for Sections: Group related blocks of code with comments to make it easier to understand the purpose of different sections.

  • Avoid Commenting Out Large Code Blocks: Instead of commenting out large sections of code, consider using version control systems (e.g., Git) to track changes and roll back when necessary.

 

Conclusion
Oracle APEX's {!.../} comment syntax is a small but powerful feature that aids in documenting, debugging, and structuring your templates. By using APEX-specific comments, you ensure that notes stay within the developer layer and do not leak into the end-user interface. Incorporating these comments into your workflow leads to cleaner templates and easier collaboration with other developers working on the same application.

 

UI Defaults

 In Oracle APEX, User Interface (UI) Defaults are a set of metadata-driven, table- and column-scoped attributes that APEX consults when it g...