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.

Customising the CMS /

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

DataObject as Page - Not passing variables?


Go to End


5 Posts   1345 Views

Avatar
Parker1090

Community Member, 46 Posts

22 May 2015 at 12:15am

Edited: 22/05/2015 12:17am

Hello :)

I don't do a huge amount of SS developing (which is a shame really), so I'm hoping someone can shed some light on this.

I have a data object which is for a blog, and I have a blog page which obviously displays the blog posts. In the BlogPage file, in the controller, I have the following:

	public function display() {
		$params = $this->getURLParams();
		$slug = $params['ID'];
		
		$article = Blog::get()->filter(array(
			'Slug' => $slug
		));

		$count = $article ? $article->Count() : 0;

		if($count == 1) {
			return array("Article" => $article);
		} else {
			$errorPage = DataObject::get_one('ErrorPage');
			$this->redirect($errorPage->Link(), 404);	
		}
	}

However, the following in the template simply doesn't work:

                    <% with $Article %>
                        $Slug - $Title - $Post
                    <% end_with %>

All of those are correct variables defined in the DataObject file, but they all output nothing. The page is correct (not seeing the 404), but still no blog details.

Any help appreciated :)

Avatar
Pyromanik

Community Member, 419 Posts

22 May 2015 at 3:27am

You probably have a scope issue, or you've put it into the wrong tempalte.
By scope I mean, you need to be at page level before <% with Article %> will work. If you're already in some form of control the variable 'Article' will not exist, and the output will not happen.

Alternatively you can try $Article.Title, etc. This way you don't need the with control, and you'll see the hyphens ('-') between the variables. If you can see these, you've got the right file, but the output is blank for some reason, if you cannot see them, you might not be editing the right file (or need to ?flush).

Avatar
Parker1090

Community Member, 46 Posts

22 May 2015 at 3:38am

H Pyromanik,

Thanks for your reply. As far as I can see, being out of scope shouldn't be an issue. Literally, my full page is as follows:

<% include Header %>
        <div class="container white-bg">
            <div class="row">
                <div class="col-xs-12">                  
                        $Article.Slug - $Article.Title - $Article.Post
                </div>
            </div>
<% include Footer %>

As you can see, I've tried $Article., which doesn't seem to help either, even trying it with the <% with $Article %> surrounding it. I am however seeing the dashes, so it is loading the right template

Avatar
Pyromanik

Community Member, 419 Posts

22 May 2015 at 4:18am

Oh duh, silly me.
You've got a list, not a single object.
<% with $Article.First %> or: return ['Article' => $article->first()];

Avatar
Parker1090

Community Member, 46 Posts

22 May 2015 at 5:43am

No, not silly you at all! That works :)

Thank you!