The processContent
function is a powerful feature in Aurelia 1 that allows developers to manipulate the content of a custom element's view before it's compiled. This feature is crucial for scenarios where you need to preprocess templates, implement custom syntax, or modify the DOM structure before Aurelia's standard compilation process.
@processContent(contentProcessorFunction)
To use processContent
, add it as a decorator to your custom element class:
import { customElement, processContent } from 'aurelia-framework';
@customElement('my-element')
@processContent((compiler, resources, node, instruction) => {
// Your content processing logic here
return true;
})
export class MyElement {
// Your element logic
}
The processContent
function receives four parameters:
compiler
: TheViewCompiler
instance, used for compiling views.resources
: TheViewResources
instance, containing view-related resources.node
: The DOM node representing the view (usually aDocumentFragment
).instruction
: TheBehaviorInstruction
for the custom element.
true
: Aurelia will compile the content normally after your processing.false
: Aurelia will not compile the content, leaving it as-is.
Replace placeholders in the template:
@customElement('date-display')
@processContent((compiler, resources, node, instruction) => {
const content = node.textContent;
node.textContent = content.replace(/\{\{TODAY\}\}/g, new Date().toLocaleDateString());
return true;
})
export class DateDisplay {}
Add or remove elements from the template:
@customElement('enhanced-list')
@processContent((compiler, resources, node, instruction) => {
const list = node.querySelector('ul');
if (list) {
const wrapper = document.createElement('div');
wrapper.className = 'list-wrapper';
list.parentNode.insertBefore(wrapper, list);
wrapper.appendChild(list);
}
return true;
})
export class EnhancedList {}
Process custom attribute syntax:
@customElement('custom-button')
@processContent((compiler, resources, node, instruction) => {
const button = node.querySelector('button');
if (button && button.hasAttribute('custom-color')) {
const color = button.getAttribute('custom-color');
button.style.backgroundColor = color;
button.removeAttribute('custom-color');
}
return true;
})
export class CustomButton {}
Selectively compile parts of the template:
@customElement('conditional-element')
@processContent((compiler, resources, node, instruction) => {
const shouldCompile = node.hasAttribute('compile');
if (!shouldCompile) {
// Remove all bindable attributes to prevent compilation
Array.from(node.attributes).forEach(attr => {
if (attr.name.startsWith('bind-')) {
node.removeAttribute(attr.name);
}
});
}
return shouldCompile;
})
export class ConditionalElement {}
Use the resources
parameter to register custom elements or attributes:
@processContent((compiler, resources, node, instruction) => {
resources.registerElement('custom-tag', CustomTagClass);
return true;
})
Utilize the compiler
to manually compile parts of the view:
@processContent((compiler, resources, node, instruction) => {
const customPart = node.querySelector('.custom-part');
if (customPart) {
const factory = compiler.compile(customPart, resources);
// Use the factory as needed
}
return true;
})
- Performance Considerations:
- Use
processContent
judiciously, as it runs for each instance of the custom element. - Keep manipulations focused and efficient.
- Use
- Maintainability:
- Document the purpose and behavior of your
processContent
function. - Consider extracting complex logic into separate functions for clarity.
- Document the purpose and behavior of your
- Compatibility:
- Be aware that DOM manipulations may affect Aurelia's binding system.
- Test thoroughly, especially with complex templates or bindings.
- Alternatives:
- Consider using value converters, custom attributes, or composition for simpler cases.
The processContent
function is a powerful tool in Aurelia 1 for customizing template compilation. It offers unparalleled flexibility in manipulating views before they're processed by Aurelia's standard compilation. While powerful, it should be used thoughtfully, considering both performance and maintainability implications.