Are there template engines that can be used both on the backend (PHP) and frontend (JS) side? In particular, I am interested in a solution that allows the use of a single syntax in a template and implements three main operations:

  1. Variable insertion
  2. Condition
  3. Cycle

The application is to implement widgets that can be rendered on the backend:

<ul> <?php foreach($todos as $todo): ?> <!-- Дублирующийся шаблон --> <li><?= $todo->content ?></li> <?php endforeach; ?> <li> <input type="button" value="add"/> </li> </ul> 

with the addition of logic at the frontend level and subsequent state change by applying the same templates:

 function TodoListView(ul){ var $ul = $(ul), // Дублирующийся шаблон liTemplate = tempalte('<li>{todo.content}</li>'); $ul.find('input:button').on('click', function(){ $ul.prepend(liTemplate({ content: 'FooBar' })); }); } 
  • Twig, pug, handlebars, ... thousands of them. But it's better to render all the templates in one place - Dmitriy Simushev
  • Twig can work on JS? Pug and handlebars can work in PHP? It is clear that it is better not to duplicate the logic, but legacy and tasks are such that you need to somehow solve the problem with double templating. - Artur-Mamedbekov
  • There are Twig ports on js, as well as Pug and Handlebars ports for PHP. If you want a very, very large cross-platform, you can try mustache - it is implemented in a very large number of languages. - Dmitriy Simushev
  • Doesn’t the syntax differ, for example {{$ var}} and {{var}}? - Artur-Mamedbekov
  • No, one-to-one syntax. - Dmitriy Simushev

0