Elxis CMS Forum

Support => Elxis 4.x/5.x DEV => Topic started by: fgijsels on June 08, 2018, 10:34:27

Title: Howto retrieve meta keywords
Post by: fgijsels on June 08, 2018, 10:34:27
Several weeks I try to solve a problem, without finding a real solution.

I work on a template for my website.
I want to retrieve the meta keywords in \index.php, but not with $eDoc-> showHead().
I have tried in different ways in a function in "\includes\hygieia.class.php" and in a function at the end of the "\index.php". Here I have found no way to retrieve the meta keywords.

The only way I have found to retrieve the meta keywords is to place the following code in "\index.php":

$eDoc = eFactory::getDocument();
$keywords = $eDoc->keywords;

<html <?php echo $eDoc->htmlAttributes(); ?>>   
   <head>
      ...
      <? php test1($keywords); ?>
      ...
   </head>
</html>

function test1(&$keywords){
      if (count($keywords) > 0) {
         $str = eUTF::str_replace('"',"'", implode(', ', $keywords));
         $html .= "\t".'<meta name="keywords" content="'.$str.'" />'."\n";
      }
      echo $html;
}

My questions:
Keywords is a private variabele In class elxisDocument (\inetpub\wwwroot\includes\libraries\elxis\document.class.php), so i do not understand wy this code works.
Is it safe to do it like this?
Are there beter ways to retrieve the meta keywords tag?
Title: Re: Howto retrieve meta keywords
Post by: datahell on June 08, 2018, 14:55:30
Are you sure this code works? As you said keywords is a private variable. I will see it in the afternoon and answer you again but as far as I remember you cannot get the meta keywords, you can only set them.
Title: Re: Howto retrieve meta keywords
Post by: fgijsels on June 08, 2018, 15:47:44
I could not figure it out. I thought that by changing and trying code (while searching for a solution to my problem), something gets messed up somewhere in the elxis code.

That is why I have performed a completely new elxis installation.
The code (as I put it on the forum) pasted into template.
And it still works!
Title: Re: Howto retrieve meta keywords
Post by: datahell on June 08, 2018, 20:10:27
You made a big discovery! I haven't noticed that before. Private variables work because you actually work within elxisDocument class! So, you can access keywords directly: $eDoc->keywords. I have to say that I dont like that but it works...
Title: Re: Howto retrieve meta keywords
Post by: webgift on June 09, 2018, 18:07:31

Private variables work because you actually work within elxisDocument class!
I feel the same as i don't like it either :D
Title: Re: Howto retrieve meta keywords
Post by: fgijsels on June 10, 2018, 18:05:24
finally found the following solution:

in C:\inetpub\wwwroot\templates\hygieia\index.php

...
elxisloader::loadFile('templates/hygieia/includes/hygieia.class.php');
$hygieia = new templateHygieia();

// display the document type before the html tag.
echo $eDoc->getDocType()."\n";

?>

<!-- Start html this way (elxis will append some attributes on the html tag) -->
<html <?php echo $eDoc->htmlAttributes(); ?>>
   
   <head>
      <?php $hygieia->getmetaKeywords($eDoc); ?>
      ...
   </head>
...
</html>

in C:\inetpub\wwwroot\templates\hygieia\includes\hygieia.class.php

class templateHygieia {

   /*********************/
   /* GET META KEYWORDS */
   /*********************/
   public function getMetaKeywords($eDoc) {
      $myReflectionClass = new ReflectionClass(get_class($eDoc));
      $myProperty = $myReflectionClass->getProperty('keywords');
      $myProperty->setAccessible(true);
      $keywords = $myProperty->getValue($eDoc);
      if (count($keywords) > 0) {
         $str = eUTF::str_replace('"',"'", implode(', ', $keywords));
         $html .= "\t".'<meta name="keywords" content="'.$str.'" />'."\n";
      }
      echo $html;
   }
   
}