๐ŸŽจ Mastering Oracle APEX Template Directives with Practical Examples

๐ŸŽจ Mastering Oracle APEX Template Directives with Practical Examples

๐Ÿ”น About

Oracle APEX Template Directives are one of the most powerful features for creating dynamic, reusable, and conditional UI components directly inside templates.

They allow developers to:

โœ… Add conditional rendering
โœ… Loop through data
โœ… Display dynamic content
โœ… Simplify template customization
โœ… Reduce JavaScript complexity

In this blog, weโ€™ll explore Oracle APEX Template Directives with practical examples and step-by-step implementation.


๐Ÿ› ๏ธ Tools and Technologies

  • Oracle APEX
  • SQL
  • HTML
  • Template Directives

๐Ÿš€ What are Template Directives?

Template Directives are special instructions used inside Oracle APEX templates to dynamically render content.

They work similarly to conditional statements and loops in programming languages.


๐ŸŽฏ Why Use Template Directives?

Without template directives:

โŒ Large amount of duplicate HTML
โŒ Complex conditional rendering
โŒ More JavaScript code
โŒ Harder template maintenance

With template directives:

โœ… Cleaner templates
โœ… Better readability
โœ… Dynamic rendering
โœ… Easier customization


๐Ÿ“Œ Common Template Directives

DirectivePurpose
{if/}Conditional rendering
{case/}Switch-case logic
{loop/}Iterate over data
{with/}Define local scope
{apply/}Apply subtemplate

๐Ÿš€ Where Can We Use Template Directives?

Template Directives can be used in:

โœ… Region Templates
โœ… Report Templates
โœ… List Templates
โœ… Cards
โœ… Email Templates
โœ… Content Rows


๐Ÿ“Œ Step-by-Step: How to Create Template Directives in Oracle APEX


โœ… Step 1: Create a Card Region

Go to:

Create Page โ†’ Region โ†’ Card Region

Use EMP table query:

SELECT empno, ename, job, sal, deptno FROM emp

โœ… Step 2: Body Formating

Go to:

Card Regionโ†’ Attributes

Set:

Body โ†’ Advance Formating โ†’ Enable

or choose any custom report template.


โœ… Step 3: Enable Template Directives

Go to:

Body โ†’ HTML Expression

Now add Template Directive code.


๐Ÿš€ Example 1: Conditional Display Using IF

Suppose we want to display employee salary conditionally.


๐Ÿ“Œ HTML Expression

<div class="template-card-container">
    <h3 class="u-textUpper u-bold" style="margin: 0 0 12px 0; font-size: 1.2rem; color: #1a2f3f;">
        &ENAME.
    </h3>

    <div class="card-badge-container">
        {if SAL/}
            <span class="t-Badge t-Badge--success t-Badge--pill">
                <span class="t-Badge-label">Salary: &SAL.</span>
            </span>
        {else/}
            <span class="t-Badge t-Badge--danger t-Badge--pill">
                <span class="t-Badge-label">No Salary</span>
            </span>
        {endif/}
    </div>
</div>

โœ… Result

If SAL contains value:

โœ” Green badge displays

Else:

โœ” Red badge displays


๐Ÿš€ Example 2: CASE Directive

CASE directives work like switch-case logic.


๐Ÿ“Œ HTML Expression

{case JOB/}

{when MANAGER/}
   <span class="fa fa-user-tie"></span>
   Manager

{when ANALYST/}
   <span class="fa fa-chart-line"></span>
   Analyst

{when CLERK/}
   <span class="fa fa-user"></span>
   Clerk

{otherwise/}
   Employee

{endcase/}

โœ… Result

Different icons display based on JOB value.


๐Ÿš€ Example 3: Conditional CSS


๐Ÿ“Œ HTML Expression

<div class="
{if DEPTNO/}
dept-active
{else/}
dept-inactive
{endif/}">
    #ENAME#
</div>

๐Ÿš€ Example 4: Highlight High Salary Employees


๐Ÿ“Œ SQL Query

SELECT
    empno,
    ename,
    sal,
    CASE
        WHEN sal > 3000 THEN 'HIGH'
        ELSE 'NORMAL'
    END salary_status
FROM emp

๐Ÿ“Œ HTML Expression

<div class="
{case SALARY_STATUS/}

{when HIGH/}
high-salary

{otherwise/}
normal-salary

{endcase/}">
    
    #ENAME#
    
</div>

๐Ÿš€ Example 5: LOOP Directive

LOOP directives are useful in advanced templates.


๐Ÿ“Œ Example

{loop EMPLOYEES/}

<div>
   #ENAME#
</div>

{endloop/}

๐Ÿš€ Example 6: WITH Directive

The WITH directive creates reusable local variables.


๐Ÿ“Œ Example

{with EMP_NAME:=ENAME/}

<h2>&EMP_NAME.</h2>

{endwith/}

๐Ÿš€ Example 7: APPLY Directive

Used to apply reusable subtemplates.


๐Ÿ“Œ Example

{apply EMP_TEMPLATE/}

๐ŸŽฏ Real-World Use Cases

Template Directives are extremely useful for:

โœ” Employee dashboards
โœ” Status-based cards
โœ” Notification panels
โœ” Dynamic badges
โœ” Role-based UI
โœ” Custom reports


๐ŸŽจ Example CSS

Add this CSS in:

Page โ†’ CSS โ†’ Inline
.high-salary {
    background: #d4edda;
    padding: 10px;
    border-radius: 10px;
}

.normal-salary {
    background: #f8d7da;
    padding: 10px;
    border-radius: 10px;
}

โš ๏ธ Important Considerations

๐Ÿ”น Use Correct Syntax

Template directives are case-sensitive.

Incorrect syntax may break rendering.


๐Ÿ”น Avoid Heavy Business Logic

Use directives mainly for UI rendering.

Keep business logic in:

โœ… SQL
โœ… PL/SQL


๐Ÿ”น Test Nested Directives Carefully

Complex nested directives can become difficult to debug.


๐Ÿš€ Advanced Use Cases

You can use Template Directives for:

โœ” Dynamic Cards
โœ” Report Formatting
โœ” Email Templates
โœ” Dashboard Widgets
โœ” Notification Systems
โœ” Dynamic Icons


๐Ÿ“Š Workflow

SQL Query
     โ†“
Template Directive Processing
     โ†“
Dynamic HTML Rendering
     โ†“
Final UI Output

๐Ÿ”ฅ Why Oracle APEX Developers Love Template Directives

Template Directives help developers create dynamic user interfaces without writing excessive JavaScript.

Benefits include:

โœ… Reusable templates
โœ… Cleaner HTML
โœ… Better maintainability
โœ… Flexible rendering


๐ŸŽ‰ Conclusion

Oracle APEX Template Directives are a powerful feature for building intelligent and dynamic UI components.

Using directives like:

โœ… IF
โœ… CASE
โœ… LOOP
โœ… WITH
โœ… APPLY

developers can create highly customizable templates with minimal effort.

If you want cleaner templates and smarter rendering in Oracle APEX, Template Directives are definitely worth learning.


Happy Coding!

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply

    Your email address will not be published. Required fields are marked *