Jump to:

3401 Posts in 979 Topics by 855 members

Template Questions

SilverStripe Forums » Template Questions » Need different <% base_tag %> for development and production

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

Page: 1
Go to End
Author Topic: 1548 Views
  • Laurie
    Avatar
    Community Member
    21 Posts

    Need different <% base_tag %> for development and production Link to this post

    Hi -

    I'm running SS on a development box and then writing out a static cache of the pages for our production box. URL for the site on the development box is www.site.mirror and for the production box is www.site.com. The problem is that the <% base_tag %> being written into the cached pages is for our development URL, not our production URL. Is there a way to conditionally switch its value or to suppress it altogether when the pages are published statically? Thanks!

    Cheers,
    Laurie

  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Re: Need different <% base_tag %> for development and production Link to this post

    I don't have any code to offer but you could write something that checks for whether the site is in dev or production mode, and writes the local url for dev mode, the production url for production mode. Then simply switch between the modes when you need to publish the files.

    Best idea I have off the top of my head at the moment.

  • Ryan M.
    Avatar
    Community Member
    309 Posts

    Re: Need different <% base_tag %> for development and production Link to this post

    Update... I thought I'd write some quick and dirty code for you.

    Put this in Page.php

    public function BaseHref() {
    if(Director::isDev()) { return "http://localhost:8000/"; }
    if(Director::isLive()) { return "http://your-production-domain.com/"; }
    }

    Then remove <% base_tag %> from your template and write your own using $BaseHref in the href attribute.

    Warning, this hasn't been tested. Use at your own risk.

  • Laurie
    Avatar
    Community Member
    21 Posts

    Re: Need different <% base_tag %> for development and production Link to this post

    Thanks Ryan! I was able to get this to work with a few tweaks. For some reason, naming the function BaseHref didn't work (perhaps because $BaseHref already exists and I wasn't overriding something properly)...in any event, I went with MyHref and everything worked beautifully. I also added some code to RebuildStaticCacheTask.php to switch between dev and live so I can automate the refresh of the cache someday.

    So, in summary...

    1. Added the following function to ...\mysite\code\Page.php

       function MyHref() {
          if(Director::isDev()) { return "http://www.mysite.mirror/"; }
          if(Director::isLive()) { return "http://www.mysite.com/"; }
       }

    2. Changed <% base_tag %> to<base href="$MyHref"></base> in Page.ss

    3. Added the following lines of code near the top and bottom, respectively, of the function rebuildCache in RebuildStaticCacheTask.php
       Director::set_environment_type("live");
       Director::set_environment_type("dev");

    And, voila, the correct base href for each situation. Thanks again.

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

    Re: Need different <% base_tag %> for development and production Link to this post

    Just out of curiosity could you use $BaseHref? it is a function that works fine to do the job you require I think... it works on my localhost and ip addresses and proper.com's... I'm not sure about the statically published thing tho - never done that

  • Laurie
    Avatar
    Community Member
    21 Posts

    Re: Need different <% base_tag %> for development and production Link to this post

    $BaseHref does resolve to the http://www.mysite.mirror/ address (as that is address configured in my SS installation). The problem I was having was switching that to different address when I wrote out the static pages.

    1548 Views
Page: 1
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.