使用 vprintf 构建便宜的 php 模板?

发布于 2024-07-19 08:12:37 字数 1317 浏览 6 评论 0 原文

好的, 所以 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");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

森末i 2024-07-26 08:12:37

如果您想要便宜的 PHP 模板,请使用带有 PHP 表达式块的单独文件。 可以使用 printf 样式的格式字符串创建模板系统,但我发现这种方法有两个主要问题:速度和可读性。 printf 函数旨在用于较短的字符串,尽管我手头没有任何统计数据,但我认为可以肯定地说运行 sprintf() 或在表示页面主体的一个大字符串上使用 vprintf() 会比仅在文件中使用 PHP 表达式块慢。

这就引出了下一个问题:可读性。 比较这两个 HTML 模板:

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

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

或者,假设我决定使用带索引参数的格式字符串。 比如说:

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

现在,如果我想改变顺序怎么办?

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

这些显然是人为的,但请考虑一下从长远来看如何维护 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. 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.

吾家有女初长成 2024-07-26 08:12:37

我通常有两个文件:

  • 某种控制器(recipes.controller.php 重写为 /recipes/123)
  • 控制器的许多视图之一(recipes.view.html)

我只需在控制器,然后在末尾包含适当的视图。 该视图可以访问控制器中的所有变量,因此我已经创建了 $title、$ingredients[] 等内容。 我不太确定为什么人们把事情弄得比这更复杂。 这很容易遵循。

视图文件基本上如下所示:

<html>
<head>
<title><?=$title ?></title>
</head>
etc...

I generally have two files:

  • A controller of some sort (recipes.controller.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...
泪痕残 2024-07-26 08:12:37

PHP 的创建者 Rasmus Lerdorf 更喜欢像这样包含他的变量:

    <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;
?>

作为参考,<<EOB; 是一个 heredoc

来源:无框架 PHP MVC 框架< /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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文