Load view codeigniter with plain text

孤者浪人 提交于 2020-01-05 10:32:32

问题


I am using codeigniter for my website and I was wondering if I could load a view in a view without using PHP. So like a Template Parser, is it possible to run this code:

<div>
    <?php $this->load->view('widget'); ?>
</div>

with plain text like:

<div>
    {loadview:widget}
</div>

Tnx


回答1:


Sorry, but there's nothing built in to Codeigniter that does this. You'll have to look at using a third party template parser or write your own.

Some suggestions:

  • Twig: http://twig.sensiolabs.org/ (recommend)
  • Smarty: http://www.smarty.net/
  • Dwoo: http://dwoo.org/
  • Mustache: http://mustache.github.com/ (not full featured, but language indepenent)

With CI's native template parser, you can assign a variable that contains the content:

$data['widget'] = $this->load->view('widget', NULL, TRUE);
$this->load->view('my_view', $data);

The third param as TRUE buffers the output so you can use it in a variable without it printing. Then in your view:

<div>
    {{widget}}
</div>

..but currently, CI's parser has very limited capabilities. I'd go with Twig or Smarty.



来源:https://stackoverflow.com/questions/9669776/load-view-codeigniter-with-plain-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!