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.

Template Questions

Need different <% base_tag %> for development and production


Reply

6 Posts   1703 Views

Avatar
Laurie

2 October 2010 at 3:11pm Community Member, 21 Posts

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

Avatar
Ryan M.

2 November 2010 at 11:40am Community Member, 309 Posts

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.

Avatar
Ryan M.

2 November 2010 at 11:52am Community Member, 309 Posts

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.

Avatar
Laurie

23 November 2010 at 8:57am Community Member, 21 Posts

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.

Avatar
swaiba

23 November 2010 at 10:23am Forum Moderator, 1796 Posts

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

Avatar
Laurie

23 November 2010 at 10:44am Community Member, 21 Posts

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