views:

141

answers:

3

Ok, so printf/sprint/vprintf all accept a certain type specifier syntax %[num][type]. (http://us2.php.net/sprintf see examples 3 and 4) Where num is the index to the type.

Example: vprintf('Number %1$d string %2$s. String %2$s, number %1$d',array(1,"no"));

Yes, it is limited... And you would need to maintain the indexes. But it's native to the language and (i think) fast.

I just want some thoughts on how useful this would be as say a second stage to something like this: http://www.techfounder.net/2008/11/18/oo-php-templating/.

(and if anyone knows about printf/vprintf's speed that would be appreciated)

full example of what i'm talking about:

frontpage.php:

<html>

<head>

<title> %1$s </title>

</head>

<body>

Hello %2$s! You have reached page: %1$s!

</body>

</html>

whatever.php:

ob_start();

include frontpage.php;

$ob_output = ob_get_clean();

vprintf($ob_output,"Page Title","Bob");

+5  A: 

If you want cheap PHP templating, use separate files with PHP expression blocks. It is possible to make a templating system using printf-style format strings, but there are two main problems I can see with this approach: speed and readability. The printf functions are intended for use on shorter strings, and although I don't have any statistics on hand, I think it's safe to say that running a sprintf() or a vprintf() on one huge string representing the page body will be slower than just using PHP expression blocks in a file.

That leads into the next issue: readability. Compare these two HTML templates:

<html>
<head>
   <title>%s</title>
</head>
<body>
<div id="main">
    <h1>%s</h1>
    <p>%s</p>
</div>
<div id="other">
    <p>%s</p>
</div>
<p id="footer">
    %s. Took %.2f seconds to generate.
</p>
</body>
</html>

and

<html>
<head>
   <title><?= $title ?></title>
</head>
<body>
<div id="main">
    <h1><?= $header ?></h1>
    <p><?= $body_text ?></p>
</div>
<div id="other">
    <p><?= $misc_info ?></p>
</div>
<p id="footer">
    <?= $copyright ?>. Took <?= $load_time ?> seconds to generate.
</p>
</body>
</html>

Or, let's say I had decided to use format strings with indexed arguments. Say, something like this:

<h1>%1$s</h1>
<p>%2$s</p>
<span id="blah">%3$s</p>
<p>%4$s</p>
<p>%5$s</p>

Now, what if I wanted to switch the ordering around?

<h1>%1$s</h1>
<p>%3$s</p>
<span id="blah">%5$s</p>
<p>%4$s</p>
<p>%2$s</p>

These are obviously contrived, but think about how it would be to maintain the printf templates in the long run.

So, in general, if you want quick-and-dirty PHP templating, use template files that contain PHP expression blocks. The printf functions are a lot better at tackling smaller string formatting tasks.

htw
http://ca3.php.net/sprintfsee example 4. It's in the doc
(and example 3)
I agree with you even though not all of your facts are straight
Joe Philllips
PHP is a templating language, might as well use it as one as htw suggests.
Steve Claridge
@synthos: Sorry, I didn't realize PHP had that. Fixed.
htw
yeah, all I really want to know is what kind of performance knock you would take if you did vprintf on a large string.
+1  A: 

I generally have two files:

  • A controller of some sort (recipes.view.php rewritten to /recipes/123)
  • One of many views for a controller (recipes.view.html)

I simply do all of the logic/database work within the controller and then include the appropriate view at the end. The view has access to all of the variables in the controller so I already have things like $title, $ingredients[], etc. created. I'm not really sure why people make it any more complicated than that. It's very easy to follow.

The view file will basically just look like this:

<html>
<head>
<title><?=$title ?></title>
</head>
etc...
Joe Philllips
+1  A: 

Rasmus Lerdorf, creator of PHP, prefers to include his variables something like this:

    <select class="f" name="cat" id="f_cat" size="1">
      <option selected>Category</option>
<?php foreach($categories as $cat) echo <<<EOB
      <option value="{$cat}">{$cat}</option>

EOB;
?>

For reference, <<<EOB through EOB; is a heredoc.

Source: The no-framework PHP MVC Framework by Rasmus Lerdorf

R. Bemrose
Can't say I'm a huge fan of the heredoc.
Joe Philllips