将数据放置在视图中的最佳方式 (MVC)

发布于 2024-08-17 01:40:40 字数 406 浏览 4 评论 0原文

在我正在使用的框架中,如果我想在视图中呈现数据,我会这样做:

<h1><?php echo $this->header ?></h1>

最近我发现了 xTemplate 并注意到它使用字符串占位符,因此 xTemplate 中的上述代码将是

<h1>{HEADER}</h1>

:视图还可以,而且比执行 str_replace 更快,但我最近开始与设计师合作,我认为 html 中的很多 php 标签可能会让他感到困惑。

我想知道您对这个问题的想法。

谢谢。

注意:这可能是主观的,但我真的想找到最好的方法,或者至少知道这两种方法的所有优点和缺点。

In the framework I am working with, if I want to present data in a view I do this:

<h1><?php echo $this->header ?></h1>

Recently I discovered xTemplate and noticed it uses string placeholders, so the above code in a xTemplate would be:

<h1>{HEADER}</h1>

I always thought that a little php in the view was ok and was also faster than doing a str_replace, but I have recently started working with a designer and I think lot's of php tags in the html might confuse him.

I would like to know your thoughts on this subject.

Thanks.

Note: This might be subjective but I really want to find out the best way or at least know all the pros and cons of both of these approaches.

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

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

发布评论

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

评论(2

生生漫 2024-08-24 01:40:40

PHP 被设计为一种模板语言。我认为在模板引擎之上创建模板引擎没有任何意义。

比较

 Hi, <b><?=$username?></b>

 Hi, <b>{username}</b>

第一个变体真的很复杂吗?

如果您尝试使用 Smarty,您将会非常惊讶它的复杂程度。。这是纯粹的邪恶: http://www.smarty.net/manual /en/language.function.foreach.php

 {foreach name=outer item=contact from=$contacts}
   <hr />
   {foreach key=key item=item from=$contact}
     {$key}: {$item}<br />
   {/foreach}
 {/foreach}

是不是比 PHP 的 foreach 更容易阅读?别让我笑。

邪恶的。
除了 PHP 本身之外,不要使用任何模板引擎。
为你的设计师提供足够的辅助功能,他不会抱怨。

PHP is designed as a templating language. I see no point in creating template engines above template engine.

Compare

 Hi, <b><?=$username?></b>

 Hi, <b>{username}</b>

Is the first variant really complicated?

If you try Smarty, you will be very surprized how complicated it is. It is pure EVIL: http://www.smarty.net/manual/en/language.function.foreach.php

 {foreach name=outer item=contact from=$contacts}
   <hr />
   {foreach key=key item=item from=$contact}
     {$key}: {$item}<br />
   {/foreach}
 {/foreach}

Is it simpler to read than PHP's foreachs? Don't make me laugh.

EVIL.
Don't use any template engines other than the PHP itself.
Provide enough helpers functions to your designer and he will not complain.

梦忆晨望 2024-08-24 01:40:40

有趣的问题。我不知道设计者看到 PHP 会有多困惑,但 PHP 具有以下优点:

  • 你不必学习另一种语法(模板语言)
  • PHP 足够灵活,可以做所有事情(当然你不应该做所有事情) )在模板中。

Interesting question. I don't know how confused a designer could be by seeing PHP but PHP as the following advantages:

  • You don't have to learn another syntax (template language)
  • PHP is flexible enough to do everything (although of course you should NOT do everything) in the template.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文