如何删除代码点火器中 lang() 的自动标签包装。
手册没有说什么:https://www.codeigniter.com/user_guide/helpers/language_helper.html
我必须自己编写一个函数还是缺少一种简单干净的方法?
不要编写第二个参数。将其留空。
看看 lang 函数(在: /system/helpers/language_helper.php
中找到):
function lang($line, $for = '', $attributes = array())
{
$CI =& get_instance();
$line = $CI->lang->line($line);
if ($for !== '')
{
$line = '<label for="'.$for.'"'._stringify_attributes($attributes).'>'.$line.'</label>';
}
return $line;
}
如您所见,它需要 3 个参数。第一个参数是必需的,但后两个参数是可选的。如果声明第二个参数,它将返回包装在标签中的语言字符串。
因此,仅声明第一个参数应该使其仅输出语言字符串。
<小时 />更新:
从阅读您的评论来看,听起来您最好直接使用语言课程。但是,仅靠语言课程是不够的,您需要根据您的目的扩展它。为此,您可以在application/core
文件夹中创建一个名为 MY_lang.php
的新文件。
class MY_Lang extends CI_Lang {
// You want to extend the line function
function line($line = '', $value = '')
{
$line = ($line == '' OR ! isset($this->language[$line])) ? FALSE : $this->language[$line];
// We can assume that if a value is passed it is intended to be inserted into the language string
if($value) {
$line = sprintf($line, $value);
}
// Because killer robots like unicorns!
if ($line === FALSE)
{
log_message('error', 'Could not find the language line "'.$line.'"');
}
return $line ;
}
}
假设您的语言文件具有如下字符串:
$lang['welcome_text'] = "Welcome %s";
然后,您可以通过加载语言类并使用以下代码来使用它:
$name = "foo";
$this->lang->line('welcome_text', $name);
以上是100%未经测试的,所以它可能需要一些调整,但它应该给你一个开始的地方。