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

Access $Link from a custom function


Go to End


3 Posts   1494 Views

Avatar
Josiah

Community Member, 7 Posts

31 October 2013 at 8:24am

Edited: 31/10/2013 8:25am

Hello, I'm building a one page site that is put together by pulling in other pages in the site on the home page. These other pages have custom styles that are managed through the CMS and I'm targeting them by placing a 'data-path="$Link"' attribute on each page's section in the home page template (example output: <section data-path="/portfolio/first-entry/">)

I need to output a style block in my template that targets the data-path attribute to give that section custom styles. Here's the starter code I have for the function I'm calling into my template file:

	function CustomStyles() {
		$pages = Page::get();

		echo '<style type="text/css">';
		foreach($pages as $page) { 
			echo '[data-path="' . $page->Link . '"]{}';
		}
		echo '</style>';
	}

However, $page->Link doesn't output anything because there isn't a Link property in the $page array. How can I access the same $Link property in my custom function that is used in the templating engine?

Avatar
Devlin

Community Member, 344 Posts

31 October 2013 at 9:26pm

Edited: 31/10/2013 9:27pm

Link is a method. You need to use:

$pages = Page::get();
foreach($pages as $page) {
         echo $page->Link();
} 

Avatar
Josiah

Community Member, 7 Posts

1 November 2013 at 6:15am

Duh. I don't know what I was thinking, lol. Thanks!