I have a javascript array which contains html for a bunch of templates.
var TemplateLibrary = [
{
name: "hero-1",
code: '<header class="pt-xxl pb-xxl bps-pt-0 bps-pb-0 bps-height-100 ta-center bc-blue js-Template"><div class="display-table height-100 width-100"><div class="display-tableCell va-middle"><div class="width-90 bpm-width-50 horizontallyCenter"><h1 class="fs-xxxl lh-xxxl bps-fs-xxxxl bps-lh-xxxxl fw-3 color-white ls-s bps-ls-xs js-componentHighlightText">This is hero 1.</h1></div></div></div></header>'
}
]
I have a click event which finds a specific template in the array and gets its content.
// Append component to text editor when clicked
$('.js-TemplateThumbnail').click(function() {
// Get name of clicked template
var TemplateName = $(this).data("name");
// Find template in array
var result = TemplateLibrary.filter(function (obj) {
return obj.name === TemplateName;
})[0];
// Get template content
var templateContent = result.code;
// Define new template container
var newTemplateContainer = $('<div class="position-relative hideChild bps-height-90 js-TemplateContainer">');
// Insert selected template into new template container
newTemplateContainer.html(templateContent);
// Build new template
$('.js-Canvas .js-TemplateContainer').last().after(newTemplateContainer);
});
My problem is, it's awkward editing those templates inside the array, so I want to move the "code" data into a file inside a different folder. How do I locate the filename and pull its contents using rails or ajax? Something like this?
var TemplateLibrary = [
{
name: "hero-1",
code: '<%= ../js/templates/hero-1.html %>'
}
]