1

I'm trying to figure out what is the best way to clone/template HTML that is frequently repeated in my web app. For example, I have a voting <form> (see below) that needs to be located on several pages.

Two ways I thought of doing this:

  1. a function call, e.g., voteForm($action, $user_id, $formType, $success);
  2. an include statement, e.g., include '/myApp/views/voteForm.php'

I prefer an include statement b/c:

  1. Then I don't have to decide on the function's parameters which may change over time forcing me to rewrite the function calls everywhere they exists in my app. With the include statement, I can just use the variables as they are wherever I put the included php file (avoiding redeclaring them which is a pain b/c there are often lots of variables).
  2. I can write the HTML in HTML and not as a PHP string where I have to deal with escaping characters/json_encode issues.

Should I reconsider using include instead function() for any reasons (e.g., performance)? Are there are other templating solutions I'm not thinking of?

    <form action="<?=$action?>" method='post' data-form-data='{'formType': '<?=$formType?>', 'success': '<?=$success?>'} >`
    <input type='hidden' value='<?=$user_id?>' name='user_id'>
    <input type='radio' value='1' name='vote'>
    <input type='radio' value='-1' name='vote'>
    </form>

3 Answers 3

1

It's really up to you--you could have a hybrid of a function that calls include for you (setting up any necessary variables that the include file may need for display purposes). e.g.

function createForm($action,$foo,$bar){
  $form_action = $action;
  $form_foo = $foo;
  include('templates/form.inc');
}

As far as performance, there's no huge benefit that I'm aware of. Although If you're looking for a better way for templating, you may want to look at smarty or some other system that handles most of the 'tough work' for you.

Just keep in mind that when you have code outputting HTML you no longer have a separation of concerns. That is to say that if you decide to change the look and feel of the site at a later date you're not looking through just .inc (or whatever extension you've used) files, but now both .inc and .php files to apply changes.

0

I would try to avoid putting HTML into function calls. I think what your trying to achieve here would be best suited to an include statement - based on personal preference.

As for performance - it's hard to tell but you could use a PHP Profiler like XDebug to see whether a function or include is the most efficient.

http://xdebug.org/docs/profiler

2
  • What is your reasoning for saying one should "avoid putting HTML into function calls"? Nov 5, 2012 at 14:05
  • 1
    I think it's personal preference - I think the HTML should be kept in the main page / view itself rather than clutter up your functions. Nov 5, 2012 at 14:06
0

For big blocks of generated HTML, I'd recommend using includes. I prefer to use function calls to get specific bits of data back.

The again, this is personal preference and cannot be answered with a truly 'correct' answer.

In terms of performance, I would guess not much difference at all unless you're making thousands of calls on each one in one go.

Hope that helps.

2
  • by "thousands of calls on each" you mean thousands of function() calls or thousands of includes? And do you know which one is more performant? Nov 5, 2012 at 14:06
  • 2
    I haven't done any tests, but in reality, it's not really worth testing. The difference would be so extremely small. Like a fraction of a second; possibly not even that. Build it first and THEN work to optimize if it's causing you problems. It's usually a bad idea to optimize before your app is even built. Nov 5, 2012 at 14:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.