Jump to content

Multiple templates for one page, like PHP traits, or "granular" templates?


martin
 Share

Recommended Posts

I'm wondering if there's a clean way I can implement a granular templates system for a web application I want to build. By granular templates, I mean that you can apply more than one template to a page, and all fields from all applied templates will be made available.

I want something similar to this:

House (page) implements Basic-House, Garage, Garden (templates)

or

Vehicle (page) implements Basic-Car, Diesel-Engine, Turbo-Engine, Four-Wheel-Drive (templates)

The simple (already available) option is to use the page's children as the "granules", but it means having 2-4 children per item just to implement extra fields. I would rather some sort of inline solution.

Thanks in advance for any suggestions.

Link to comment
Share on other sites

There's currently no way to implement this. Each template does have a single fixed set of fields. That is at least the answer to the backend structure. What you can do is provide a own php class (subclass of Page) to the "parent" entity. This class could then populate itself at runtime with the fields of those childpages you mentioned. This way you could at least maintain the structure in your code. The option to set such a class is in the template settings if you enable advanced mode. Maybe this could even be done with hooking the Page class, and without using the advanced mode settings. 

Link to comment
Share on other sites

I mean that you can apply more than one template to a page, and all fields from all applied templates will be made available.

Is it needed for the Backend Editmode? What's about to simply have one template with some fieldsets, each fieldset contains a few fields (those you mentioned in your subtemplates) and the visibility of the fieldsets are bound through checkboxes and the onlyShowIf settings for templates?

| Select granule Subtemplates:                                                                |
|                                                                                             |
| [ ] use Basic-Car | [ ] use Diesel-Engine | [ ] use Turbo-Engine | [ ] use Four-Wheel-Drive |
|                                                                                             |

| Select granule Subtemplates:                                                                |
|                                                                                             |
| [x] use Basic-Car | [ ] use Diesel-Engine | [ ] use Turbo-Engine | [ ] use Four-Wheel-Drive |
|                                                                                             |
|--- Basic car -------------------------------------------------------------------------------|
|                                                                                             |
|  BC_textfield_1            BC_ceckbox_1            ...                                      |
|                                                                                             |
|---------------------------------------------------------------------------------------------|

| Select granule Subtemplates:                                                                |
|                                                                                             |
| [x] use Basic-Car | [ ] use Diesel-Engine | [ ] use Turbo-Engine | [x] use Four-Wheel-Drive |
|                                                                                             |
|--- Basic car -------------------------------------------------------------------------------|
|                                                                                             |
|  BC_textfield_1            BC_ceckbox_1            ...                                      |
|                                                                                             |
|---------------------------------------------------------------------------------------------|
|--- Four-Wheel-Drive ------------------------------------------------------------------------|
|                                                                                             |
|  FWD_textfield_1            FWD_ceckbox_1          ....                                     |
|                                                                                             |
|---------------------------------------------------------------------------------------------|

-

Pseudocode is like: only show Fieldset_BasicCar if Checkbox_BC is checked

  • Like 2
Link to comment
Share on other sites

There's currently no way to implement this. Each template does have a single fixed set of fields. That is at least the answer to the backend structure. What you can do is provide a own php class (subclass of Page) to the "parent" entity. This class could then populate itself at runtime with the fields of those childpages you mentioned. This way you could at least maintain the structure in your code. The option to set such a class is in the template settings if you enable advanced mode. Maybe this could even be done with hooking the Page class, and without using the advanced mode settings. 

Yes, I thought as much. My backup plan was just to do it in the template for the parent page, selecting all children and find out details about their template and fieldgroups.

Is it needed for the Backend Editmode? What's about to simply have one template with some fieldsets, each fieldset contains a few fields (those you mentioned in your subtemplates) and the visibility of the fieldsets are bound through checkboxes and the onlyShowIf settings for templates?

| Select granule Subtemplates:                                                                |
|                                                                                             |
| [ ] use Basic-Car | [ ] use Diesel-Engine | [ ] use Turbo-Engine | [ ] use Four-Wheel-Drive |
|                                                                                             |

| Select granule Subtemplates:                                                                |
|                                                                                             |
| [x] use Basic-Car | [ ] use Diesel-Engine | [ ] use Turbo-Engine | [ ] use Four-Wheel-Drive |
|                                                                                             |
|--- Basic car -------------------------------------------------------------------------------|
|                                                                                             |
|  BC_textfield_1            BC_ceckbox_1            ...                                      |
|                                                                                             |
|---------------------------------------------------------------------------------------------|

| Select granule Subtemplates:                                                                |
|                                                                                             |
| [x] use Basic-Car | [ ] use Diesel-Engine | [ ] use Turbo-Engine | [x] use Four-Wheel-Drive |
|                                                                                             |
|--- Basic car -------------------------------------------------------------------------------|
|                                                                                             |
|  BC_textfield_1            BC_ceckbox_1            ...                                      |
|                                                                                             |
|---------------------------------------------------------------------------------------------|
|--- Four-Wheel-Drive ------------------------------------------------------------------------|
|                                                                                             |
|  FWD_textfield_1            FWD_ceckbox_1          ....                                     |
|                                                                                             |
|---------------------------------------------------------------------------------------------|

-

Pseudocode is like: only show Fieldset_BasicCar if Checkbox_BC is checked

This is an interesting option... and within 20 minutes I have it working like this. PW continues to impress! I've even managed to use the fieldsets in my PHP template to visually group and perform logic on those granules.

Still needs some more testing but I think it'll work. Thank you :)

Link to comment
Share on other sites

This is an interesting option... and within 20 minutes I have it working like this. PW continues to impress!

The only issue I'd have with it, is that you can't easily reuse fixed groups of fields. You have to set this up for each template on it's own or implement a custom setup process via api. But still better than nothing.

Link to comment
Share on other sites

The only issue I'd have with it, is that you can't easily reuse fixed groups of fields. You have to set this up for each template on it's own or implement a custom setup process via api. But still better than nothing.

Na, there is also PageTable and PageTableExtended available. :)

Link to comment
Share on other sites

The only issue I'd have with it, is that you can't easily reuse fixed groups of fields. You have to set this up for each template on it's own or implement a custom setup process via api. But still better than nothing.

I only have one template and will never need more. The app has a very specific requirement to fill. But yes, it's not perfect.

Link to comment
Share on other sites

@horst

I know that and I actually meant reusing groups of fields in different templates not in a single one. Just like the example in the entry post with have multiple classes which implement the same base classes to share sets of properties / methods.

Link to comment
Share on other sites

The one problem I have now is that, although I can find out which fields are in each fieldset from the template, I don't have any direct link between the fields and the granule checkbox.

Can I access the "show if" value in my PHP template?

Link to comment
Share on other sites

You can't, you need to check that manually. Inputfield dependencies are only evaluated on the frontend by javascript.

But I think the checkbox states are saved with the pages, or not?

if ($pages->checkbox_bc) { // the fields bundled to "Basic Car" are in use

You once have to manually note which fields belong to which subtemplate, (will be good if the fields will not grow in the future)

or you stick with a naming scheme and iterate over all template fields to get that information. Maybe not very elegant or high dynamcly, but a fast way to get the job done. :)

EDIT:

$subtemplateIds = array('bc'=>aray(), 'fwd'=>array());

foreach($page->template->fields as $field) {
    foreach(array_keys($subtemplateIds) as $k) {
        $fn = $field->name;
        $len = strlen($k) + 1;
        if (substr($fn, 0, $len) == $k . '_') {
            $subtemplateIds[$k][] = $fn;
        }
    }
}

// later on you can check like:
if ($page->checkbox_bc) {
    // use also all fields from $subtemplateIds['bc']
}

(written in browser, be careful :)  )

Edited by horst
added example code
Link to comment
Share on other sites

But I think the checkbox states are saved with the pages, or not?

if ($pages->checkbox_bc) { // the fields bundled to "Basic Car" are in use

You once have to manually note which fields belong to which subtemplate, (will be good if the fields will not grow in the future)

or you stick with a naming scheme and iterate over all template fields to get that information. Maybe not very elegant or high dynamcly, but a fast way to get the job done. :)

Schema changes should be extremely rare, but hard-coding the relationships still doesn't sit well with me. I suppose I'll have to go with prefixes on fields to decide their group. :)

  • Like 1
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...