无法在codeigniter中获取动态 meta标记

rkkpypqq  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(152)

我在codeigniter工作,我试图使用动态 meta标记,但元标记不为我工作,这是我的代码在控制器

$id = $this->uri->segment(2);
        $data['id'] = $id;
        $data['metas'] = array(
             array('name'=>'description', 'content'=>'A short but sweet DEFAULT description of this fine site'),
             array('name' =>'keywords', 'content'=>'some awesome DEFAULT keywords for those rascally web crawlers')
            );

以下是我的观点

<?php 
      foreach($metas as $meta)
      {?>
         <meta name="<?=$meta['name']?>" content="<?=$meta['content']?>" />
<?php }?>
p8h8hvxi

p8h8hvxi1#

只需进入codeigniter 3 system/helpers/html_helper.php并复制该函数,然后将其放入您自己的库中

function meta($name = '', $content = '', $type = 'name', $newline = "\n")
{
    // Since we allow the data to be passes as a string, a simple array
    // or a multidimensional one, we need to do a little prepping.
    if ( ! is_array($name))
    {
        $name = array(array('name' => $name, 'content' => $content, 'type' => $type, 'newline' => $newline));
    }
    elseif (isset($name['name']))
    {
        // Turn single array into multidimensional
        $name = array($name);
    }

    $str = '';
    foreach ($name as $meta)
    {
        $type       = (isset($meta['type']) && $meta['type'] !== 'name')    ? 'http-equiv' : 'name';
        $name       = isset($meta['name'])                  ? $meta['name'] : '';
        $content    = isset($meta['content'])               ? $meta['content'] : '';
        $newline    = isset($meta['newline'])               ? $meta['newline'] : "\n";

        $str .= '<meta '.$type.'="'.$name.'" content="'.$content.'" />'.$newline;
    }

    return $str;
}

相关问题