K
K
Kirill Nesmeyanov2013-09-19 21:49:38
PHP
Kirill Nesmeyanov, 2013-09-19 21:49:38

Double sided templating?

Gentlemen are good, I'll ask a rather unusual question. Can you tell me if there is a template engine in the world that can be executed both on the server side (PHP or Ruby, although if there is something similar in other variations, it will also be great) and on the client side? Ideally, with the ability to restore the original version after “compilation” (but these are dreams).

If it’s not clear why, I’ll explain. We have a template on the server (we all know - smart, xsl, blade, haml, slim, jade, etc.), we process it and send the result to the client. The client receives the html code and rejoices. Now let's add some ajax. Ideally, we caught the "a" tag and the "href" link, made a request to the server - received a new template and json data to fill it. We inserted the data where necessary, showed it to the user and changed the url through the history api.

If you combine these two options, you can get the following picture: Open the page - we got statics, go through the pages - load them dynamically, adding template caching here (well, for example, in localStorage \ IndexedDb \ WebSQl, etc.) - we get an ideal situation when we have completely an asynchronous resource and at the same time neither templates nor controllers (servers) absolutely need to be adjusted for this matter. Something like this, I hope I explained it clearly.

As for “restoring after assembly”, it’s a small whim - to make sure that the first load of the static version allows the JS code to understand the template and cache it, and when interacting with it (we add a new comment, plus the rating, go through the pages - it doesn’t matter what ) - we can immediately work with existing data.

Answer the question

In order to leave comments, you need to log in

9 answer(s)
S
Sergey, 2013-09-19
@SerafimArts

Twig has a js implementation

_
_ _, 2013-11-26
@AMar4enko

The fact is that you are diligently trying to ignore the fact that the requirements came to a single page application in JS. And try to find some approach that will keep you within the current development paradigm.
And solutions have already been invented for a long time and there are a lot of them, you can choose for every taste. And all of them lie in the fact that there is no templating at all on the server - there is only data.
All interaction with the front is through the REST API.
The only thing that can cause a problem in this case is the indexing of such an application by the browser. But even here there is a solution that requires minimal intervention in the server code - you just take snapshots of the page that requires indexing, for example, using PhantomJS, and then give them as static from the server when requested from the bot.

N
Nikolai Vasilchuk, 2013-09-19
@Anonym

XSLT can be rendered on both the server and the client.
But in your case, it's easier to display everything on the client, and send only the layout of the page and data in json from the server.

K
Konstantin Kitmanov, 2013-09-19
@k12th

mustache has implementations in several languages.

E
egorinsk, 2013-09-19
@egorinsk

Transferring templates is not enough, you still have to transfer models and partly business logic to the client (unless you have a primitive 3-page website). In my opinion, you can avoid double work only by using technologies like node.js: in this case, you can make part of the code with logic shared between the client and the server.
Well, or there is another option, somehow set up code generation and generate JS models based on server code.

K
Kirill Nesmeyanov, 2013-09-19
@SerafimArts

<deleted>(missed the thread)

M
Maxim Dyachenko, 2013-09-20
@Mendel

Now we are smoking this approach:
Views using simple classes generate simple html through DOM.
This code does not contain decoration, but only content, more or less semantically designed.
For design, CSS and JS are used.
I know that everyone does this, but I mean ALL the design to take out there.
Something like this:
jsfiddle.net/x8mmC/
At the output, we have a slender html, which is very semantic, looks quite nice even without design, is understood by search engines, and is noticeably smaller in size.
From the view side, we have the usual OOP code that can be inherited, extended, encapsulated, etc. in one way or another.
If the interface is well thought out, then it will be quite enough for the layout designer not to touch the html but to manage the zhs. (see example).
All of the above, in principle, works for us, although not fully debugged.
But here's what else I want to do - by Ajax, give not a new page, but its difference in comparison with the current one.
those. some DOMdiff or in the form of jquery (as the style in the example) or in a more concise form - it does not matter.
BUT so far the hands have not reached it. First of all, it is necessary to formalize the first part, rewrite all classes to a clean copy, etc.

A
Anton, 2013-09-20
@sHinE

I haven't tried it personally, but recently I saw a link to an article where, judging by the description, they solve your problem:
www.phpied.com/server-side-react-with-php/
www.phpied.com/server-side-react-with -php-part-2/
True, as far as I understand, there is a wrapper in php for the JS engine.

M
Maxim Barulin, 2013-09-20
@Slavenin999

I use smarty + jsrender.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question