Rails 部分和模板的 CodeIgniter 或 PHP 等效项
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我可能违反了一些 MVC 规则,但我总是只是将我的“片段”放置在各个视图中,并从需要它们的其他视图中以 CodeIgniter 样式加载它们。 几乎我所有的视图都分别在顶部和底部加载页眉和页脚视图:
然后页眉可以以相同的方式包含导航栏等。
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:
The header could then include a NavBar in the same fashion, etc.
这基本上就是我使用的:
这允许您调用类似的内容:
并且在 /path/to/person.phtml 中有:
这是正在发生的一些魔法,尽管这可能会帮助您更好地了解正在发生的事情。 完整文件: view.class.php
this is essentially what I use:
this allows you to call something like:
and in /path/to/person.phtml have:
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
在 PHP 中,您可以使用
include
In PHP you'd use
include
我发现自己也从 Rails 转向 CI,我对部分所做的基本上是将视图中的部分渲染为变量并从控制器设置它。
所以在视图中你会有类似 (_partial.php) 的东西:
你可以从控制器中设置它,例如: 就
我个人而言,我更喜欢使用 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):
And you can set it from the controller like:
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?
我最近发布的模板库就是这样工作的。
该模板库也与我的 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.
不幸的是,这根本不是 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.
CodeIgniter 和 Smarty 配合得很好。
CodeIgniter and Smarty play nicely together.
这是一篇博客文章,描述如何将 smarty 与 codeigniter 结合使用< /a>. 还实现了类似 asp.net 的母版页。
Here is a blog post describing how to use smarty with codeigniter. The asp.net like masterpage is also implemented.
尝试 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.
你可以检查一下,部分和模板
You can check it, for partials and template
Symfony 通过他们的 partial/component 来做到这一点 设置。
Symfony does this with their partial/component setup.