brendanmetzger / diatom

A template framework
1 stars 0 forks source link

add a 'when' construct to templates that act as a condition. #68

Open brendanmetzger opened 3 years ago

brendanmetzger commented 3 years ago

This needs some thought, as the facilities are already available with controller manipulation. Currently one can easily enough do something like:

// within a controller
protect function GETexample(\model\user $user) {
   $this->request->user = $user;
   $template = $user instanceof \model\admin ? 'admin.html' : 'general.html';
   $this->yield('dashboard', $template);
}

While this is fine, it does require creating multiple templates that could perhaps have a great deal of overlap and get confusing to orchestrate over time. In a more applicable example, there may be data that is not sensitive to look at, but is sensitive to who can take action, which makes creating new templates (or directly manipulating the DOM) kinda clunky. The consideration is something of the following service being added to templates similar to iterations, such that:


<form>
  <!-- when user.is.admin -->
  <button>submit</button>
</form>

The benefit in this particular example is somewhat small (fwiw, the form can still be submitted by a cunning user, but it should fail with unauthorized when it reaches the controller which would be the arbiter of who can submit), but the general condition approach could be useful in all sorts of ways, notable cleaning up complicated templates:


<!-- when something -->
<article>

  <h1>
         This data explains the following content, but if it doesn't
          exist, it's useless. The nodes articulating the iteration cleans 
          up fine, but it's hard to keep surrounding info in check.
   </h1>

  <!--iterate nothing-->
  <section>
     ... iterating 0 times will clean up inclosing sections.
  </section>
</article>
brendanmetzger commented 2 years ago

This seems pretty easy as I think about it. It's basically the iterator modified to not apply a data set but just embed the 'iterated' sibling node 0 or 1 times.