我需要以不同的方式循环许多数组并将其显示在页面中.数组由模块类生成.我知道最好不要在'views'上包含函数,我想知道在哪里插入函数文件.
我知道我可以"扩展"帮助者,但我不想扩展帮助者.我想用循环函数创建一个帮助器.让我们称之为loops_helper.php
我有点困惑,在代码点火器中使用库和助手的方法.我还在学习代码点火器.
CONTROLLER
function index(){
$this->load->helper('text');
$this->load->library('auth'); //custom library
$data['string'] = 'this is sample ..... this is sample';
$this->load->view('article', $data);
}
Run Code Online (Sandbox Code Playgroud)
视图
<?php
if(is_logged_in()){ //is_logged_in() is the method from the library, 'auth'
echo 'You are logged in';
}
<p><?php echo word_limiter($string, 10); ?></p> <!--word_limiter() is the method from the helper, 'text' -->
Run Code Online (Sandbox Code Playgroud)
在上面的视图文件中,帮助方法word_limiter()工作正常.但该方法is_logged_in()不起作用.但如果我这样做($this->auth->is_logged_in()),它会起作用.
但是为什么来自helper的方法word_limiter()不必像这样写($this->text->word_limiter()).
调用helper和library的方法有什么区别吗?
它是一个很好的解决方法,并且可以在CodeIgniter中的视图中使用帮助程序类.我有一种情况,我必须从一对文本字符串中提取常量表达式并在匹配项上生成输出.我不想在视图中直接这样做,我想为此目的使用帮助器.
application
--view
---myview.php
Run Code Online (Sandbox Code Playgroud)
在这里我应该调用帮助器并返回结果
例如,我想从文本中提取处理器的类型,而不是传递文本并返回处理器类型.需要这个是因为视图中的所有数据都是由API动态生成的.
echo $myhelper->processor($text);
Run Code Online (Sandbox Code Playgroud)