Or the equivalent of a statement using mustache.js

Ok, so I know that mustache patterns do not have logic, but how to implement this:

<?php if ($a || $b) { echo $c, $d, $e; } ?> 

... using the mustache pattern syntax? The best I can come up with is this:

 {{#a}} {{c}}{{d}}{{e}} {{/a}} {{^#a}} {{#b}} {{c}}{{d}}{{e}} {{/b}} {{/a}} 

... which is clearly disgusting and requires me to duplicate something inside the "if".

Any ideas?

+4
source share
2 answers

Mustache clearly prohibits such things. This logic, and you are trying to put it in your template :)

An appropriate way would be to move the logic into a ViewModel or View object:

 <?php class MyView { public $a; public $b; public function aOrB() { return $this->a || $this->b; } } 

But if it were me, I would call this function something like hasFoo or showBar , so it has a little meaning.

Because you are processing "should I show this block?" logic in View or ViewModel, you will return to the normal section in your template:

 {{#aOrB}} {{c}}{{d}}{{e}} {{/aOrB}} 
+5
source

For recording, this is the only way to do this with a mustache. At the moment (mustache 5, I think) there is no better solution.

0
source

Source: https://habr.com/ru/post/1401803/


All Articles