I'm currently developing a reusable UI component and am exploring options to allow the user of this component to provide their own template for a specific section within it.
Utilizing TypeScript, I have been experimenting with string interpolation as a potential solution. Here is my progress so far:
export class Pager {
pageNumber: number = 1;
getButtonHtml(buttonContentTemplate?: string, isDisabled?: boolean): string {
buttonContentTemlpate = buttonContentTemplate || '${this.pageNumber}';
isDisabled = isDisabled || false;
return `<button id="button-id" type="button" ${!isDisabled ? '' : disabledAttribute}>
${buttonContentTemplate}
</button>`;
}
}
While I have additional methods in place to update the page number based on user input/interaction, my goal is for getButtonHtml
to return
<button id="button-id" type="button">1</button>
when called. Unfortunately, it currently returns <button id="button-id" type="button">${this.pageNumber}</button>
.
I am curious if there is a method to prompt JavaScript to reevaluate the string and interpolate the remaining placeholders?
After reviewing the MDN article on this matter, I suspect that utilizing the String.raw
method may provide a solution. However, despite various attempts, I have yet to achieve success.
Any assistance or guidance on this matter would be highly appreciated.