Search the Community
Showing results for tags 'Custom Function'.
-
Hello, as an example imagine this structure: Templates: CarManufacturerTemplate Name: String CarTemplate Name: String Owner: String CarManufacturer: Page(where Template == CarManufacturerTemplate) Pages: CarManufacturers Manufacturer 1 [Template = CarManufacturerTemplate] Name = "Toyota" Manufacturer 2 [Template = CarManufacturerTemplate] Name = "Ford" Cars Car 1 [Template = CarTemplate] Name = "Car 1" Owner = "Steve" CarManufacturer = Manufacturer 1 (Shown as "Toyota") Car 2 [Template = CarTemplate] Name = "Car 2" Owner = "Bob" CarManufacturer = Manufacturer 1 (Shown as "Toyota") Car 3 [Template = CarTemplate] Name = "Car 3" Owner = "Jack" CarManufacturer = Manufacturer 2 (Shown as "Ford") So there are multiple manufacturers, multiple cars, and every car has a reference to its manufacturer. On the page mysite/cars/car-1/ I want to be able to present the data in a manufacturer-specific way but without doing something like this: <?php if ($page->CarManufacturer->Name == "Toyota") { //Show Toyota specific formatting } else if ($page->CarManufacturer->Name == "Ford") { //Show Ford specific formatting } ... ?> Instead I want to be able to give every child-page of the CarManufacturers-page some code which takes the required data as parameters and creates the formatted output on its own. This way, whenever I add a new manufacturer, I don't have to go through all the places which output manufacturer-specific data and add another if-statement. Instead I want to be able to write code on a per-manufacturer basis. A possible way of doing this would be a FieldType which allows me to enter PHP code in the backend. I'd add another field to the CarManufacturer-template: Templates: CarManufacturerTemplate Name: String CustomFunctions: MyCustomTypeWhichExecutesPHP And the backend page for Manufacturer 1 (Toyota) looks like this: |------------- | Name: | |-------- | | Toyota | |-------- | CustomFunctions: | |------------------------------------------------------ | | public function OutputCarData($SomeCarData) { | | //Show Toyota specific formatting | | } | |------------------------------------------------------ And in the template-code of CarTemplate I can just do this: <?php $SomeCarData = $page->whatever; $page->CarManufacturer->CustomFunctions->OutputCarData($SomeCarData); ?> And I don't need to change that code at all when I add another car manufacturer. I just type in another function OutputCarData which does the formatting appropriately. I hope there is a simpler way for this than creating a custom FieldType. If you have any further questions feel free to ask and I will try to answer them or provide more details in what I want to achieve. Many thanks, _NameLess_
- 4 replies
-
- API
- Custom FieldType
-
(and 2 more)
Tagged with: