Hacker News new | past | comments | ask | show | jobs | submit login

Yeah, and that idea was always pretty stupid. They'd create some some new, terribly ugly, syntax. Maybe at first functions were limited, nut inevitably, they'd add connectors for every function in the "host" language (php/js/...). Or just something generic, allowing you to do something like <this-is-definately-not-a-programming-language native="Kernel.exec('sudo rm -rf /'"/>.

OF course it meant you had to learn a new language, even though you were perfectly capable of perfectly suitable language already. But at least designers didn't have to learn "programming".

There's no way around this. Templates need logic, mostly for loops (or maps, same difference), or dynamic formatting.




Templates need some level of logic, yes. Putting the entirety of the business logic in your templates is not only bad, but dangerous. Because unless you're replicating your business logic in your non-template code, I can change things in the DOM, and potentially corrupt the system.

Unless I completely miss the boat about how Angular/React/Vue/etc are supposed to be used. I only know from my previous years of seeing it done in the way I mentioned above, and every time, the work always ended up duplicated in the non-template code.


What's your definition of business logic?


  > "make it easy for non-programmers to write templates"
  > I feel like that has been an illusory strawman from the start.
You're right, the nonprogramming web designer may be a rare bird. Even if they exist, they don't exist long. I have always believed that someone can pick up the rudiments of programming in a couple of weeks. Someone who starts out writing HTML and CSS will eventually rub up against JavaScript. In time they will incorporate a snippet of JavaScript here and there. First it will be mindlessly pasted from the Internet. But after reading enough blogs, they will be bundling React and adhering to the functional programming paradigm to load their own blogs. Come to think of it, I wish there were more people who knew just HTML and CSS.

More seriously, I myself know HTML, CSS, and a few programming languages, but unless the page is very simple I still like to separate things into template files. I prefer Handlebars, but often I just use PHP's alternative syntax.

  > apparently suffer from a nervous breakdown if they have to look at
  > an if-block or understand a for-loop
Well, if-blocks and for-loops are the two main pieces of logic I think every template syntax must have. Mustache said it wrong. Templates should not be "logicless." Templates should be "processingless": they should not have side effects or further manipulate the data. This is okay:

  {{#if items}}
  <ul>
      {{#each items}}
          <li><a href="{{url}}">{{name}}</a>: {{description}}</li>
      {{/each}}
  </ul>
  {{else}}
      <p>none</p>
  {{/if}}
This is less nice:

  <p>Any items you have will be listed below:</p>
  <? $q = pg_query('select * from items') ?>
  <? if (0 !== pg_num_rows($q): ?>
      <ul>
      <? while ($item = pg_fetch_assoc($q)): ?>
          <? if (userCanSeeItem($_SESSION['username'], $item['id'])): ?>
          <li>
              <a href="<?= h($item['url']) ?>"><?= h($item['name']) ?></a>:
              <?= h(ucfirst($item['description'])) ?>
          </li>
          <? endif ?>
      <? endwhile ?>
      </ul>
  <? else: ?>
      <p>none</p>
  <? endif ?>
The problem is not ifs and loops nor the use of raw PHP, with its alternative syntax. The problem is the database calls (pg_query), controller-esque function calls (userCanSeeItem) and text clean-up (ucfirst). This kind of stuff should be hidden inside a model-esque function (getItemsOfUser($username)) which returns a ready-to-print associative array.

I use templates not because I'm scared of all that. Instead it's sort of like the recently re-loved functional programming paradigm. Template files should have no side effects. I know that once I stuff the data into a template, then nothing else will happen besides output to the user's screen.


> I know that once I stuff the data into a template, then nothing else will happen besides output to the user's screen.

Exactly. It's that WTF moment of having database and HTTP queries spring from unexpected locations that you are trying to avoid.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: