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.

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   1232 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');
	}