Skip to content

Commit b78f2b0

Browse files
committed
Make meta tags more customizable
1 parent 65105de commit b78f2b0

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

src/WebApp/Layout.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,24 @@ protected function renderHeader() {
4545
}
4646

4747
protected function renderMeta() {
48-
$rc = '<meta charset="utf-8">'.
49-
'<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">'.
50-
'<meta name="pageclass" content="'.get_class($this->page).'">';
51-
// keywords
52-
$keywords = $this->page->getMetaKeywords();
53-
if ($keywords != NULL) {
54-
if (is_array($keywords)) $keywords = implode(',', $keywords);
55-
$rc .= '<meta name="keywords" content="'.htmlentities($keywords).'">';
48+
$rc = '<meta charset="utf-8">';
49+
50+
// Meta
51+
$meta = $this->page->getMeta();
52+
53+
// Some specifics
54+
if (!isset($meta['viewport'])) $meta['viewport'] = 'width=device-width, initial-scale=1, shrink-to-fit=no';
55+
if (!isset($meta['pageclass'])) $meta['pageclass'] = get_class($this->page);
56+
if (!isset($meta['canonical'])) {
57+
$params = $this->app->request->params ? '?'.$this->app->request->params : '';
58+
$meta['canonical'] = $this->app->router->getCanonicalPath().$params;
5659
}
57-
// descriptions
58-
$description = $this->page->getMetaDescription();
59-
if ($description != NULL) {
60-
$rc .= '<meta name="description" content="'.htmlentities($description).'">';
60+
61+
foreach ($meta AS $name => $content) {
62+
$s = is_array($content) ? implode(',', $content) : $content;
63+
$rc .= '<meta name="'.$name.'" content="'.htmlentities($s).'">';
6164
}
62-
// canonical
63-
$params = $this->app->request->params ? '?'.$this->app->request->params : '';
64-
$rc .= '<meta name="canonical" content="'.htmlentities($this->app->router->getCanonicalPath().$params).'">';
65+
6566
// Alternates
6667
foreach ($this->app->router->getLanguages() AS $key => $label) {
6768
if ($key != $this->app->request->language) {

src/WebApp/Page.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ public function getTitle() {
4646
return 'hello_world_title';
4747
}
4848

49+
public function getMeta() {
50+
$rc = array();
51+
$s = $this->getMetaKeywords();
52+
if ($s != NULL) $rc['keywords'] = $s;
53+
$s = $this->getMetaDescription();
54+
if ($s != NULL) $rc['description'] = $s;
55+
return $rc;
56+
}
57+
4958
public function getMetaKeywords() {
5059
return $this->app->getMetaKeywords();
5160
}

0 commit comments

Comments
 (0)