Jump to:

23371 Posts in 18161 Topics by 2865 members

General Questions

SilverStripe Forums » General Questions » [SOLVED] A question of <% control %>

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1 2
Go to End
Author Topic: 710 Views
  • ambient
    Avatar
    Community Member
    123 Posts

    [SOLVED] A question of <% control %> Link to this post

    Hi All,

    I'm having an issue here which I thought was simple enough but for some reason isn't working for me.

    I have a page with 5 subpages. I want the title on the mainpage to be 'All Listings' instead of $Title but for the sub pages I want the title to be '$Title'

    This is my code now

    <% if Level(1) %>
    <% control Level(1) %>
    <h2>All Listings</h2>
    <% end_control %>
    <% end_if %>

    <% if Level(2) %>
    <% control Level(2) %>
    <h2>$Title</h2>
    <% end_control %>
    <% end_if %>

    I've tried it without the 'if' and I've tried it without the 'control'
    but I get the same result which is on the subpages both titles appear. The 'All Listings' and the '$Title'

    I've checked the 'Built-in Page Controls' documentation here http://doc.silverstripe.org/sapphire/en/reference/built-in-page-controls
    but can't see what I might be doing wrong?

  • Scott Farmer
    Avatar
    Community Member
    35 Posts

    Re: [SOLVED] A question of <% control %> Link to this post

    I'm guess that returns another page title.

    Have you tried this:
    <% control Parent %> or $Parent.Title, $Parent.Content, etc

    Cheers
    Scott

  • ambient
    Avatar
    Community Member
    123 Posts

    Re: [SOLVED] A question of <% control %> Link to this post

    Hi Scott,

    Using <% control Parent%> still affects the child/sub pages.

    Using only <% control Level(2) %> gives the desired effect by not changing the parent or first level page
    But then when I try <% control Level(1) %> it again affects all the pages, parent and child

    If anyone has a solution for this I'd really appreciate it.

    Thanks

  • ambient
    Avatar
    Community Member
    123 Posts

    Re: [SOLVED] A question of <% control %> Link to this post

    Can anyone help with this pleeeeease...

    I just don't understand why when I use <% if Level(1) %> or <% control Level(1) %> it affects 2nd level pages

  • swaiba
    Avatar
    Forum Moderator
    1784 Posts

    Re: [SOLVED] A question of <% control %> Link to this post

    Hi ambient,

    I cannot be sure but... silverstirpe does cache it's responses to calls within the template engine... I am not sure if have a different function parameter changes things.

    Search I only found this (http://www.silverstripe.org/template-questions/show/16374) I am not self-aggrandising but I hope this helps.

    Barry

  • Scott Farmer
    Avatar
    Community Member
    35 Posts

    Re: [SOLVED] A question of <% control %> Link to this post

    Have you trued /?flush=all

    Can you post more of your code, page names and directory located in?

    Thanks
    Scott

  • JonoM
    Avatar
    Community Member
    103 Posts

    Re: [SOLVED] A question of <% control %> Link to this post

    Hi Ambient,

    As far as I know the Level(x) control will always return details for the page at that level if it can find it - so if you're on a page that's 5 levels deep, you will get a positive result for Level(1), Level(2) etc. through Level(5).

    You're getting both titles on your subpage because the Level(x) functions are working correctly and returning positive values. On the parent page there is no second level page available so you're only getting the top level page details.

    You don't need to use a Level(x) control to get the details for the page you're on, and there's no reason to call <% control Level(1) %> in your template if you don't need any variables from the page at that level (as in your example, where you are using a hard-coded title). So how about trying this format:

    <% if Level(2) %>
       <h2>$Title</h2>
    <% else %>
       <h2>All Listings</h2>
    <% end_if %>

    Your parent page will return false for Level(2) and then fallback to your default heading. Although if I'm clear about what you're trying to do - why not just change your $Title value for the parent page in the CMS to 'All Listings' and then you don't even need to insert any logic, you can just use <h2>$Title</h2>. You can always set the meta title to something different for that page.

  • JonoM
    Avatar
    Community Member
    103 Posts

    Re: [SOLVED] A question of <% control %> Link to this post

    p.s. if you're trying to set up an index parent page that lists a kind of preview of the sub pages under it it might be cleaner to create two different page types so you can have separate templates. i.e. PageHolder and Page. Then you don't have to put a bunch of logic in you're template because you'll have one template for the parent page and one for the sub pages. You can limit the types of page that can go under a page in the CMS and vice versa - see example of BlogHolder / BlogEntry here: http://doc.silverstripe.org/sapphire/en/reference/sitetree#limiting-children-parent

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