Skip to main content

PHP & HTML Custom Code Injection

Write once โ€” inject anywhere. Gain total control over logic, layout, and rendering. What Is Custom Code Injection in JCB?

What Is Custom Code Injection in JCB?

JCB allows developers to define and inject reusable PHP and HTML code blocks throughout their components. Whether you need a custom helper function, a data transformation routine, a special HTML layout block, or a form override โ€” JCB provides structured ways to store and re-use custom logic via its Custom Code system and tag-based injection.

Unlike Insert/Replace tags, which preserve code in compiled files, the Custom Code system lets you manage your code blocks directly in the JCB interface. These blocks are injected during the compilation process at specific tagged locations, ensuring that your logic is inserted where and how you want it, every time.

Key Features

  • ๐Ÿง  GUI-Managed Code Blocks: Define PHP or HTML/JS code blocks inside JCBโ€™s โ€œCustom Codeโ€ manager. Tag them with a unique name (key) and organize by context (view, field, layout, etc.).
  • ๐Ÿ”‘ Key-Based Injection: Inject code blocks into any part of a view, model, layout, or field using smart placeholders like:[CUSTOMCODE=getCustom]
  • ๐Ÿ”„ Central Reuse: Write code once and inject it in multiple places. Great for repeating logic like formatting, access checks, reusable layouts, etc.
  • โš™๏ธ Context-Aware Output: Custom code can vary its behavior depending on whether itโ€™s injected in admin/site, item/list views, frontend/backend, or field get/save.
  • ๐Ÿงฉ Field, View, and Layout Support: Inject logic at the field level (e.g. save hooks), inside templates or snippets, or within model/controller files.
  • ๐Ÿ” Track Usage & Tags: JCB tracks where each custom code block is used, making debugging and refactoring easier.
  • ๐Ÿ›  Compiler-Safe: Injected code is written only at compile time. You donโ€™t manually edit the compiled file โ€” JCB controls the logic insertion safely.
  • ๐Ÿ”’ Supports PHP, HTML, JavaScript: Choose your syntax and apply it based on the type of output โ€” render views, backend logic, AJAX handling, UI markup, or inline event handling.

Use Cases

  • Inject PHP logic into a modelโ€™s save method for preprocessing or validation
  • Add a reusable HTML snippet to multiple layouts (e.g. โ€œPowered byโ€ footer)
  • Define a JavaScript callback block for a dynamic field
  • Wrap a view with a conditional rendering check
  • Create a library of formatting helpers or partial views
  • Store a site-wide notice block and include it across views

How to Use in JCB

  1. Navigate to Components โ†’ Custom Code
  2. Create a new Custom Code block
    • Name it (e.g. renderPriceWithDiscount)
    • Select type: PHP, HTML, or JS
    • Write your code
  3. In the context (view, layout, template, snippet, field):
    • Use [CUSTOMCODE=renderPriceWithDiscount]
  4. Compile your component โ€” your code is now injected wherever itโ€™s tagged.

๐Ÿง  Pro Tip: Use naming conventions like renderPriceWithDiscount (e.g. getAdminAccess) to stay organized in large projects.

Conclusion

JCBโ€™s Custom Code Injection system empowers you to keep logic modular, reusable, and centralized. Whether you're a developer building advanced workflows or a team managing multiple components, this feature ensures that you never write the same code twice โ€” and that every block is injected with precision and safety during compile.