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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

Need different <% base_tag %> for development and production


Go to End


6 Posts   4141 Views

Avatar
Laurie

Community Member, 21 Posts

2 October 2010 at 3:11pm

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.

Community Member, 309 Posts

2 November 2010 at 11:40am

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.

Community Member, 309 Posts

2 November 2010 at 11:52am

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

Community Member, 21 Posts

23 November 2010 at 8:57am

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

Forum Moderator, 1899 Posts

23 November 2010 at 10:23am

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

Community Member, 21 Posts

23 November 2010 at 10:44am

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