Skip to main content
Topic: 2.0: mustache template engine?  (Read 9208 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Re: 2.0: mustache template engine?

Reply #15

QuoteOn top of that, any error in the writing of a simple tweak results in the crash of the forum (or at least part of it, but since 99% of the edits people want to do are done in index.tempalte.php, it means full crash).
Then is it not too big for its britches?

Re: 2.0: mustache template engine?

Reply #16

I think that is more that 99% of the edits are done either in the header area or in the footer so that they are visible in any page (and both are in index.template.php).
Bugs creator.
Features destroyer.
Template killer.

Re: 2.0: mustache template engine?

Reply #17

Quote from: scripple – All the ifs, elses, and variable setting has to happen anyway.
They do, but — and I must stress that this has nothing to do with template engines — you can separate such logic from the output to a much greater degree than it presently is. This tends to increase the clarity of both logic and output.

The bulk just looks like a slight overuse of inline ternary operators. :P

Re: 2.0: mustache template engine?

Reply #18

A question I just thought of while working on an addon for a client: How would template layers work in Twig et al? Genuinely curious, as I don't know how to work with any template engine other than PHP[1].
It started out as one, anyway.
LiveGallery - Simple gallery addon for ElkArte

Re: 2.0: mustache template engine?

Reply #19

Quote from: emanuele – I thought about it for a while myself, but never managed to even start reading twig documentation. lol
:P
Bugs creator.
Features destroyer.
Template killer.

Re: 2.0: mustache template engine?

Reply #20

depends on where you want it integrated..
it could be injected in the TemplateLayers.class.php (haven't looked at how, tbh) or you could use the existing template functions and replace the code with a template loader:

Code: [Select]
function template_mail_queue()
{

global $mustache, $context, $txt:
   
$tpl_data = array(
'context' => $context,
'txt' => $txt,
);
$tpl = $mustache->loadTemplate('manage_mail'); // loads __DIR__.'/views/manage_mail.html';
    echo $tpl->render($tpl_data); //renders the tpl_data array

template_show_list('mail_queue');
}
That would (in theory) allow us to create different themes with different engines. And funnily without touching the core files at all,

for example one Theme with pure PHP, HTML, CSS (what we currently use) ,..
another one with mustache as template engine ..
and a third one with twig..
Thorsten "TE" Eurich
------------------------

Re: 2.0: mustache template engine?

Reply #21

I'm not sure that quite answers the question though? Those "layers" seem more like dynamically generated {{% BLOCKS }} or something (to keep it in mustache.php lingo).

Re: 2.0: mustache template engine?

Reply #22

Please explain to me exactly what advantage this brings.  Syntax substitution is all I've seen really.

Re: 2.0: mustache template engine?

Reply #23

Please, read the topic from the beginning and I made at least a couple of arguments that are not at all about syntax substitution.
Bugs creator.
Features destroyer.
Template killer.

Re: 2.0: mustache template engine?

Reply #24

Quote from: TE – IMO there are some big benefits for designers (and developers):
- easy readable code (almost simple html files)
Syntax.  It really doesn't look any easier to me.  As I've said you can rearrange the php to make it prettier if you want.

Quote from: TE – - reduced syntax errors while developing code (PHP is sometimes complex, especially if the templates are using nested ternary operators)
Syntax.  And one more syntax / system to learn.

Quote from: TE – - syntax highlightning in editors (makes it easier to find a syntax error)
Syntax.

Quote from: TE – - reusable code (mustache templates can be rendered via PHP or Javascript)
So you have to load mustache into the browser so it can do the work?

Quote from: emanuele – 1) we stop having at least one potential security risk (I know is just a part of the big picture, because the package manager is not different but at least is one less) not having to write directly to php files on the server,
Not really seeing this one, especially since you'll still be writing to all the ones in say the source directory.  Plus what security risks will whatever template engine whose files you're updating have?

Quote from: emanuele – 2) we would give end-users a much more user-friendly way of dealing with template edits (i.e. no need to explain that to add a google analytics script they have to either learn a bit of php or install an addon that may or may not work because "someone" forgot to update it xD).
Not seeing this at all.  It's just a different syntax to learn.  Probably on top of php if you're adding anything new feature wise as there will be php code required to do that.

As to the crash the forum, at least that gets the bug fixed.  I don't know how the various template engines will actually respond if you have a, you know, syntax error.  Plus you've still got all the conditionals which will probably be in php anyway and thus just as prone to crashes.  Really all I see is changing syntax and taking on the hassle of some third party package and whatever support it does or doesn't have and whatever limitations and bugs it does have.

Re: 2.0: mustache template engine?

Reply #25

Quote from: scripple – So you have to load mustache into the browser so it can do the work?
I actually hadn't thought of turning this pro-template argument right on its head. PHP supposedly lures you into using more features than those strictly needed for templating. By contrast, Mustache easily enables doing much worse! :P

But the answer to the question is no. I wouldn't use terms like "mostly harmless" for Mustache if that were the case.

The funny thing is that Mustache doesn't do what it advertises at all. Like I said, they're not "logic-less" (hah!): they're logic-unclear. It's still spaghetti code. For proper logic-less templates, which are highly superior to Mustache's "look how cleverly we're trying to hide the logic" attitude, you need a template engine like I already linked to above.

https://github.com/tropotek/tk-domtemplate
https://code.google.com/archive/p/querytemplates/
https://github.com/punkave/phpQuery

Those actually deliver what Mustache promises. Unlike Mustache.

tl;dr Smarty doesn't pretend to be anything it's not and is therefore infinitely superior to "Strawman" Twig and "Logic Soup Spaghetti Code" Mustache. However, that which Mustache argues actually does make sense. It just plain sucks at it.

PS The actual logic-less approach may not meet the "friendly on the user" requirement. But it'll properly reduce maintenance nightmares.

QuoteI don't know how the various template engines will actually respond if you have a, you know, syntax error.
They will compile down to valid PHP and silently output different HTML than you're expecting. There's no such thing as a syntax error really. It's either something special or just regular old output. But you can easily make the same kind of mistakes in PHP.

Re: 2.0: mustache template engine?

Reply #26

Quote from: Frenzie – I actually hadn't thought of turning this pro-template argument right on its head. PHP supposedly lures you into using more features than those strictly needed for templating. By contrast, Mustache easily enables doing much worse! :P

[…]

However, that which Mustache argues actually does make sense. It just plain sucks at it.
I'd like to expand on this. Mustache doesn't enforce separation between controller and view at all. It still very easily allows for things to be conditionally displayed in precisely the wrong way. What's necessary is this kind of thing which Elk already (largely) does.

Code: [Select]
		// Set some permission related settings.
if ($user_info['is_guest'] && !empty($modSettings['enableVBStyleLogin']))
{
$context['show_login_bar'] = true;
$context['theme_header_callbacks'][] = 'login_bar';
loadJavascriptFile('sha256.js', ['defer' => true]);
}

You don't stick if ($user_info['is_guest'] && !empty($modSettings['enableVBStyleLogin'])) in the view but in the controller. The view just receives a presentational $show_login_bar.

This is what's necessary, and as demonstrated by Elk right here you can do it perfectly fine in plain PHP. Mustache merely fakes being logic-less through sly obfuscating syntax without taking away the option to mix controller logic and presentational logic. I don't think taking away the option is necessary because it's completely besides the point. What you need is conscious design decisions to do the right thing and plain PHP suffices for that. By contrast, I find the fallacies and fakery in Twig and Mustache offensive at best.

I did some searching in the meantime and I came across a few other interesting examples:


Via http://www.workingsoftware.com.au/page/Your_templating_engine_sucks_and_everything_you_have_ever_written_is_spaghetti_code_yes_you (I wish I'd known about this blogpost prior to ten minutes ago.)
Last Edit: November 15, 2017, 06:39:00 am by Frenzie

 

Re: 2.0: mustache template engine?

Reply #27

I like twig. I did not use mustache before.
phpBB 3.1 uses twig.
Sorry for my English

Re: 2.0: mustache template engine?

Reply #28

Apart from the template engine, that could be whatever, I think we can agree on two things:
1) lots of logic should disappear from the templates (pretty much like queries disappeared from the controllers),
2) admins that are not insterested in learning anything deserves something that doesn't crash their forums incase of mistakes.

These are tge two current problems.

1 can be addresses withot a template engine (and actually I think it would be better to solve it before thinkinf about anything else).
2 would definitely require an approach similar to that of a template engine, but is nor an immediate concern.

Based on that, I think the first thing to do is solve 1, with agreed c9nventions and conding styles, something like do not use any other variable apart $context in the template files.
Bugs creator.
Features destroyer.
Template killer.

Re: 2.0: mustache template engine?

Reply #29

Sounds like a good plan.  After one is handled you can better decide how to handle two if it's even still much of a problem.