Smarty + CodeIgniter - 使用模型的正确方法 [英] Smarty + CodeIgniter - proper way to use a model

查看:186
本文介绍了Smarty + CodeIgniter - 使用模型的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先:我意识到在视图中使用模型是违反MVC层次结构 - 但这是我发现到目前为止最平滑的解决方案。



我已将Smarty集成到我的CodeIgniter CMS应用程序中。它的一个功能是模板的使用以及缩减静态内容,如CSS和JavaScript。因此,我想在模板文件中做这样的事情:

  //添加静态内容
<?php
$ this-> content-> css(array('my.css','style.css'));
?>

< html>
< head>
//显示现在缩减的静态内容
<?php $ this-> content-> display()? >
< / head>
< / html>

由于最终模板应由最终用户或类似用户编辑,我相信这是最简单的解决方案。



欣赏所有输入!

解决方案

不应该在Smarty模板中使用任何与codeigniter相关的函数。



如果你需要一些来自CI的信息,你将 assign 到控制器函数中的智能变量 - 所以不会打破MVC:

  $ smarty - > assign(array(
display=> $ this-> content-> display(),
css_include=> $ this-> content-& (array(my.css,style.css))
));

由于 jco 在简单的情况下写上CI& Smarty也许是overkill。但同样的情况下,它非常有用(例如模板继承可用于创建相同的表单类型)。 p>

First off: I do realize that using models in a view is against the MVC hierarchy - but that's the smoothest solution I've found so far.

I've integrated Smarty into my CodeIgniter CMS application. One of its features is the usage of templates along with minifying static content such as CSS and JavaScript. Hence, I'm trying to do something like this in the template file:

//Adding the static content
<?php
$this->content->css( array('my.css', 'style.css') );
?>

<html>
   <head>
      //Displaying the now minified static content
      <?php $this->content->display() ? >
   </head>
</html>

Since the final template should be editable by the end user or similar, I believe that this is the most simple solution.

Appreciating all input!

解决方案

I think you shouldn't use any codeigniter-releated function in Smarty-templates. The CSS directory is constant so unnecessary to call a php-function.

If you'll need some information from CI, you'll assign to a smarty-variable in your controller-function - so doesn't break MVC:

$smarty->assign(array(
   "display" => $this->content->display(),
   "css_include" => $this->content->css(array("my.css","style.css"))
));

As jco wrote above in simple cases CI&Smarty maybe "overkill". But same cases it's very useful (e.g. template inheritance and blocks can use to create same form-types).

这篇关于Smarty + CodeIgniter - 使用模型的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆