mintyphp / core

MIT License
6 stars 5 forks source link

Looks good, but no conditions without functions? #3

Open Norcoen opened 5 years ago

Norcoen commented 5 years ago

Just found this little gem from this article: https://tqdev.com/2019-php-templating-engine-in-165-lines

So far I really like it but as far as I can see it is not possible to have conditions in the template without calling a dedicated functions which must be provided to the sandbox.

This seems to be impossible: {{if:SOME_VARIABLE!="sometext")}} Some output {{endif}}

That makes it unpleasant to work with, because it is basic functionality you are probably used to from other engines like FatFreeFramework

mevdschee commented 5 years ago

as far as I can see it is not possible to have conditions in the template without calling a dedicated functions which must be provided to the sandbox

Correct, but this is not hard. You specify the following functions:

[
  'eq' => function ($a, $b) {return $a == $b;},
  'neq' => function ($a, $b) {return $a != $b;}
]

And then you can write:

{{if:SOME_VARIABLE|eq("sometext")}} Some output {{endif}}

This is how we use it ourselves:

https://github.com/Usecue/InvoiceLion/blob/master/lib/InvoiceTemplate.php#L40

I hope this helps.

Norcoen commented 5 years ago

Yeah..., No... What I meant was, that I know how to use it (eq, neq, lt, gt, lte, gte functions for comparison)

But it is such a basic functionality, that you want to use it without explicitly passing the needed functions every time. Right now I just integrated them into the template class with a third (and experimental fourth) parameter

$if = function($a, $b, $op='==', $mod='') {
  $ret = false;
  switch($op) {
    case '==':
      $ret = ($a == $b);
      break;
    case '===':
      $ret ($a === $b);
      break;
    case '!=':
      $ret = ($a != $b);
      break;
    case '!==':
      $ret = ($a !== $b);
      break;
    case '>':
      $ret = ($a > $b);
      break;
    case '<':
      $ret = ($a < $b);
      break;
    case '>=':
       $ret = ($a >= $b);
       break;
    case '<=':
       $ret = ($a <= $b);
       break;
  }
  if($mod == '!') {
    $ret = !$ret;
  }
  return $ret;
};

Right now I am experimenting using this for .htaccess file generation based on environmental variables - so I just searched for a small, self-sustained class capable of inserting variables based on some conditions or even doing loops.

Regarding your example

Are you trying to filter out Githubissues.

  • Githubissues is a development platform for aggregating issues.