Smarty template in javascript - pfaciana/latte-js GitHub Wiki

How to use a Smarty template in Javascript

Suppose you have a PHP Smarty application and you want to move some part of it on the client side, fetching data with Ajax and showing them "at run time" in client's browser.

Use Smarty {fetch} function to get the contents of a template without parsing and put them on a page inside a

<script type="text/x-latte-tmpl">

with unique id.

<script type="text/x-latte-tmpl" id='myTemplate'>
    {fetch file="path/to/templates/myTemplate.tpl"}
</script>

Now you can get the template's content through document.getElementById('myTemplate').innerHTML;

var myTpl = new Latte( document.getElementById('myTemplate').innerHTML );

var res = tpl.fetch( {} );

document.getElementById('result').innerHTML = res;
⚠️ **GitHub.com Fallback** ⚠️