4 ms·
> "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
by combatentropy 10y ago
> "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.
- paulddraper 10y ago> 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.