1383 Posts in 492 Topics by 521 members
Page: 1 2
|Go to End||Next >|
28 October 2009 at 11:23am Last edited: 28 October 2009 11:31am
I am looking for a way to get the homepage of a site so I can retrieve some values from it.
At http://doc.silverstripe.org/doku.php?id=recipes:final_parent is a function to find the final parent of a page - I got this to work and was able to get values from it to appear in my pages. It does not get me to the homepage, but I think it is a good start.
At http://doc.silverstripe.org/doku.php?id=recipes:what_to_return_from_a_function there is another recipe to get the results from a function from a specific page but ideally I would like to be able to do this without targeting a page called 'home' because I am working on a multi-language website.
What do I want to do: i want to get certain values from the homepage in each language to appear in the footer on every page in that language. (I would be using a special homepage template that has some extra fields for contact info, a short statement etc). I do not believe widgets are the answer to my problem because you have to apply them to every page manually and even if you can apply them to all the pages in the site, you need to fill in the data manually on every page. But please correct me if I am wrong - I am new here.
28 October 2009 at 12:33pm
The way you are suggesting (top parent) is very bad and intensive on the server for what you want to do. I think it calls the parent from the database every time! So this could be a huge amount of DB hits for each page load.
If you want to get the home page, there are two ways.
1. If no HomePage page type:
$homepage = DataObject::get_one('Page','URLSegment = "home"');
$homepage->Content; //is the content of the homepage...
NB: The home page always has the URLSegment home.
2. if you have a homepage page type:
$homepage = DataObject::get_one('HomePage'); //where HomePage is the class name of the homepage page type
If you want to get it based on language, then look that the multilingualcontent documentation for how to get dataobjects by language: http://doc.silverstripe.org/doku.php?id=multilingualcontent#usage1
28 October 2009 at 12:58pm
Or if you want to get the values for something in the template you can do this
<% control Page(home) %>
<% end_control %>
28 October 2009 at 8:53pm
Hi Pigeon and Willr,
Thanks for the answers! It sounds relatively simple and even fun:-)
@Pigeon: Regarding your second method: is that still useful if there are more pages based on the same page type? To me it sounds as if you can never be sure what you get with this method.
@willr: control Page(home): is 'home' based on the name of the item in the menu or on the urlsegment?
28 October 2009 at 8:57pm
yurigoul - control Page() is given a URLSegment
28 October 2009 at 9:19pm
30 October 2009 at 11:08am
I'm sorry, but two of the three methods offered here give me the right results right now but they do not get me the page that has the function of the Homepage - that is: being at the top of the tree and therefore the landing page of a site.
When I am in silverstripe and I change the value of the Page Name field on the Content.Main tab, Silverstripe asks me if I want to change the link name. If I do this on the home page and press ok, then that page remains the homepage but the two methods that give me the right result stop working.
I could not get the third method to work (DataObject::get_one('HomePage') and even if I got it to work it does not sound idiot proof to me. The third method assumes - as far as I can tell - that there is only one page that uses the HomePage page type.
As long as the name of the home page remains the same everything is ok - they work like charm, I have to admit that. But the thing is that I can not be sure what my users do, especially with a site that is several years up and running and I am just taking that well earned holiday or in the middle of a big assignment ...
The only other idea I can come up with at the moment is creating a special page that can not appear in the menu and has the name footer and then get the third method to work.
30 October 2009 at 11:27am Last edited: 30 October 2009 11:33am
Sorry for being rather anonymous since my first post.
You are indeed correct that if you had more than one HomePage type page then my second method could return unexpected results. I do make an assumption that there will only be one (and indeed enforce it) so that the CMS users can't make home pages.
Changing the URL of the homepage is not a good idea, because if a /dev/build is done and no page has the URL 'home' it creates one for you (and it takes the place of the homepage). See http://silverstripe.org/general-questions/show/260421
I personally never like to rely on using the URL segment to get me some info from a page because - like you say - the user can change them. However, for the homepage type, you could remove the URLSegment input box so they can't edit it.
It's a tricky one, but something that you should decide on based on your needs.
Page: 1 2
|Go to Top||Next >|