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.

General Questions /

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

[SOLVED] A question of <% control %>


Go to End
Reply


9 Posts   906 Views

Avatar
ambient

Community Member, 123 Posts

9 November 2011 at 2:50am

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?

Avatar
Scott Farmer

Community Member, 42 Posts

9 November 2011 at 11:02am

I'm guess that returns another page title.

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

Cheers
Scott

Avatar
ambient

Community Member, 123 Posts

10 November 2011 at 12:05am

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

Avatar
ambient

Community Member, 123 Posts

11 November 2011 at 6:19am

Edited: 11/11/2011 6:20am

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 :(

Avatar
swaiba

Forum Moderator, 1805 Posts

11 November 2011 at 8:30am

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

Avatar
Scott Farmer

Community Member, 42 Posts

11 November 2011 at 8:54am

Have you trued /?flush=all

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

Thanks
Scott

Avatar
JonoM

Community Member, 103 Posts

11 November 2011 at 10:55am

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.

Avatar
JonoM

Community Member, 103 Posts

11 November 2011 at 11:03am

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

Go to Top