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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

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

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

Short code to list dataobjects [solved]


Go to End


3 Posts   1057 Views

Avatar
stallain

Community Member, 68 Posts

6 November 2011 at 3:14am

Hello,

In Page.php :

<?php
class Page extends SiteTree {
...
public static $has_many = array(
'Chapters' => 'Chapter'
);
...

In Chapter.php :

<?php
class Chapter extends DataObject {
...
public static $db = array(
'Title' => 'Text',
'Content' => 'Text'
);
public static $has_one = array(
'Photo' => 'Image',
'MyPage' => 'Page'
);
...

In templates/Includes/Chapter.ss :

<% control Chapters %>
<h2>$Title</h2>
<img <% control Photo %><% control SetWidth(440) %>src="$URL" height="$height" width="$width"<% end_control %><% end_control %> alt="$Title" />
<p>$Content</p>
<% end_control %>

I would like to enable my client to include the Chapter.ss template wherever he wants in the content editor with a short code (http://www.silverstripe.org/form-questions/show/14225 - http://www.silverstripe.org/general-questions/show/12609).

But I have no idea of what to put in my "shortcodehandler" function in Page.php.

Could anyone give me a hint ? Thank you !

Stan

Avatar
Willr

Forum Moderator, 5523 Posts

6 November 2011 at 1:04pm

SSbits has a good tutorial on using short codes - http://www.ssbits.com/tutorials/2010/2-4-using-short-codes-to-embed-a-youtube-video/ and rendering into a template.

Avatar
stallain

Community Member, 68 Posts

6 November 2011 at 3:18pm

Thank you Willr !

I finally found a solution that works ; in page.php :

static function chapterShortCodeHandler(){
	$current = Controller::curr(); 
	return $current->renderWith('Chapter');
	}