Jump to content

Custom functions per page (single template)


_NameLess_
 Share

Recommended Posts

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_

Link to comment
Share on other sites

you could just have each function be in a named file inside a directory which matches the name of the manufacturer, for your output.

then you can use your code editor more easily than having code stored in the DB.

e.g.

include('/manufacturers/' . $car->CarManufacturer->name . '.inc';

or you can make a textarea field and use AceExtended in PHP mode, then eval() your code on the front end.

Link to comment
Share on other sites

Hi Macrura,

I'd rather enter the code through the admin panel anyways because I want all the information of the manufacturer on one page without having to open another php file.

The code is relatively short anyways so there is no real need for a whole file. :)

The ACE Extended plugin you mentioned is a nice touch, though not strictly necessary i assume.

Thanks you for your suggestions, seems like there is no other way than writing my own fieldtype.
Link to comment
Share on other sites

I don't understand, what would that fieldtype have to offer instead of a regular textarea field? Like Macrura said, you can use PHP's eval() construct in the template file to run any PHP code you typed in that field in the admin area. If you're aware of the potential for disaster this is the way to add code via a field in the admin. Example here: https://processwire.com/talk/topic/10873-which-field-to-use-for-custom-codemarkup/

Link to comment
Share on other sites

It's easier to use that way.
In the template I only have to type this:

<?php
$SomeCarData = $page->whatever;
$page->CarManufacturer->CustomFunctions->OutputCarData($SomeCarData);
?>

 
Instead of doing everything myself:
 

<?php
$ClassName = SomeNonConflictingClassName;
$Code = "class " . $ClassName . " {\r\n";
$Code .= $page->CarManufacturer->CustomFunctions;
$Code .= "\r\n}";
eval($Code);
$CustomFunctions = new $ClassName();
$SomeCarData = $page->whatever;
$CustomFunctions->OutputCarData($SomeCarData);
?>

The FieldType could handle all that. And I'd only have to write that code once and not everywhere I need the custom functions.
 
Thanks again. :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...