我试图在标题中添加元数据标签,这是我的代码:
/帮助/app/扩展FacebookHtml.php
<?php
namespace app'extensions'helper;
class FacebookHtml extends 'lithium'template'Helper {
protected $_strings = array(
'title' => '<meta property="og:title" content="{:contenido}" />',
'site_name' => '<meta property="og:site_name" content="{:contenido}" />',
'url' => '<meta property="og:url" content="{:contenido}" />',
'description' => '<meta property="og:description" content="{:contenido}" />',
'image' => '<meta property="og:image" content="{:contenido}" />',
'image' => '<meta property="og:image" content="{:contenido}" />',
'locate' => '<meta property="og:locate" content="{:contenido}" />',
);
public function meta($contenido, $options) {
return $this->_render(__METHOD__, $options['type'], compact('contenido'));
}
}
在app/views/layout/default.html.php中
<?=$this->FacebookHtml(); ?>
在其他视图文件中:
<?=$this->FacebookHtml->meta('title', 'Test.. 1...2...3...'); ?>
我在谷歌和核心代码中寻找几个小时,以了解如何添加元数据
第一几注:
在你的例子中,<?=$this->FacebookHtml(); ?>
没有做任何事情。
就像Oerd在他的回答中说的,你的参数不正确。它们应该与FacebookHtml.php
中的函数声明相匹配。它应该是:
<?= $this->FacebookHtml->meta('Test.. 1...2...3...', array('type' => 'title')); ?>
您的助手完成了它应该做的事情,呈现原始元标记。你打电话给你的帮手是很重要的。目前,您只是在适当的位置呈现元标记。然而,li3 Renderer类提供了$this->head()
方法,它做了两件事。
- 将值传递给
head
将其添加到使用当前Renderer的所有模板的上下文中。示例:$this->head("<meta property="og:title" content="The Title" />");
- 回显
$this->head()
将呈现当前head
上下文中持有的所有标签。
下面是一些真实世界的例子:
app/views/布局/default.html.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<?php echo $this->head() ?>
<title><?php echo $this->title(); ?> | My Website</title>
</head>
<body>
<?php echo $this->content(); ?>
</body>
</html>
app/views/页面/index.html.php
<?php $this->FacebookHtml->meta('Test.. 1...2...3...', array('type' => 'title')); ?>
上面的例子允许你在视图中指定任何你想要的头。
除了$this->head()
, li3还提供了$this->styles()
和$this->scripts()
类似的功能。
在视图模板中,必须通过在options数组中提供title
来调用帮助器:
<?= $this->facebookHtml->meta('Test.. 1...2...3...', array('type' => 'title')); ?>
我相信你已经看过了,但是这里是关于帮助器的Lithium手册页