You can use an instance of InlineViewStrategy bound to a compose
element.
Here's a demo: https://gist.run/?id=4e2ec80c1010c638e908589ce3fc5067
Custom Element:
inline.js
templateChanged()
ensures real dynamic behaviour for this demo (re-rendering as you type). Although, it might not be needed in all cases.
import { bindable, bindingMode, InlineViewStrategy } from 'aurelia-framework';
export class Inline {
viewStrategy;
@bindable({ defaultBindingMode: bindingMode.oneWay })
template;
@bindable({ defaultBindingMode: bindingMode.oneWay })
displayValue;
attached() {
this.render();
}
templateChanged() {
this.render();
}
render() {
this.viewStrategy = new InlineViewStrategy(`<template>${this.template}</template>`);
}
}
inline.html
<template>
<compose view.bind="viewStrategy"></compose>
</template>
Usage in a view:
For example, we'd like to display a nice icon.
app.js
export class App {
customTemplate = '<i class="fa fa-3x fa-${displayValue}"></i>';
customValue = 'stack-overflow';
}
app.html
<template>
<require from="./inline"></require>
<div class="container-fluid">
<h4 class="page-header">Inline template in custom component</h4>
<div class="form-group">
<label>Template:</label>
<input class="form-control" type="text" value.bind="customTemplate" />
</div>
<div class="form-group">
<label>Display Value:</label>
<input class="form-control" type="text" value.bind="customValue" />
</div>
<div class="panel panel-primary">
<div class="panel-heading">Rendered view:</div>
<div class="panel-body">
<inline template.bind="customTemplate" display-value.bind="customValue"></inline>
</div>
</div>
</div>
</template>
Hope, this helps.