Jump to:

23481 Posts in 18965 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Generator HTML Tag

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

Page: 1 2
Go to End
Author Topic: 8387 Views
  • Mo
    Avatar
    Community Member
    508 Posts

    Generator HTML Tag Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5503 Posts

    Re: Generator HTML Tag Link to this post

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

  • Mo
    Avatar
    Community Member
    508 Posts

    Re: Generator HTML Tag Link to this post

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

    Cheers Willr!

    Mo

  • pietro
    Avatar
    Community Member
    18 Posts

    Re: Generator HTML Tag Link to this post

    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.

  • edk
    Avatar
    Community Member
    39 Posts

    Re: Generator HTML Tag Link to this post

    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

  • Tonyair
    Avatar
    Community Member
    81 Posts

    Re: Generator HTML Tag Link to this post

    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

  • inCharge
    Avatar
    Community Member
    102 Posts

    Re: Generator HTML Tag Link to this post

    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...

  • Bastir
    Avatar
    Community Member
    8 Posts

    Re: Generator HTML Tag Link to this post

    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

    8387 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.