Template compilation using processContent
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.
Syntax
@processContent(contentProcessorFunction)Implementing processContent
Basic Structure
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
}Parameters
The processContent function receives four parameters:
compiler: TheViewCompilerinstance, used for compiling views.resources: TheViewResourcesinstance, containing view-related resources.node: The DOM node representing the view (usually aDocumentFragment).instruction: TheBehaviorInstructionfor the custom element.
Return Value
true: Aurelia will compile the content normally after your processing.false: Aurelia will not compile the content, leaving it as-is.
Use Cases and Examples
1. Preprocessing Text Content
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 {}2. Modifying DOM Structure
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 {}3. Implementing Custom Syntax
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 {}4. Conditional Compilation
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 {}Advanced Techniques
Accessing ViewResources
Use the resources parameter to register custom elements or attributes:
@processContent((compiler, resources, node, instruction) => {
resources.registerElement('custom-tag', CustomTagClass);
return true;
})Working with the ViewCompiler
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;
})Best Practices
Performance Considerations:
Use
processContentjudiciously, as it runs for each instance of the custom element.Keep manipulations focused and efficient.
Maintainability:
Document the purpose and behavior of your
processContentfunction.Consider extracting complex logic into separate functions for clarity.
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.
Conclusion
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.
Last updated
Was this helpful?