🎨 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 *