Skip to main content

Nested Custom Code Support

By enabling composable and centrally defined logic layers, it allows you to scale your Joomla component architecture with clarity, consistency, and flexibility.

What Is It?

Nested Custom Code Support means one Custom Code entity can contain special insert tags (e.g. [CUSTOMCODE=myReusableBlock]) that reference and pull in other Custom Code blocks during the build process.

Instead of duplicating similar logic across components or views, you:

  • Define a reusable logic block (e.g., sanitizeInput)
  • Create a โ€œparentโ€ Custom Code block (e.g., save logic)
  • Nest the reusable logic using a placeholder tag
  • JCB replaces the tag with the full nested block during compile

๐Ÿ”„ Compilation is recursive โ€” nested blocks can themselves include more blocks, with safeguards against infinite loops.

Where Can You Use It?

  • ๐Ÿง  Inside Controllers (save, delete, cancel)
  • ๐Ÿ’พ In Models (prepareTable, validate)
  • ๐Ÿ“„ In Layouts or Templates
  • โš™๏ธ In Field Logic (on save/get)
  • ๐Ÿงฌ In Snippets (HTML, JS, or mixed)

Key Features

  • ๐Ÿงฉ Logic Composition: Break large functions into reusable building blocks โ€” inject as needed.
  • ๐Ÿ” DRY Architecture: Write it once, nest it anywhere โ€” no duplication.
  • ๐Ÿง  Compile-Time Safety: Nested injection happens only during compile, so logic remains clear and easy to trace in the final source code.
  • ๐Ÿ” Secure Namespacing: Nested blocks inherit and respect JCBโ€™s compile-time variable context (placeholders still work inside nested logic).

Use Case Examples

  • A reusable error handler injected into multiple save controllers
  • A shared API authentication check nested into multiple views
  • A language string parser reused across multiple template renderers
  • A data formatting function reused across different layout views

Why It Matters

  • โœ… Simplifies maintenance: update nested logic in one place
  • โœ… Encourages modular thinking and micro-function structure
  • โœ… Promotes sharing logic across large or complex components
  • โœ… Avoids spaghetti code and long, monolithic code blocks

Best Practices

  • Give nested blocks clear and meaningful names (e.g., validateUserAccess)
  • Keep nested logic focused on one task (single-responsibility principle)
  • Avoid deep recursion (>3 levels) for performance and readability
  • Use nesting for shared concerns: validation, error reporting, utility logic

Conclusion

Nested Custom Code support turns JCBโ€™s compiler into a true modular logic engine. By enabling composable and centrally defined logic layers, it allows you to scale your Joomla component architecture with clarity, consistency, and flexibility. It's code reuse โ€” supercharged.

Summary

JCB allows Custom Code blocks to call or include other Custom Code blocks during compilation, enabling a modular architecture where logic is broken down into reusable, composable parts. This nested system dramatically improves maintainability, scalability, and DRY (Don't Repeat Yourself) compliance.