热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Yii框架中给网页设置keywords和description的方法

Yii框架中给网页设置keywords和description的方法

If we want to set meta tags on a per page (controller-action) basis, we may use the?clientScript?application component.

Controller

Yii::app()->clientScript->registerMetaTag('This is an example', 'description');

    $this->render('example');

  }

}

?>

This will insert the description meta tag automatically into the layout when the render() function gets called.

Another solution would be to use a parent controller with the property $pageDescription. That way we don't have to make a function-call to set the description. Instead, we can easily modify the description inside of the controller. In the layout-file we'll simply echo the meta tag (if defined).

Parent-Controller

Controller

pageDescription = 'This is an example';

    $this->render('example');

  }

}

?>

Layout


pageDescription))
{
  echo 'pageDescription . '" />';
}

?>





As you can see, the second solution is probably better to work with because we can directly access $pageDescription and won't have to call an ugly long function every time. On the other hand, we have now additional php-code in the layout. Well that's not bad at all, but it could lead to problems.

What if we have 10 different layouts? What if we use several themes? In each of these files we would've to insert that snippet. Now let's think instead of a single string (the description), we would implement the same functionality for several rss-feed meta tags (an array). That would mean we have to do a foreach inside of the layout(s).

For a more flexible solution, we extend CController with a custom function.

beforeRender())
        {
            parent::render($view, $data, $return);
        }
    }

    public function beforeRender()
    {
        return true;
    }

}

?>

The parent-controller will make use of our extended CController and the special function.

pageDescription))
        {
            Yii::app()->clientScript->registerMetaTag($this->pageDescription, 'description');
        }

        return true;

    }

}

?>

The actual controller remains the same (like in the second solution).

pageDescription = 'This is an example';

    $this->render('example');

  }

}

?>

When the render() function gets called, ExtendedCController will first execute the custom beforeRender() function in our parent-controller to insert the meta tag and after that it will call the original CController::render() function.

That means with this solution we can access the $pageDescription property inside of the controller and we don't have to add any special code to the layout. Of course this solution comes in handy especially for things like the already noted rss-feeds.

Think about this:

$this->pageFeeds[] = 'http://example.com/feeds/recent.xml';
$this->pageFeeds[] = 'http://example.com/feeds/popular.xml';

I hope you enjoyed the read. Feel free to share your thoughts - thank you.

Tip:?Since the two classes?ParentController?and?ExtendedCController?are no real controllers (they only function as parent-classes), you have to put them into the components folder (egprotected/components).


推荐阅读
  • 本文详细介绍了暂估入库的会计分录处理方法,包括账务处理的具体步骤和注意事项。 ... [详细]
  • PHP 编程疑难解析与知识点汇总
    本文详细解答了 PHP 编程中的常见问题,并提供了丰富的代码示例和解决方案,帮助开发者更好地理解和应用 PHP 知识。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 郑州大学在211高校中的地位与排名解析
    本文将详细解读郑州大学作为一所位于河南省的211和双一流B类高校,在全国211高校中的地位与排名,帮助高三学生更好地了解这所知名学府的实力与发展前景。 ... [详细]
  • 深入理解 Oracle 存储函数:计算员工年收入
    本文介绍如何使用 Oracle 存储函数查询特定员工的年收入。我们将详细解释存储函数的创建过程,并提供完整的代码示例。 ... [详细]
  • 优化ASM字节码操作:简化类转换与移除冗余指令
    本文探讨如何利用ASM框架进行字节码操作,以优化现有类的转换过程,简化复杂的转换逻辑,并移除不必要的加0操作。通过这些技术手段,可以显著提升代码性能和可维护性。 ... [详细]
  • 本文总结了2018年的关键成就,包括职业变动、购车、考取驾照等重要事件,并分享了读书、工作、家庭和朋友方面的感悟。同时,展望2019年,制定了健康、软实力提升和技术学习的具体目标。 ... [详细]
  • 电子元件封装库:三极管、MOS管及部分LDO(含3D模型)
    本资源汇集了常用的插件和贴片三极管、MOS管以及部分LDO的封装,涵盖TO和SOT系列。所有封装均配有高质量的3D模型,共计96种,满足日常设计需求。 ... [详细]
  • 在计算机技术的学习道路上,51CTO学院以其专业性和专注度给我留下了深刻印象。从2012年接触计算机到2014年开始系统学习网络技术和安全领域,51CTO学院始终是我信赖的学习平台。 ... [详细]
  • CSS 布局:液态三栏混合宽度布局
    本文介绍了如何使用 CSS 实现液态的三栏布局,其中各栏具有不同的宽度设置。通过调整容器和内容区域的属性,可以实现灵活且响应式的网页设计。 ... [详细]
  • 本文详细介绍了如何使用PHP检测AJAX请求,通过分析预定义服务器变量来判断请求是否来自XMLHttpRequest。此方法简单实用,适用于各种Web开发场景。 ... [详细]
  • 小红书提高MCN机构入驻门槛,需缴纳20万元保证金
    近期,小红书对MCN机构的入驻要求进行了调整,明确要求MCN机构在入驻时需缴纳20万元人民币的保证金。此举旨在进一步规范平台内容生态,确保社区的真实性和用户体验。 ... [详细]
  • Linux 系统启动故障排除指南:MBR 和 GRUB 问题
    本文详细介绍了 Linux 系统启动过程中常见的 MBR 扇区和 GRUB 引导程序故障及其解决方案,涵盖从备份、模拟故障到恢复的具体步骤。 ... [详细]
  • 动物餐厅高效获取小鱼干攻略
    本文将介绍2023年动物餐厅中快速赚取小鱼干的有效方法,帮助玩家更轻松地积累资源。 ... [详细]
  • This guide provides a comprehensive step-by-step approach to successfully installing the MongoDB PHP driver on XAMPP for macOS, ensuring a smooth and efficient setup process. ... [详细]
author-avatar
风中凌乱2602938623
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有