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.

General Questions /

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

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

Short code to list dataobjects [solved]


Reply


3 Posts   568 Views

Avatar
stallain

Community Member, 61 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 ([url]http://www.silverstripe.org/form-questions/show/14225[/url] - [url]http://www.silverstripe.org/general-questions/show/12609[/url]).

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, 5513 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, 61 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');
   }