Silverstripe 3.2 reusable blocks

I created a new page type that needs to break something down. I created a strapline block that I would like to use in 3 places on the page, however I want to create only 1 version of the strapline block and manage dynamic data with this content.

I have in Straplines.php

following:
class Straplines extends DataObject{ private static $db = array( 'Title'=>'Text', 'Content'=>'HTMLText', 'SortOrder'=>'Int' ); private static $has_one = array( 'Parent'=>'Page' ); private static $default_sort = 'SortOrder'; public function getCMSFields(){ $fields = parent::getCMSFields(); $fields->addFieldToTab("Root.Main", new HtmlEditorField('Content','Content')); $fields->addFieldToTab("Root.Main", new TextField('Title','Title')); return $fields; } } 

Then add the cms fields to HomePage.php. I can add straplines without problems, and they all appear. Then in HomePage.ss I have the following

 <% include PricesBlock %> <% include TourStraplineBlock %> <% include QuickFacts %> <% include TourStraplineBlock %> 

But then I can not figure out TourStraplineBlock to get separate content for each of them. Of course, there should be no parameters to parameterize include or not to create several templates. I am completely new to Silverstripe dev and find it difficult to create reusable content.

Edit: This is the strapline.ss template that handles the display.

 <div class="strapline"> <% loop Straplines %> $Content <% end_loop%> </div> 

As you probably can guess, if I put this twice, it will just show all the straplines. I want to do something like

 <% include Strapline?id=1 %> 

Then we decode it in Strapline.ss and go from there.

Sorry, yes, its 3.2 is not 3.0. I suggested that they are very similar.

+5
source share
3 answers

As mentioned in Firesphere, you can simply create a function that you can call in your template and pass the object identifier to it.

Put this function in your Page_Controller class or if you want to access it only from the home page, and do not put it in your HomePage_Controller class.

 public function StraplineByID($id) { $strapline = Straplines::get()->byID($id); if($strapline) { return $strapline; } } 

In your template, you can now use this:

 <% if $StraplineByID(1) %> <% with $StraplineByID(1) %> $Title, $Content, $WhatEver or an include <% else %> Can't find a strapline with this ID <% end_with %> <% end_if %> 

If you want to define straplines not by id, but by name (this would be more convenient for the user), you must create a new field called "Name" or something like that, and use it instead of id. But note that the values ​​in the Name field must be unique

 public function StraplineByName($name) { $strapline = Straplines::get()->find('Name', $name); if($strapline) { return $strapline; } } 

and in your template

 <% if $StraplineByName(prices) %> <% with $StraplineByName(prices) %> $Title, $Content, $WhatEver or an Include <% else %> Can't find a strapline with this Name <% end_with %> <% end_if %> 

The code is not verified, but it should work;)

+5
source

You can make a function on a page that takes an argument as a literal and returns the desired strapline. eg.

 <% $currentStrapline(1) %> 

and functions on the page,

 public function currentStrapline($id) { // returns the wished strapline or null; } 
+4
source

Yes, there is a way to parameterize include, and you almost got it:

 <% include Strapline MyVar=$someVar, MyVar2=$someVar2 %> 

Then in Strapline.ss :

 <% if $MyVar == 1 %> Say One <% else_if $MyVar2 == 2 %> Say Two <% end_if %> 
0
source

Source: https://habr.com/ru/post/1238683/


All Articles