title="page title";但是如何设置 keywords 和 descripition那?" />
入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

Yii2 中 如何设置 keywords 和 descripition

创建时间:2015-02-01 投稿人: 浏览次数:2984

在Yii2中如何设置title是非常容易的,只要在view.php里面设置$this->title="xxx"即可

代码如下:

$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"/>";//第二种



第一次写博文,显得比较稚嫩,有不对的地方还望多多包涵。
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
  • 上一篇:没有了
  • 下一篇:没有了
未上传头像