note 115309 added to function.printf
| From: | snaake | Date: | Thu, 03 Jul 2014 21:11:46 +0000 |
| Subject: | note 115309 added to function.printf | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199827@lists.php.net to get a copy of this message | ||
I made a function that wraps around printf to help me when working on codebases that don't use
a standard templating system. I found that interpolating variables and class properties gets pretty
ugly when mixing the data with presentation, so I made this as a sort of in-line template helper so
I can easily add tabs to the result html with less mess. It also really helps with looping to build
templates.
This requires PHP 5+ since it uses the ReflectionFunction class.
<?php
/**
* Works similarly to printf, but the first parameter is the number of tabs to indent the whole
block.
* This function will output directly to the browser, and terminate each call with a \n line break.
* The third parameter can be an array of values instead of one parameter for each
* printf parameter.
*
* Usage with args:
* int htmlf ( int $tabs, string $format [, mixed $arg [, mixed $... ]] )
*
* Usage with array:
* int htmlf ( int $tabs, string $format, array $args)
*/
if(!function_exists('htmlf'))
{
function htmlf()
{
$arg_list = func_get_args();
$format = '';
foreach (explode("\n",$arg_list[1]) as $line)
{
if ($line)
for ($i = 0; $i < $arg_list[0]; $i++)
$format.="\t";
$format.=$line."\n";
}
// Use reflection to make a wrapper around printf
$ref = new ReflectionFunction('printf');
// Check if the third parameter is an array, if use the array input style
if (($arg_num = func_num_args()) === 3 && is_array($arg_list[2]))
// invoke printf
$ref -> invokeArgs(array_merge(
array($format),
$arg_list[2]
));
// Run as normal argument style
else
// invoke printf
$ref -> invokeArgs(array_merge(
array($format),
array_slice($arg_list, 2, func_num_args() - 2)
));
}
}
?>
Here is an example usage:
<?php
// top part
echo
'<div class="container">
<div class="sub-container">
<div class="content">
';
// Define the template using numbered argument swapping
$template =
'<li>
<div class="thumbnail">
<div class="thumbnail-wrapper">
<a href="%1$s">
<img src="%2$s" class="img">
</a>
</div>
<div class="caption">
<a href="%1$s" class="name">%3$s</a><br>
</div>
</div>
</li>';
// start 3 tabs in
htmlf(3,'<ul>');
// Output using normal argument style
// 4 tabs in
htmlf(4, $template, "link/one.html", "image/one.jpg", "Image One");
// Output next group using array input style
$myArray = array(
"link/two.html",
"image/two.jpg",
"Image Two"
);
htmlf(4, $template, $myArray);
// close <ul> 3 tabs in
htmlf(3,'<\ul>');
// bottom part
echo
' </div>
</div>
</div>';
?>
The above will output:
<div class="container">
<div class="sub-container">
<div class="content">
<ul>
<li>
<div class="thumbnail">
<div class="thumbnail-wrapper">
<a href="link/one.html">
<img src="image/one.jpg" class="img">
</a>
</div>
<div class="caption">
<a href="link/one.html" class="name">Image One</a><br>
</div>
</div>
</li>
<li>
<div class="thumbnail">
<div class="thumbnail-wrapper">
<a href="link/two.html">
<img src="image/two.jpg" class="img">
</a>
</div>
<div class="caption">
<a href="link/two.html" class="name">Image Two</a><br>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
----
Server IP: 216.194.115.244
Probable Submitter: 107.3.191.108
----
Manual Page -- http://php.net/manual/en/function.printf.php
Edit -- https://master.php.net/note/edit/115309
Del: integrated -- https://master.php.net/note/delete/115309/integrated
Del: useless -- https://master.php.net/note/delete/115309/useless
Del: bad code -- https://master.php.net/note/delete/115309/bad+code
Del: spam -- https://master.php.net/note/delete/115309/spam
Del: non-english -- https://master.php.net/note/delete/115309/non-english
Del: in docs -- https://master.php.net/note/delete/115309/in+docs
Del: other reasons-- https://master.php.net/note/delete/115309
Reject -- https://master.php.net/note/reject/115309
Search -- https://master.php.net/manage/user-notes.php