Jump to:

3434 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » Keep url segment consistent to render with alternative template?

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

Page: 1 2
Go to End
Author Topic: 3008 Views
  • LisaB
    Avatar
    Community Member
    28 Posts

    Keep url segment consistent to render with alternative template? Link to this post

    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!

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: Keep url segment consistent to render with alternative template? Link to this post

    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

  • LisaB
    Avatar
    Community Member
    28 Posts

    Re: Keep url segment consistent to render with alternative template? Link to this post

    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.

  • LisaB
    Avatar
    Community Member
    28 Posts

    Re: Keep url segment consistent to render with alternative template? Link to this post

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

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: Keep url segment consistent to render with alternative template? Link to this post

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

  • LisaB
    Avatar
    Community Member
    28 Posts

    Re: Keep url segment consistent to render with alternative template? Link to this post

    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.

  • martimiz
    Avatar
    Forum Moderator
    1067 Posts

    Re: Keep url segment consistent to render with alternative template? Link to this post

    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();
    }

  • LisaB
    Avatar
    Community Member
    28 Posts

    Re: Keep url segment consistent to render with alternative template? Link to this post

    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!

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