PHP `include()`s in Blueprints

Can one use php include() to share common blueprint fields across a set of similar blueprints?

1 Like

@FabianSperrle wanted to try this: Blueprint Snippets

Why you don’t just try it and see, if it works? :smiley:

@distantnative: I did, but it did not work. Maybe I’m doing something wrong though. Here’s an example blueprint:

<?php if(!defined('KIRBY')) exit ?>

title: Page
pages: true
fields:
  title:
    type: text
    label: Name
    placeholder: Used in navigation and lists.
  opening:
    <? include(kirby()->roots()->site() . DS . 'panel' . DS . 'blueprints' . DS . 'scaffolds' . DS . 'related_pages.php'); ?>
files: false

And the file getting included:

    type: structure
    label: Related Pages
    fields:
      page_uri:
        label: Page
        type: select
        options: query
        query:
          page: containers
          fetch: children
          value: '{{uri}}'
          text: '{{title}}'
    entry: >
      <a href="/panel/#/pages/show/{{page_uri}}/p:1">{{page_uri}}</a>

Any ideas?

Just tried it myself, but it doesn’t seem to work with an include :confused:

It’s probably due to the fact that include() interprets the file getting included as PHP code; and it is YAML. Right, @bastianallgeier?

This would be a lovely feature!

I just tried this too, but the blueprint file does not seem to execute any custom php inside it, just ignores it.

You could use text pre-processing outside kirby for parsing blueprints from a shared source. Like using grunt and a watcher to append the shared fields at the end of blueprints. I think I’ll try that.

Some people would like to use .yaml files instead of .php for blueprints. That would make include() definitely impossible, but not the feature itself. I can imagine an “import” option like:

fields:
  title:
    label: title
    type: text
  import: my_nice_field
…

Wondering when that would actually be useful. And it’s yet another layer of abstraction.
So far I’m happy with cutting & pasting, but I’ve don’t have that many shared fields. Mainly the Visual Markdown Editor field in 6 blueprints, and with the exact same options in only 4 of them.

One could add a function to index.php which generates all the blueprints based on a template folder using mustache. Or less hacky one could request a new panel hook from @bastianallgeier which gets called ‘on opening new page’ popup.

@bastianallgeier and his team seams to have listened; blueprint snippets are there at your service: Tutorial: Global field definitions aka blueprint snippets Have fun, M.