I am converting a handlebars template to be reusable within the compiled page.
Sample code:
{{title}}
<div id="desktop">
{{#each menu}}
<a href="{{link}}">{{name}}</a>
{{/each}}
</div>
On the compiled page, I am wanting
Search Engines
<div id="desktop">
<a href="https://google.com">Google</a>
<a href="https://yahoo.com">Yahoo</a>
<script type="text/template" id="repeatable-menu"><!--
<a href="{{link}}">{{name}}</a>
--></script>
</div>
I am able to get halfway there by doing a regex replace on the template before it is compiled by handlebars and get the output
Search Engines
<div id="desktop">
<a href="https://google.com">Google</a>
<a href="https://yahoo.com">Yahoo</a>
<script type="text/template" id="repeatable-menu"><!--
<a href=""></a>
--></script>
</div>
by using the regex statement:
$templateHTML = preg_replace('/({{#each (.*?)}}(.*?){{\\/each}})/s', '$1<script type="text/template" id="repeatable-$2"><!--$3--></script>', $templateHTML, -1);
Another alternative I have come up with is to break up the handlebars statements with something like this <a href="{|{link}|}">{|{name}|}</a>
but I am unsure how to do that and preserve the {{title}} variable.
would it be the wisest to run this code in a while loop until no replacements are made?
$templateHTML = preg_replace('/(<script type="text\\/template" .*?\\><!--.*?){{(.*?--\\>\\<\\/script\\>)/s', '$1{|{$2', $templateHTML, -1, $count);