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

发布于 2025-01-10 12:54:58 字数 639 浏览 0 评论 0原文

我正在使用 codeigniter,我正在尝试使用动态元标记,但元标记对我不起作用,这是我在控制器中的代码

$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 }?>

I am working on codeigniter and i am trying to use dynamic meta tag but meta tags not working for me, Here is my code in controller

$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')
            );

Here is my view

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新人笑 2025-01-17 12:54:58

只需进入 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;
}

Just go inside codeigniter 3 system/helpers/html_helper.php and copy the function and put it in your own libraries

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文