eZ Community » Forums » Developer » Meta Tags - individual and Fallback
expandshrink

Meta Tags - individual and Fallback

Meta Tags - individual and Fallback

Tuesday 28 April 2015 2:30:51 pm - 5 replies

Hey
I would like to have the standard meta tags but as well to create new ones for individual sites.

 

Therefore i created a new class named metatags with the attributes of title description keywords.
So i think i need to create an if loop like when there is the class of metatags use this and not the standard metatags. But i'm not sure where to insert this and how to actually write it down..
Can anybody explain this to me? I'm still new to all of this..

Tuesday 28 April 2015 9:09:46 pm

Hello Lena happy.gif Emoticon

If your using legacy the best solution for the use case(s) you describe that I (and many others) regularly use (and have done so for many customer websites / years / versions) is xrowmetadata! It's ... awesome. I've not (yet) met a need in this regard that it did not nicely provide for. 

https://github.com/xrowgmbh/xrowmetadata

Feel free test it and let us know what you think.

As always, I hope this helps!

Cheers,
Heath

Modified on Tuesday 28 April 2015 9:10:15 pm by // Heath

Thursday 30 April 2015 2:08:23 pm

Hello Heath,

 

Thank you for your suggestin! Looks good. But sadly i'm forbidden to use  any add ons / extensions like this one..

 

I found this code on the web:

 {let node_metas=fetch(content, node, hash(node_id, $module_result.node_id))}
    <meta name="description" content="{$node_metas.object.data_map.meta_description.content.output.output_text}" />
    <meta name="keywords" content="{$node_metas.object.data_map.meta_keywords.content.output.output_text}" />
 
{/section}
 
{/let}

It's jsut that it's not working for me.. I inserted at the page_head and the pagelayout tpl. But the only output i get is an empty :

<meta content="" name="description"></meta> 
<meta content="" name="keywords"></meta>

Any suggestions?

Friday 01 May 2015 1:55:07 am

Hello Lena,

I understand that you can not use non-default extensions, but I hope you can still test them / read them / learn from them. The xrowmetadata is datatype based but there are still lessons that can be learned and re-implemented withing your own custom solution.

Deprecated code causing problems

In any case first we need you to understand that you should not use the let feature which is deprecated and to new developers confusing, instead you should use it's replacement def which is much simpler to use.

https://doc.ez.no/eZ-Publish/Technical-manual/4.x/Reference/Template-functions/Variables/let

https://doc.ez.no/eZ-Publish/Technical-manual/4.x/Reference/Template-functions/Variables/def

You should also not use the section feature which is also deprecated and confusing to new users, instead you should use foreach, for, do or while for looping.

https://doc.ez.no/eZ-Publish/Technical-manual/4.x/Reference/Template-control-structures/Deprecated/section

https://doc.ez.no/eZ-Publish/Technical-manual/4.x/Reference/Template-control-structures/Looping

Your code does not work because your incorrectly using let which requires closing tags, switch to def and watch your problems (probably) go away!

Best practices

You should also wrap your meta tag in an if conditional and provide a else case with a reliable (ini based?) fall back in case the content object in question can not be found ... for some unpredictable reason, this is just a good idea because you never know what might happen and having a fall back could save the day temporarily; plus it's just good code to plan for possible failure ... you don't want empty meta tags to be displayed.

You should also escape your meta tag content, attribute content output with the 'wash' operator to ensure you do not break the validation of the page with invalid characters, wrap usage solves that. Here is an example: https://github.com/xrowgmbh/xrowmetadata/blob/master/design/standard/templates/page_head.tpl.sample#L45

You may also need to dynamically replace characters in your meta tag content output to ensure you don't break the html, things like '"' char needs to be replaced or at least striped out (per your example). This case might not come up later but I always think ahead so the content editors using the admin don't have to worry about what content they input might break the meta tag output. 

Most common meta tag solution written from scratch using default datatypes

Also your example is what I call the first stage, moving meta tag content from static or ini settings based storage into a content object. This is a good first step for the editors, so they can control this content, I wish this was a default feature in eZ Publish myself. The next step, stage 2, is to add additional content class attributes in -almost- all the classes within the system (not just the ones you are currently using, you never know and it's best to be sure upfront). This stage 2 provides for per page / content object unique content for meta tag usage.

You would want to make use of default datatypes (at first) and add at least meta tag attributes (in a class attribute group) to provide for storage of the following meta tag's content: meta title (ezstring), meta description (eztext), meta tags (variable, everyone seems to use all kinds of different storage solutions for this meta tag, I start with the simple comma separated ezstring, others use eztags extension (which may not work for you), others use a custom datatype or different default datatype). 

Attribute group usage helps keep the meta tags available but out of sight when not needed.

Then you refactor the first stage template logic to first try to fetch the (stage 2; per page meta tag content) current node's object meta tag attribute content (if it is set use $node.data_map.meta_title.content.has_content|eq( true ) to test before trying to use the content) then if their is not meta tag attributes in the object or if it does not have content then fall back to your stage 1 meta tag content (global). This is still just the start of what customers have had me implement frequently in the past to provide the very best possible unique (per page) meta tag content (for search engine ranking / seo / etc).

Redundant and content location specific Meta tags and fallbacks 

In short this means your template logic needs to follow at least the following chain of meta tag content access. This provides for the very best fallback in case for some reason any one source does not have the content expected.

  1. Current Node Meta tag content
  2. Global Node Meta tag content
  3. INI Meta tag content
  4. Static template string content

The conversation on StackOverflow

Worth noting I found this question cross-posted to StackOverflow (no judgements, just ensuring we keep some continuity to the discussion): http://stackoverflow.com/question.../ezpublish-legacy-fetching-meta-tags

Once you confirm your question is solved here, I'll post a cleaned up, clearer, more specific, direct answer on StackOverflow as I am trying to ensure the ezpublish questions on stack get good answers too (plus free internet points if you confirm my answer as correct on stack happy.gif Emoticon

Apologies for my late reply; I had to get some sleep before answering your question; been working too many long days in a row and just crashed. Also sorry for my long winded answer, just trying to share some additional lessons I've learned over the last 10+ years when creating the same solution your trying to create now.

I hope this helps! Please feel free to ask any questions you might have!

Cheers,
Heath

Modified on Friday 01 May 2015 2:02:27 am by // Heath

Monday 04 May 2015 9:41:47 am

Hello Heath!

Thank you so so much for your long reply! No pronlem it took you longer, it's worth the wait! I'm still trying to understand everything and look thou everything.
My problem is i think that i'm still new to ezpublish and i didn't learn it from the beginning. My Chef has thrown me in a running project and told me learn it. Second problem: I'm still a newbie at PHP as well..

But your long answere gave me so much new information! Thanks a lot! I will give you a feedback as soon as i finished reading it all and trying out some things.

 

Thank you so much for all your help! I'm so happy someone's there who is willing to help! Means a lot for me!
Have a great start into the week and hope you had some free time on the weekend to reload your energy!

Lena

Monday 04 May 2015 10:52:19 am

Hello Lena,

Your very welcome. Feel free to keep asking more questions on this or any topic related to eZ Publish.

We are here to help.

Hehehe and yes. While I got a lot of personal work done this weekend, I made sure to sleep in / over sleep / catch up on sleep as well big-smile.gif Emoticon

I'm ready for the challenges and opportunities this new week will surely present happy.gif Emoticon

One last suggestion. You might find you enjoy even faster / more interactive eZ Publish help if you login to the #ezpublish irc chat room. You can learn more about it here, http://share.ez.no/get-involved/exchange#eztoc526814_0_3

The chat room is great for simple quick answers to smaller questions and general discussions about eZ Publish.

Cheers,
Heath

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from