Elxis CMS Forum

Support => General => Topic started by: xmanhattan on January 06, 2017, 15:10:11

Title: Elxis 4.x language detection for meta tags
Post by: xmanhattan on January 06, 2017, 15:10:11
Hello all,
I would like to use a routine that I have from Elxis 2009.x to add meta tags based upon the language as I added tags for Open Graph Protocol, facebook, twitter, etc. in a separate file.

This is what I have that works in 2009.x but I am not sure if the variable and detection is the same.

Code: [Select]
class seometatags {
    public function dispseometatags() {
global $mainframe, $lang, $option;

if ($lang == 'english') {
// display english meta data

}else{
// display greek meta data

}

I found this in https://www.elxis.net/docs/developers/libraries/elxisuri.html (https://www.elxis.net/docs/developers/libraries/elxisuri.html) but do not know if I should use it and how can I use it with the above IF statement?

Code: [Select]
public function getUriLang()
Otherwise do I have to create each line using this method?
Code: [Select]
$eDoc->setMetaTag('viewport', 'width=device-width, initial-scale=1.0');


Title: Re: Elxis 4.x language detection for meta tags
Post by: xmanhattan on January 10, 2017, 11:08:48
Anyone?
Title: Re: Elxis 4.x language detection for meta tags
Post by: datahell on January 10, 2017, 11:53:46
In Elxis 4.x you can get the current language like this:

$currentlang = eFactory::getLang()->currentLang();

The result is the Elxis 2 characters language code (en, el, es, it, de, ru, etc...)

After you get the current language you can use the if/else statements like before

if ($currentlang == 'el') {
    //do something for greek
} else if ($currentlang == 'it') {
    //do something for italian
} else if ($currentlang == 'en') {
    //do something for english
} else {
    //other language or english again
}

To add a meta tag in Elxis 4.x you work with setMetaTag method like this:

eFactory::getDocument()->setMetaTag($name, $content);

Where $name the name of the meta tag and $content its value.

Example:
eFactory::getDocument()->setMetaTag('twitter:description', 'Some description for Twitter');

As setMetaTag method adds name/content attributes if you want to insert other attributes such as "property" use the addCustom method instead.

Example:
eFactory::getDocument()->addCustom('<meta property="og:title" content="Title here"/>');

With the addCustom method of the Elxis Document library you can put ANYTHING you want in page head section which is not supported by the standard methods.
Title: Re: Elxis 4.x language detection for meta tags
Post by: xmanhattan on January 10, 2017, 15:26:59
Datahell,

Can you also explain what is the difference between using the eFactory with parameters and simply using html in the if statements?

example:
Code: [Select]
eFactory::getDocument->addCustom('<meta name="geo.placename" content="Athens, Greece"/>');
eFactory::getDocument->addCustom('<meta name="geo.position" content=37.9; -23.7"/>');
eFactory::getDocument->addCustom('<meta name="geo.region" content="GR-Attiki"/>');
eFactory::getDocument->addCustom('<meta name="ICBM" content="37.9; -23.7"/>');
eFactory::getDocument->addCustom('<meta name="DCTERMS.created" scheme="ISO8601" content="2016-01-01"/>');

Code: [Select]
echo '<!--  geo metadata  -->
<meta name="geo.placename" content="Athens, Greece">
<meta name="geo.position" content="37.9; -23.7">
<meta name="geo.region" content="GR-Attiki">
<meta name="ICBM" content="37.9; -23.7">
<meta name="DCTERMS.created" scheme="ISO8601" content="2016-01-01">
';

I added the following into the Flex template index.php file after the HEAD but it didn't work.
It just shows up as a blank page.  This is using localhost through wamp.
I tried it with and without the elxisloader loadfile.

Code: [Select]
<?php $eDoc->showHead(); ?>

<?php 
//  to load the seo meta tags through a separate file
//elxisloader::loadFile('templates/flex/includes/seometatags.class.php'); 

$currentlang eFactory::getLang()->currentLang();

if (
$currentlang == 'en') {
eFactory::getDocument->addCustom('<meta name="geo.placename" content="Athens, Greece"/>');
eFactory::getDocument->addCustom('<meta name="geo.position" content=37.9; -23.7"/>');
eFactory::getDocument->addCustom('<meta name="geo.region" content="GR-Attiki"/>');
eFactory::getDocument->addCustom('<meta name="ICBM" content="37.9; -23.7"/>');
eFactory::getDocument->addCustom('<meta name="DCTERMS.created" scheme="ISO8601" content="2016-01-01"/>');

/*
echo '<!--  geo metadata  -->
<meta name="geo.placename" content="Athens, Greece">
<meta name="geo.position" content="37.9; -23.7">
<meta name="geo.region" content="GR-Attiki">
<meta name="ICBM" content="37.9; -23.7">
<meta name="DCTERMS.created" scheme="ISO8601" content="2016-01-01">
';

*/
} else if ($currentlang == 'el') {

}
?>


Elxis is great!!!

Thank you
Title: Re: Elxis 4.x language detection for meta tags
Post by: datahell on January 10, 2017, 18:53:43
The difference: You cannot affect page headers with simple html echo except if you are working on template's php files. For all other extensions you can work only with elxis Document library. But even if you work on a template it is better to use Elxis Document library because it is more safe (for example avoid duplicated entries added by other extensions - modules/components/etc) and you have a better structured HTML.

Regarding the error: My mistake, I forgotten the parenthesis after the getDocument method. Sorry for the trouble. I fixed my previous post.
eFactory::getDocument()->addCustom('blah blah anything you want to put on page head...');

This is the proper usage for you:

$eDoc = eFactory::getDocument();
$eDoc->setMetaTag('geo.placename', 'Athens, Greece');
$eDoc->setMetaTag('geo.position', '37.9; -23.7');
$eDoc->setMetaTag('geo.region', 'GR-Attiki');
$eDoc->setMetaTag('ICBM', '37.9; -23.7');
$eDoc->addCustom('<meta name="DCTERMS.created" scheme="ISO8601" content="2016-01-01">');

Better use the above code BEFORE you echo anything on the template. If you put it after you echo something it will also work because Elxis uses output buffering but semantically it is more correct to type it before any echos.
Title: Re: Elxis 4.x language detection for meta tags
Post by: xmanhattan on January 11, 2017, 15:12:12
Datahell,

Okay, using $eDoc and eFactory work when inserted into the index.php file of the flex template but I am having a problem trying to load it as a separate file.  The display is blank.

I noticed that using $eDoc places the meta tags in the begining of the output whereas eFactory places them after the initial javascript.

Can you have a look at this and indicate the correct method of loading and executing the external file?

Code: [Select]
defined('_ELXIS_') or die ('Direct access to this location is not allowed');

$eLang = eFactory::getLang();
$eDoc = eFactory::getDocument();
$elxis = eFactory::getElxis();

$touch_icon = $elxis->secureBase().'/templates/flex/images/touch.png';
$eDoc->setMetaTag('viewport', 'width=device-width, initial-scale=1.0');
$eDoc->addLink($touch_icon, '', 'apple-touch-icon');

$jslink = $elxis->secureBase().'/templates/flex/includes/flex.js';
$eDoc->addJQuery();
$eDoc->addScriptLink($jslink);
unset($touch_icon, $jslink);

elxisloader::loadFile('templates/flex/includes/flex.class.php')."\n";
//elxisloader::loadFile('templates/flex/includes/seometatags.class.php')."\n";
$flex = new templateFlex();
//elxisloader::loadFile('templates/flex/includes/seometatags.class.php')."\n";

echo $eDoc->getDocType()."\n";
?>
<html<?php echo $eDoc->htmlAttributes(); ?>>
<head>
<?php $eDoc->showHead(); ?>

I placed both uses into the external file seometatags.class.php as:
Code: [Select]
defined('_ELXIS_') or die ('Direct access to this location is not allowed.');

class seometatags {

$currentlang = eFactory::getLang()->currentLang();
if ($currentlang == 'en') {

//$eDoc = eFactory::getDocument();  already included in index.php
echo "<!--  geo metadata  -->";
$eDoc->setMetaTag('geo.placename', 'Athens, Greece');
$eDoc->setMetaTag('geo.position', '37.9; -23.7');
$eDoc->setMetaTag('geo.region', 'GR-Attiki');
$eDoc->setMetaTag('ICBM', '37.9; -23.7');
$eDoc->addCustom('<meta name="DCTERMS.created" scheme="ISO8601" content="2016-01-01">');

eFactory::getDocument()->addCustom('<meta name="geo.placename" content="Athens, Greece"/>');
eFactory::getDocument()->addCustom('<meta name="geo.position" content=37.9; -23.7"/>');
eFactory::getDocument()->addCustom('<meta name="geo.region" content="GR-Attiki"/>');
eFactory::getDocument()->addCustom('<meta name="ICBM" content="37.9; -23.7"/>');
eFactory::getDocument()->addCustom('<meta name="DCTERMS.created" scheme="ISO8601" content="2016-01-01"/>');

} else if ($currentlang == 'el') {
}
}

Thanks again!

Title: Re: Elxis 4.x language detection for meta tags
Post by: datahell on January 11, 2017, 20:33:18
Your seometatags.class.php file is wrong. You create a class and write raw php code in it. This is invalid. You must create methods, put your code inside them, initialize the class from the template file and then use the methods you want.

class seometatags() {

    public function __construct() {}

    public function mycode() {
        put your code here
    }

}

In template:
elxisloader::loadFile('templates/flex/includes/seometatags.php');
$seotags = new seometatags();
$seotags->mycode();

If object oriented programming is hard for you better work without class. Create file seometatags.php, put your raw php code inside it without the class wrapper and then include it in your template with

elxisloader::loadFile('templates/flex/includes/seometatags.php');
Title: Re: Elxis 4.x language detection for meta tags
Post by: xmanhattan on January 12, 2017, 12:15:09
Datahell,

Thank you.  After a few changes to the files I managed to get it working.

As you guessed, I don't have the knowledge and training regarding classes but will give it another try to learn it better.