Yii2 中 如何设置 keywords 和 descripition
2015-02-01 19:19
615 查看
在Yii2中如何设置title是非常容易的,只要在view.php里面设置$this->title="xxx"即可
代码如下:
博主原本以为会有类似$this->keywords这种,然后果断发现没有。(强烈要求yii官方增加这个)
于是看了下源代码(View.php)发现有一个名为metaTags的变量 还有一个 registerMetaTag的方法
部分代码如下:
registerMetaTag方法中用了Html::tag 这个方法
代码如下:
在Html::tag中又使用了static::renderTagAttributes()方法
代码如下:
从以上代码可以看出,如果我们在view.php中编写如下代码
所以如果我们需要<meta name="keywords" contents="" />
那么有两种写法
第一次写博文,显得比较稚嫩,有不对的地方还望多多包涵。
代码如下:
$this->title="page title";但是如何设置 keywords 和 descripition那?
博主原本以为会有类似$this->keywords这种,然后果断发现没有。(强烈要求yii官方增加这个)
于是看了下源代码(View.php)发现有一个名为metaTags的变量 还有一个 registerMetaTag的方法
部分代码如下:
/** * @var array the registered meta tags. * @see registerMetaTag() */ public $metaTags;
/** * Registers a meta tag. * @param array $options the HTML attributes for the meta tag. * @param string $key the key that identifies the meta tag. If two meta tags are registered * with the same key, the latter will overwrite the former. If this is null, the new meta tag * will be appended to the existing ones. */ public function registerMetaTag($options, $key = null) { if ($key === null) { $this->metaTags[] = Html::tag('meta', '', $options); } else { $this->metaTags[$key] = Html::tag('meta', '', $options); } }
registerMetaTag方法中用了Html::tag 这个方法
代码如下:
/** * Generates a complete HTML tag. * @param string $name the tag name * @param string $content the content to be enclosed between the start and end tags. It will not be HTML-encoded. * If this is coming from end users, you should consider [[encode()]] it to prevent XSS attacks. * @param array $options the HTML tag attributes (HTML options) in terms of name-value pairs. * These will be rendered as the attributes of the resulting tag. The values will be HTML-encoded using [[encode()]]. * If a value is null, the corresponding attribute will not be rendered. * * For example when using `['class' => 'my-class', 'target' => '_blank', 'value' => null]` it will result in the * html attributes rendered like this: `class="my-class" target="_blank"`. * * See [[renderTagAttributes()]] for details on how attributes are being rendered. * * @return string the generated HTML tag * @see beginTag() * @see endTag() */ public static function tag($name, $content = '', $options = []) { $html = "<$name" . static::renderTagAttributes($options) . '>'; return isset(static::$voidElements[strtolower($name)]) ? $html : "$html$content</$name>"; }
在Html::tag中又使用了static::renderTagAttributes()方法
代码如下:
/** * Renders the HTML tag attributes. * * Attributes whose values are of boolean type will be treated as * [boolean attributes](http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes). * * Attributes whose values are null will not be rendered. * * The values of attributes will be HTML-encoded using [[encode()]]. * * The "data" attribute is specially handled when it is receiving an array value. In this case, * the array will be "expanded" and a list data attributes will be rendered. For example, * if `'data' => ['id' => 1, 'name' => 'yii']`, then this will be rendered: * `data-id="1" data-name="yii"`. * Additionally `'data' => ['params' => ['id' => 1, 'name' => 'yii'], 'status' => 'ok']` will be rendered as: * `data-params='{"id":1,"name":"yii"}' data-status="ok"`. * * @param array $attributes attributes to be rendered. The attribute values will be HTML-encoded using [[encode()]]. * @return string the rendering result. If the attributes are not empty, they will be rendered * into a string with a leading white space (so that it can be directly appended to the tag name * in a tag. If there is no attribute, an empty string will be returned. */ public static function renderTagAttributes($attributes) { if (count($attributes) > 1) { $sorted = []; foreach (static::$attributeOrder as $name) { if (isset($attributes[$name])) { $sorted[$name] = $attributes[$name]; } } $attributes = array_merge($sorted, $attributes); } $html = ''; foreach ($attributes as $name => $value) { if (is_bool($value)) { if ($value) { $html .= " $name"; } } elseif (is_array($value) && $name === 'data') { foreach ($value as $n => $v) { if (is_array($v)) { $html .= " $name-$n='" . Json::encode($v, JSON_HEX_APOS) . "'"; } else { $html .= " $name-$n=\"" . static::encode($v) . '"'; } } } elseif ($value !== null) { $html .= " $name=\"" . static::encode($value) . '"'; } } return $html; }
从以上代码可以看出,如果我们在view.php中编写如下代码
$this->registerMetaTag(array("xxx"=>"xxx");//实际是$this->metaTags[]="<meta xxx"."='xxx'/>";然后再输出成<head>中
所以如果我们需要<meta name="keywords" contents="" />
那么有两种写法
$this->registerMetaTag(array("name"=>"keywords","content"=>"xxx"));//第一种 $this->metaTags[]="<meta name='keywords' content='xxx'/>";//第二种
第一次写博文,显得比较稚嫩,有不对的地方还望多多包涵。
相关文章推荐
- 如何自动设置网页中meta节点keywords属性-【SEO】
- Yii2的主从数据库如何设置?
- SVN使用详解之如何设置svn:keywords
- yii2 中设置/添加 css、js、meta(keywords description) 等样式标签
- Yii2 如何设置首页
- SVN使用详解之如何设置svn:keywords
- 如何在AIX中设置中文环境?
- Access Control List and Process(如何设置DACL)
- 如何为自己的网站设置二级域名?
- 如何设置IBM HTTP Server拒绝某些IP地址的访问
- 如何检查当TextBox设置为多行时是不是超长了?
- zt:如何显示中文化的打印设置对话框?
- c#如何读INI文件中的设置信息
- 如何设置要运行的.NET Framework的版本?
- 如何在工作组设置中将本地策略应用于 Windows Server 2000 上除管理员以外的所有用户
- 如何为网页设置背景音乐
- 如何做:在 Web 服务器上设置 SSL
- 如何在程序中动态设置墙纸
- 请问在C/S系统中如何设置配置文件
- 如何为用户设置密码