J
J
Jemeron2014-06-12 10:54:46
Ruby on Rails
Jemeron, 2014-06-12 10:54:46

Are there HTML/XML-like templating engines?

There are quite a few different view templating engines that compile when they are parsed into, say, a native view. For example something like this for php:

@if ($article->status == 'active')
<div class="content">
   {{ $article->content }}
   @foreach ($articles->comments as $comment)
       {{ $comment->content }}
   @endforeach
</div>
@endif

Only block formats change: "@if", "<% if ... %>", etc. All this for the same php is reduced to the form "<?php if ... : ?>".
So it became interesting, are there template engines with HTML / XML-like syntax? And if not, why not?
For example, write
<if article.status = 'active'>
    <div class="content">
         {{ article.content }}
         <for comment in article.comments>
            {{ comment.content }}
         </for>
    </div>
</if>

It turns out that we practically do not mix languages, we write almost on the same HTML, albeit modified. Even tag syntax highlighting will partially work in many editors. The same "for" can generally be brought to the full validity of the XML structure
<for each="comment" in="article.comments" by="comment_id">
    {{ comment.content }}
</for>

With conditional constructions, of course, it is more difficult, you can also pervert up to
<check if="article.status = 'active'">
   {{ article.status }}
</check>

With such a structure, it would be possible to complement some native tags, such as forms. Let's mark up the form like this:
<form name="article" class="form">
    <label for="title" />
    <field name="title" class="form-input" />
    <field name="type" class="form-select" />
    <submit />
</form>

And programmatically, when parsing, the necessary attributes and values ​​\u200b\u200bare added
<form name="article" class="form" method="POST" action="/article/save/">
    <input type="hidden" name="_token" value="12345" />
    <label for="title">Article title</label>
    <input name="title" class="form-input" type="text" placeholder="Enter title..." />
    <select name="type" class="form-select">
        <option value="blog">Blog</option>
        <option value="gallery">Gallery</option>
    </select>
    <input type="submit" value="Create article" />
</form>

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Alexey, 2014-06-13
@rdifb0

XSLT.

P
Pavel Solovyov, 2014-06-12
@pavel_salauyou

and then figure out where the markup is, and where the templater code is, no thanks, no need, twig is enough for me.

M
maxaon, 2014-06-12
@maxaon

It is believed that xml is not convenient for editing, so they try to move from it to a simpler syntax (yaml, json).
A "template engine" exists on the client side, called angular.js. He does the right thing - complements and expands html.

V
Vyacheslav Plisko, 2014-06-12
@AmdY

Of course I have. slaslaphptal.org/introduction.html
Initially, everyone tried to do it either through comments, or by embedding it in tags, or with their own tags, and the highlighting worked, lately everyone has come up with their own tags.

I
impass, 2014-06-13
@impass

Yate: Yandex.Mail switched to a new template engine

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question