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

Shortcode for dataobjects


Go to End


4 Posts   1203 Views

Avatar
mi32dogs

Community Member, 75 Posts

2 April 2015 at 1:56pm

Edited: 02/04/2015 1:57pm

Hi, I have a dataobject with Points of interest called PointOfInterest.

Now I wand the client to be able to add it to a page with a shortcode

So in the _config.php I added

ShortcodeParser::get('default')->register('FeaturedPOI', array('StandardPage', 'FeaturedPOI'));

Then to the StandardPage.php I added

class StandardPage extends Page {

  public static function FeaturedPOI(){
    $PointOfInterest = PointOfInterest::get()
      ->filter(array(
        'Active' => true,
        'Featured' => true,
      ));
    return $PointOfInterest->renderWith('FeaturedPOI');
  }
}

And I created a FeaturedPOI.ss Include

<% loop $PointOfInterest %>
<div class="prest_cont">
    <img src="$Photo.URL" class="img-responsive pull-left img-rest">
    <div class="prest_block">
        <div class="Name">$Name</div>
        <Address>
            $Address, $City, $State $Zip<br>
            $Phone<br>
            <% if $Website %>Website: <a href="$Website" target="_blank" class="dot_under">click here</a><% end_if %>
        </Address>
        <div class="Disc">$Description</div>
    </div>
</div>
<% end_loop %>

The shorcode works it runs the FeaturedPOI function
If I do a Debug::show($PointOfInterest); within the FeaturedPOI function I see that it has 3 items
I see it gets the template but the look in the template is not working.

Where do I go wrong? Any ideas?

Avatar
Kirk

Community Member, 67 Posts

2 April 2015 at 4:52pm

First thing I would try is to put some debugging in the template to see what is happening.
So inside the loop in FeaturedPOI.ss put a debug variable in there and hopefully that will give you more info on what is being returned to the loop

<% loop $PointOfInterest %>
$Debug

Avatar
mi32dogs

Community Member, 75 Posts

2 April 2015 at 5:08pm

Thanks Kirk, I was looking for something how to debug it within the tempate.

doing this i get:
Name:PointOfInterest
Table:
Value:

but if i put it before the loop

$Debug
<% loop $PointOfInterest %>

i get the 3 records like:
DataList
ClassName =PointOfInterest
Created =2015-03-31 07:16:45
LastEdited =2015-03-31 07:16:45
Active =1
Featured =1
Name =Il Capriccio
Address =1757 N. Vermont Ave.
City =Los Angeles
State =CA
Zip =90027
Phone =(323) 662-5900
Description =<p>Il Capriccio on Vermont, the most popular Italian restaurant with locals, is well known for great Italian food. Small and quaint with a nice front patio. True to its Italian tradition, Il Capriccio makes all of its pasta, bread and desert pastry on site daily. Inspired with the Renaissance flavors of Southern Italy, Il Capriccio on Vermont is Italian food in Los Feliz at its best.</p>
Website =http://www.dineandride.com/restaurants.php?restaurantID=52
Type =Restaurant
SubType =Italian
SortOrder =0
PhotoID =18
ID =1
RecordClassName =PointOfInterest

it looks like the data is there it just does not wand to loop

Avatar
Kirk

Community Member, 67 Posts

6 April 2015 at 2:36pm

Sounds like you need to remove the loop from the include and put it where the include is something like this in the Page.ss (or whatever the template is called)

<% loop $PointOfInterest %>
<% include FeaturedPOI %>
<% end_loop %>