Something in PHP, I want to display content if a certain variable exists.
For example, For the current comment I want to check if the author has already voted on this reply, so the code will be something as the following:
<?php if (isset($replies)): ?>
<?php foreach ($replies as $reply): ?>
<?php if (isset($already_voted)): ?>
<?php foreach ($already_voted as $vote): ?>
// if vote['id'] == $reply['id'], echo what they have already voted
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
What would be an easier way of doing this, without using a bloated templating engine like smarty?
(Please, no comments about… yes ‘PHP is essentially a templating engine’ or anything along those lines).
I would recommend using a templating engine for templates (I recommend PHPTAL), but you have asked very specifically for a method to not use an engine. You can simplify the php code you have a bit just by not breaking out of it so much:
Of course, this is practically the same thing as what you have, it just uses fewer characters. Keep track of open code blocks via indentation.
Note that the point of a template, at least according to my understanding, is reuse. You want to be able to use the same template, or at least portions of a template (macros for example) across as many pages as possible. For some static content, this may not always be a possibility, but that is why the key words are as possible. As you should do with any code, reuse what you can.
Using a template system makes it easier to separate view from logic. You can create php templates (as you have done), but I think it lacks clarity and it is far easier to do processing within the template rather than before. It’s also more difficult to create reusable templates without any sort of system in php [citation needed]. You can create your own that works for you, but there are already some great ones out there.