好的,
所以 printf/sprint/vprintf 都接受某种类型说明符语法 %[num][type]。 (https://www.php.net/sprintf 请参阅示例 3 和 4)其中 num 是类型的索引。
例子:
vprintf('编号 %1$d 字符串 %2$s。字符串 %2$s,编号 %1$d',array(1,"否"));
是的,它是有限的......并且您需要维护索引。 但它是该语言的原生语言,而且(我认为)速度很快。
我只是想知道这有多么有用,就像这样的第二阶段一样: http://www.techFounder.net/2008/11/18/oo-php-templatating/。
(如果有人知道 printf/vprintf 的速度,我将不胜感激)
我正在谈论的完整示例:
frontpage.php:
<代码><标题> %1$s
<正文>
你好%2$s! 您已到达页面:%1$s!
whatever.php:
ob_start();
包含 frontpage.php;
$ob_output = ob_get_clean();
vprintf($ob_output,"页面标题","鲍勃");
Ok,
so printf/sprint/vprintf all accept a certain type specifier syntax %[num][type]. (https://www.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");
发布评论
评论(3)
如果您想要便宜的 PHP 模板,请使用带有 PHP 表达式块的单独文件。 可以使用 printf 样式的格式字符串创建模板系统,但我发现这种方法有两个主要问题:速度和可读性。 printf 函数旨在用于较短的字符串,尽管我手头没有任何统计数据,但我认为可以肯定地说运行 sprintf() 或在表示页面主体的一个大字符串上使用
vprintf()
会比仅在文件中使用 PHP 表达式块慢。这就引出了下一个问题:可读性。 比较这两个 HTML 模板:
和
或者,假设我决定使用带索引参数的格式字符串。 比如说:
现在,如果我想改变顺序怎么办?
这些显然是人为的,但请考虑一下从长远来看如何维护
printf
模板。因此,一般来说,如果您想要快速而简单的 PHP 模板,请使用包含 PHP 表达式块的模板文件。
printf
函数在处理较小的字符串格式化任务方面要好得多。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. Theprintf
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 asprintf()
or avprintf()
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:
and
Or, let's say I had decided to use format strings with indexed arguments. Say, something like this:
Now, what if I wanted to switch the ordering around?
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.我通常有两个文件:
我只需在控制器,然后在末尾包含适当的视图。 该视图可以访问控制器中的所有变量,因此我已经创建了 $title、$ingredients[] 等内容。 我不太确定为什么人们把事情弄得比这更复杂。 这很容易遵循。
视图文件基本上如下所示:
I generally have two files:
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:
PHP 的创建者 Rasmus Lerdorf 更喜欢像这样包含他的变量:
作为参考,
<< 到
EOB;
是一个 heredoc。来源:无框架 PHP MVC 框架< /a> 作者:拉斯穆斯·勒多夫
Rasmus Lerdorf, creator of PHP, prefers to include his variables something like this:
For reference,
<<<EOB
throughEOB;
is a heredoc.Source: The no-framework PHP MVC Framework by Rasmus Lerdorf