问题
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