Rails 部分和模板的 CodeIgniter 或 PHP 等效项

发布于 2024-07-16 19:05:06 字数 707 浏览 10 评论 0原文

在 CodeIgniter 或核心 PHP 中; 有没有相当于 Rails 的视图部分和模板的东西?

部分可以让我在视图中渲染另一个视图片段。 我可以有一个通用的 navbar.php 视图,我可以将其指向我的 homepage.php 视图内部。 模板将在一个位置定义 HTML 页面的整体外壳,并让每个视图只填充正文。

我在 CodeIgniter 文档中能找到的最接近的内容是 加载多个视图,其中多个视图在控制器中按顺序呈现。 在控制器内指定页面的视觉外观似乎很奇怪。 (即要移动导航栏,我的设计师必须编辑控制器)。

我一直在 stackoverflow 上搜索 PHP 方法来完成此任务。 我找到了 此页面,其中讨论了模拟带有 ob_start 的部分。 这是 CodeIgniter 中推荐的方法吗?

In CodeIgniter, or core PHP; is there an equivalent of Rails's view partials and templates?

A partial would let me render another view fragment inside my view. I could have a common navbar.php view that I could point to the inside my homepage.php view. Templates would define the overall shell of an HTML page in one place, and let each view just fill in the body.

The closest thing I could find in the CodeIgniter documentation was Loading multiple views, where several views are rendered sequentially in the controller. It seems strange to be dictating the visual look of my page inside the controller. (i.e. to move the navbar my designer would have to edit the controller).

I've been searching on stackoverflow for a PHP way to accomplish this. I have found this page, which talks about simulating partials with ob_start. Is that the recommended approach inside CodeIgniter?

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

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

发布评论

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

评论(11

送你一个梦 2024-07-23 19:05:06

我可能违反了一些 MVC 规则,但我总是只是将我的“片段”放置在各个视图中,并从需要它们的其他视图中以 CodeIgniter 样式加载它们。 几乎我所有的视图都分别在顶部和底部加载页眉和页脚视图:

<? $this->load->view( "header" ); ?>
//Page content...
<? $this->load->view( "footer" ); ?>

然后页眉可以以相同的方式包含导航栏等。

I may be breaking some MVC rule, but I've always just placed my "fragments" in individual views and load them, CodeIgniter style, from within the other views that need them. Pretty much all of my views load a header and footer view at the top and bottom, respectively:

<? $this->load->view( "header" ); ?>
//Page content...
<? $this->load->view( "footer" ); ?>

The header could then include a NavBar in the same fashion, etc.

风为裳 2024-07-23 19:05:06

这基本上就是我使用的:

function render_partial($file, $data = false, $locals = array()) {
    $contents = '';

    foreach($locals AS $key => $value) {
        ${$key} = $value;
    }

    ${$name . '_counter'} = 0;
    foreach($data AS $object) {
        ${$name} = $object;

        ob_start();
        include $file;
        $contents .= ob_get_contents();
        ob_end_clean();

        ${$name . '_counter'}++;
    }

    return $contents;
}

这允许您调用类似的内容:

render_partial('/path/to/person.phtml', array('dennis', 'dee', 'mac', 'charlie'), array('say_hello' => true));

并且在 /path/to/person.phtml 中有:

<?= if($say_hello) { "Hello, " } ?><?= $person ?> (<?= $person_counter ?>)

这是正在发生的一些魔法,尽管这可能会帮助您更好地了解正在发生的事情。 完整文件: view.class.php

this is essentially what I use:

function render_partial($file, $data = false, $locals = array()) {
    $contents = '';

    foreach($locals AS $key => $value) {
        ${$key} = $value;
    }

    ${$name . '_counter'} = 0;
    foreach($data AS $object) {
        ${$name} = $object;

        ob_start();
        include $file;
        $contents .= ob_get_contents();
        ob_end_clean();

        ${$name . '_counter'}++;
    }

    return $contents;
}

this allows you to call something like:

render_partial('/path/to/person.phtml', array('dennis', 'dee', 'mac', 'charlie'), array('say_hello' => true));

and in /path/to/person.phtml have:

<?= if($say_hello) { "Hello, " } ?><?= $person ?> (<?= $person_counter ?>)

this is some magic going on though that may help you get a better picture of what's going on. full file: view.class.php

信愁 2024-07-23 19:05:06

在 PHP 中,您可以使用 include

In PHP you'd use include

荒路情人 2024-07-23 19:05:06

我发现自己也从 Rails 转向 CI,我对部分所做的基本上是将视图中的部分渲染为变量并从控制器设置它。

所以在视图中你会有类似 (_partial.php) 的东西:

<h2>Here Comes The Partials</h2>
<?= $some_partials ?>

你可以从控制器中设置它,例如: 就

$this->load->view('the_view', 
   array('some_partials', 
         $this->load->view('_partial', array(), TRUE)
   )
);

我个人而言,我更喜欢使用 CI 的视图而不是 ob_start,但这就是我 =)
PS:加载视图时,第一个参数是视图名称,第二个参数是要传递给视图的参数,第三个是“ECHO”标志,它基本上告诉 CI 是直接渲染它还是返回视图的值view ,这基本上就是我在示例中所做的。

我认为这不是一个好的解决方案,但它对我有用。 有人有更好的解决方案吗?

I found myself moving from Rails to CI too, and what I did with partials is basically render the partials in the view as a variable and set it from the controller.

So in the view you would have something like (_partial.php):

<h2>Here Comes The Partials</h2>
<?= $some_partials ?>

And you can set it from the controller like:

$this->load->view('the_view', 
   array('some_partials', 
         $this->load->view('_partial', array(), TRUE)
   )
);

Personally, I prefer to use CI's view instead of ob_start, but that's me =)
PS: When loading views, first argument is the view name, second one is the parameters to be passed to the view, and the third one is "ECHO" flag, which basically tells CI whether to render it directly or return the value of the view instead, which is basically what I did in the example.

I don't think it's a good solution though, but it works for me. Anyone has better solutions?

做个ˇ局外人 2024-07-23 19:05:06

我最近发布的模板库就是这样工作的。

该模板库也与我的 Dwoo 实施 这将使您的观点更有力量。

My recently released Template library works in this way.

That template library also plays nicely with my Dwoo implementation which will give your views much more power.

孤城病女 2024-07-23 19:05:06

不幸的是,这根本不是 CodeIgniter 特有的,但是我建议您查看 Savant3 模板系统。 它允许您将模板呈现为字符串。 然后你可以简单地将它粘贴到你想要的任何地方。

也许 CodeIgniter 中有类似的东西?

我可以想出一种方法,在渲染视图时添加一个工具,渲染模板,然后渲染它包含的所有子模板。

This is unfortunately not CodeIgniter specific at all, however I suggest you to have a look at Savant3 template system. It allows you to render the template to a string. Then you can simply stuck it to wherever you wish.

Maybe there's something like that in CodeIgniter?

I can think of a way to add a facility when rendering the view, to render the template, and then all the sub-template it contains.

笑饮青盏花 2024-07-23 19:05:06

CodeIgniter 和 Smarty 配合得很好。

CodeIgniter and Smarty play nicely together.

素染倾城色 2024-07-23 19:05:06

Here is a blog post describing how to use smarty with codeigniter. The asp.net like masterpage is also implemented.

沧桑㈠ 2024-07-23 19:05:06

尝试 Oular - http://codeigniter.com/wiki/Oulous_Layout_Library/ - 它的灵感来自于 Rails模板库。

Try out Ocular - http://codeigniter.com/wiki/Ocular_Layout_Library/ - its a rails inspired templating library.

翻身的咸鱼 2024-07-23 19:05:06

你可以检查一下,部分和模板

    http://code.google.com/p/ocular/wiki/Introduction

You can check it, for partials and template

    http://code.google.com/p/ocular/wiki/Introduction
小伙你站住 2024-07-23 19:05:06

Symfony 通过他们的 partial/component 来做到这一点 设置。

Symfony does this with their partial/component setup.

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