Displaying Modular content from structured fields

Hi all,

I’m using structure fields in a blueprint which works fine using <?php foreach($page->x()->toStructure() as $y): ?>

However I’m stuck when it comes to using modules. The template has a query option to select from a list of pre-created modules. The modules is constructed using Structured fields.

The template content shows only:


module: default-option

The module live in

module folder > module type > module name (as sub pages)

Not sure on the PHP needed to call the selected module, then call the relevant fields…

Any help much appreciated as I’m a proper newb…

Hi @sigsoldboy,

You mean the blueprint, not the template, I suppose? I’d be happy to help, but I must admit that I don’t quite understand …:confused:

What are those modules? Snippets? Or are you talking about the modules plugin?

Yes sorry, Snippets (i’m using the term modules). Not sure of how to render the snippet content selected by the user in the page template.

For example, user has created 3 variants of the snippet snippet 1, snippet 2 and snippet 3. In the _modules folder.

On the page template user select snippet 1.

Snippet code is:

<?php $example = page('_modules/example/') ?>
<section>

  <h2><?= $example->headline() ?></h2>

  <ul>
    <?php foreach($example->panels()->toStructure() as $panel): ?>
    <li>
      <h3><?= $panel->title() ?></h3>
      <p><?= $panel->text() ?></p>
    </li>
    <?php endforeach ?>
  </ul>

</section>

Making any sense?

I’m still having problems, but this is what I think you got:

In your content folder, you have:

content
  - modules
    - module1
    - module2
    - module3

Each of these modules has a corresponding snippet (snippet1, snippet2, snippet3) to render the module content.

In the modules page, the user selects a module; in the resulting content file, you get something like this:

module: snippet1

Now in your modules template (modules.php) you need to call the snippet that corresponds to the selected module?

<?php snippet($page->module()->value()) ?>

Hope I’m not too far away …

Really close :wink:

content
  - modules
    - module-a (module type)
      - example-1 (module name)
      - example-2
    - module-b (module type)
  - Content-Page

module-a and module-b would have a different Snippets
example-1 & example-2 would share the same Snippet
So in the Content-page content file I would have

module-a: example-1

so would I add to the Content-Page.php the following?

<?php snippet($page->module-a()->value())? ?>

I don’t know, I think the field in the content file should always be called the same, no matter if the user selects an module of type a or type b, otherwise you would not know what field to call (or you would have to use as many if statements as you have module types). But I guess the examples use different blueprints/have different file names? So if the user just selected a module, you could have in your content file:

module: example1

And in your template

<?php
$module = $page->module();
snippet($module->intendedTemplate());
?>

Thanks @texnixe I’ll give it a go tomorrow…appreciate the help.