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

Keep url segment consistent to render with alternative template?


Go to End


16 Posts   5586 Views

Avatar
LisaB

Community Member, 28 Posts

11 January 2011 at 7:01am

Hi all,

I have part of my site as an article repository similar to the news area from tutorial 2. I am using renderwith, dependent upon a url segment, to render the pages with an alternative template and css file if /frame is added to the end of the url and this is working okay. The reason I am doing this is so that I can embed just the content area and sidebar menu of these pages in another site using an iframe, with styling to match the site they are embedded in, as well as displaying them on the main site as standard pages.

The problem I have is that when I use the sidebar menu to navigate to a different article or category I am losing the alternative template and css, because of course the menu link doesn't have /frame appended to it.

Can anyone help me with how to keep the template the same from one page to the next so that if I am on the main site, the pages will render as normal but if the page was reached with a /frame url segment, any pages you navigate to from that page render with the same template? I'm sure this shouldn't be tricky but at the moment I can't work it out. Any help much appreciated!

Avatar
martimiz

Forum Moderator, 1391 Posts

12 January 2011 at 9:39am

If the only purpose of the /frame urlsegment is for your Page_Controller to decide which template/css to use, you could just add '/frame' to the end of your links in the alternative template.

If /frame triggers other things as well, that you don't want to happen over and over, you might instead add something like ?framed=1 to your alternative template links and check on that in you Page_Controllers index() function...

Hope this helps

Avatar
LisaB

Community Member, 28 Posts

13 January 2011 at 6:00am

Edited: 13/01/2011 6:01am

Thank you for your help Martimiz.

Your first suggestion is what I am trying to do, but where I am running into a small problem is trying to add the /frame onto the end of menu links that are automatically created from the pages. I have created a new sidebar include where instead of <a href="$Link" for the menu links I have put <a href="$Link/frame" - this works to keep the links on the "frame" template and css as I was hoping. One minor snag, it creates links with //frame instead of /frame on the end - any idea on how I can rid of the double slash? Obviously it doesn't work with <a href="$Linkframe"...

The second problem I'm having (sorry to bombard you with questions!) is the way I am getting the two templates to render the page with - I thought it was working okay but now I have tried a few page types I realise it's not. I am trying to use this:

	function frame() {
		return $this->renderWith(array('$ClassName', 'FramePage'));
	}

in page.ss because I want pages with the /frame url segment to render with the template for their own page type first, then with the FramePage template to give the alternative surround and css, but the template for the page type is not being picked up. Is there a way I can achieve what I am trying to do in this kind of way, so I don't have to duplicate all the layout templates or create a whole new theme?

Thanks again for any help.

Avatar
LisaB

Community Member, 28 Posts

13 January 2011 at 6:41am

Edited: 13/01/2011 6:55am

UPDATE

Have the second part of my question sorted. Realised I was being stupid and using $ClassName as if it was in a template, when actually it was in a controller, so the following picks up both the templates I want no problem:

	function frame() {
		return $this->renderWith(array($this->ClassName, 'FramePage'));
	}

Also, although I have the menu links sorted apart from the double slash annoyance (I have tried <a href="$Link/frame" and <a href="{$Link}frame", but both give the double slash) I'm not sure how to add the /frame to the end of all the links on the page - I have content links too that need the /frame on the end if the page is being viewed as a frame, but not if it isn't...

Avatar
martimiz

Forum Moderator, 1391 Posts

16 January 2011 at 5:13am

I've been doing things like <a href="{$Link}something/"> in my templates all the time, and have never had them turn into double slashes, so I don't really understand that. :-( You shure the template was properly flushed?

The other thing is about the links in the content... At the moment I can only think of parsing the content, doing some preg_replace...

Good luck!!

Avatar
LisaB

Community Member, 28 Posts

24 February 2011 at 3:29am

Hi All, I still haven't solved this $Link thing and am just coming back to it.

As you will see from my earlier posts I am using the following in my Page controller to render pages with their normal layout template and content on a different top level template with different css. This is so I can display my site in an iframe on another site and make it match the site.

	function frame() {
		return $this->renderWith(array($this->ClassName, 'FramePage'));
	}

The problem I had was that all pages linked to using $Link in the templates rendered using the normal page template, because they didn't have the /frame on the end of the url to trigger the alternative template. So, where the current page has /frame on the end, I want all the links to also have it, so that you stay on the same template when browsing from page to page via the links.

I am trying to use something along these lines to override the Link() funtion:

 	function Link() { 
 		if(URLSegment == 'frame') {
			return Link() . 'frame';
		} else {
			return Link();
		}
	}

But however and wherever I use it it hasn't worked. It looks like the URLSegment isn't picking up the /frame part on the end of the URL but the part before it, so it is always just the normal link that is returned. This is confusing me because the frame() method above takes a URL Segment
and works fine.

Can anyone shed any light? I've read loads and tried loads of things but am at a bit of a loss! Any help much appreciated as always.

Avatar
martimiz

Forum Moderator, 1391 Posts

26 February 2011 at 9:34am

Hi there, sorry to see you´re still struggling...

URLSegment refers to the original URLSegment of the page, the way it is stored in the database, so adding /frame to the end of the URL will not alter the value of URLSegment itself.

In this case /frame provides an action, which you can check on using $this->urlParams['Action']. Something like this? (untested):

      if (isset($this->urlParams['Action']) && $this->urlParams['Action'] == 'frame') {
         return Link() . 'frame';
      } else {
         return Link();
      } 

Avatar
LisaB

Community Member, 28 Posts

2 March 2011 at 8:14am

Thanks again Martimiz, and sorry to keep bothering you - usually I can work this stuff out for myself using the docs and forums but this is currently a bit beyond me :-( !

I have (among other things I have tried,) put your code inside a Link() function in my page controller and it is working great to detect whether or not I am on a page with /frame appended to the URL, however my return statements are not returning the link - return Link(); is empty and return return Link() . 'frame'; just returns "frame".

Obviously $Link returns the correct link from wherever you are - in whatever template, inside child control loops etc, so what I was looking to do is to take $Link (whatever the link would have been from wherever is used in the templates) and conditionally append "frame" to it based on your url params test, but I'm not sure if it's possible to do this in one place and have it work site wide. I can get it to work if I specify what I want the link to be eg return $this->Children()->First()->Link(); but this kind of defies the point of trying to add "frame" to the existing link. Can you point me in the right direction to get this to work throughout?

Many thanks!

Go to Top