0

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).

5
  • 4
    PHP is essentially...ah you beat me! Commented May 13, 2011 at 13:59
  • 3
    What's wrong with the your current setup? It's exactly what I would recommend doing. Commented May 13, 2011 at 14:00
  • It can get confusing, when there are loads of them, show this when the user is logged in, show this when they sign in for the first time... etc Commented May 13, 2011 at 14:02
  • Would this really be a task for Smarty as well? I thought Smarty was used to provide a template for layout, not application logic. Commented May 13, 2011 at 14:03
  • Would there be an easier way to template with PHP without using a template engine? Answer: no. Commented May 13, 2011 at 14:25

3 Answers 3

2

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:

<?php
if (isset($replies)) {
   foreach ($replies as $reply) {

      if (isset($already_voted)) {
         foreach ($already_voted as $vote) {

    // if vote['id'] == $reply['id'], echo what they have already voted

         }
      }

   }
}

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.

Sign up to request clarification or add additional context in comments.

Comments

1

PHPLib (AFAIK GNU license open source) has a template class which is a single include file and is extremely lightweight and easy to use:

http://www.sanisoft.com/phplib/manual/TemplateExamples.php

Comments

0

As you point out, PHP is a templating engine in its own right, and what you're seeking to do can be done with a simple if statement.

Or, your can create a function to that effect, as is done for WordPress template tags for instance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.