I am trying to add meta data tags in header, this is my code:
app/extensions/helper/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'));
}
}
In app/views/layout/default.html.php, inside
<?=$this->FacebookHtml(); ?>
In other view file:
<?=$this->FacebookHtml->meta('title', 'Test.. 1...2...3...'); ?>
I look for hours in Google and in the core code to know how to add metadata.
First a few notes:
In your example, <?=$this->FacebookHtml(); ?>
doesn't do anything.
Like Oerd said in his answer, your parameters are incorrect. They should match your function declaration in FacebookHtml.php
It should be:
<?= $this->FacebookHtml->meta('Test.. 1...2...3...', array('type' => 'title')); ?>
Your helper does exactly what it's supposed to, rendering the raw meta tags. Where you call your helper is important. As it stands, you are just rendering the meta tags in place. However, the li3 Renderer class provides the $this->head()
method which does two things.
head
adds it to the context for all templates using the current Renderer. Example: $this->head("<meta property="og:title" content="The Title" />");
$this->head()
will render all the tags held in the current head
context.Here's some real world examples:
app/views/layouts/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/pages/index.html.php
<?php $this->FacebookHtml->meta('Test.. 1...2...3...', array('type' => 'title')); ?>
The example above allows you to specify any headers you want inside of your views.
In addition to $this->head()
, li3 also provides $this->styles()
and $this->scripts()
with similar functionality.
Check the default.html.php sample from the li3 framework repository for a more complete example: https://github.com/UnionOfRAD/framework/blob/master/app/views/layouts/default.html.php