Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Generator HTML Tag


Go to End
Reply

16 Posts   8599 Views

Avatar
Mo

29 June 2010 at 4:05am Community Member, 509 Posts

Hi All,

Just noticed that Silverstripe 2.4 now gernerates a generator HTML tag in the sites HEAD. Though this doesn't bother me for most sites, we have some high security sites built in Silverstripe that I would feel more comfortable about removing this for.

Is this possible, and if so, how?

Cheers,

Mo

Avatar
Willr

29 June 2010 at 10:05am Forum Moderator, 5511 Posts

Copy the MetaTags() function from SiteTree.php to your own Page.php. Then you can customize it as much as you need.

Avatar
Mo

29 June 2010 at 7:05pm Community Member, 509 Posts

Oooo, I have always wondered how you customise MetaTags, never really put a lot of time into working it out though.

Cheers Willr!

Mo

Avatar
pietro

30 June 2010 at 7:09am Community Member, 18 Posts

Hi there, so my site works fine in every browser except for explorer (using IE7). My site header looks like this:

<% base_tag %>
<title><% if MetaTitle %>$MetaTitle<% else %>$Title<% end_if %> &raquo; $SiteConfig.Title</title>
$MetaTags
<link rel="shortcut icon" href="/favicon.ico" />

I like how the generator works and i would like to include it in the header. Unfortunately explorer is displaying the Custom Meta Tags in the public header of my site.

The only way i have been able to fix this is by deleting $MetaTags line from the header code but as i said, i would like to include the meta tags and the problem is with IE only.

Can someone point to how I can fix this?

Thank you, P.

Avatar
edk

30 June 2010 at 10:22am (Last edited: 30 June 2010 10:25am), Community Member, 39 Posts

I like this post, because I had thought about this before, but did not know where to go to customize it a little...

I had a client ask about generating Canonical URLs for their Silverstripe site....now I though to myself - with SS you don't really need Canonical URLs because the SS URLSegment is so sweet and simple. (Other CMS struggle at this with params etc)....

Anyways I had created a method in the Page class that generates the Canonical URL, and then when I saw this post, I thought why not customize the MetaTags function and have it generate it instead of putting the call in the .ss template.

So here goes some code.

1. Create a GetCanonical Method in your Page class controller (there are bunch of if/else statement to try and handle actions/IDs/OtherIDs in the NestedURL structure of 2.4)

function GetCanonical() {
      if (Director::urlParam('Action')) {
         $action = Director::urlParam('Action');
       $parameterID = urlencode(Director::urlParam('ID'));
       $parameterOtherID = urlencode(Director::urlParam('OtherID'));
      
      
       if (strcmp($action, $this->URLSegment) == 0 && empty($parameterID) ) {
            return false;
       } else if (strcmp($parameterID, $this->URLSegment) == 0) {
          return false;
       } else if (strcmp($action, $this->URLSegment) != 0 && empty($parameterOtherID)){
          return $action . '/' . $parameterID;
       } else {
          return $parameterID . '/' . $parameterOtherID;
       }
      }
}

2. Add this simple line to your MetaTags function

$tags .= "<link rel=\"canonical\" href=\"" . $this->AbsoluteLink() . $this->GetCanonical() . "\" />\n";

This should work with simple URLs as well as accomodate the case of DataObjects being displayed with action/ID functionality. Please feel free to take it, use it, or even provide feedback on a better and more efficient means to achieve this.

- Ed

Avatar
Tonyair

3 October 2010 at 7:14am Community Member, 81 Posts

What about custom controller?

Director::addRules(50, array('shop/$Action/$ID' => 'Products_Controller'));
class Products_Controller extends Page_Controller

I tried:

   public function MetaTags($includeTitle = TRUE){
         $tags = "<title>TEST</title>\n";
         $this->extend('MetaTags', $tags);
   }


and got nothing

Avatar
inCharge

23 September 2011 at 4:32am (Last edited: 23 September 2011 5:10am), Community Member, 102 Posts

2 things to add to the Canonical discussion...

1) If you're just adding a tag, it's simpler to pull it into the .ss file than override the MetaTags function. (The original reason overriding MetaTags was recommended in this thread was to remove a tag.)

2) The canonical tag should include the domain name if there's a chance that multiple domains or subdomains point to the same page, so the code below uses protocolAndHost.

In the Page.php define...

   function CanonicalURL() {
      return Director::protocolAndHost() . $this->Link();
   }

...then in Page.ss...

<head>
   <% base_tag %>
   <link rel="canonical" href="$CanonicalURL" />
   $MetaTags
</head>

Note that this doesn't include edk's solution for dealing with virtual pages (good point!), but the two could easily be merged...

Avatar
Bastir

29 September 2011 at 2:09am Community Member, 8 Posts

Hi, I tried to use the function GetCanonical by -Ed,

so i added

- function GetCanonical() to the page.php
- $tags .= "<link rel=\"canonical\" href=\"" . $this->AbsoluteLink() . $this->GetCanonical() . "\" />\n"; to SiteTree.php

and i also added the code from inCharge

function CanonicalURL() {
return Director::protocolAndHost() . $this->Link();
}
to page.php

<link rel="canonical" href="$CanonicalURL" />
to the Page.ss

But i only geht an empty <link rel="canonical" href="" />

Can you help?

Best regards bastiR

Go to Top