0

I have a site that I've created a 'Generic Page' type for that is a Structured type with URL Format of {slug}.

This is great for the client as they can create an entry for 'toplevelpage' and then children e.g. 'toplevelpage/firstchild' and the URL structure works as desired.

In a few places, they have custom pages that are not Structured type 'Generic Page' entries but need to sit in that same site URL structure. For example, toplevelpage/fistchild is a 'Generic Page' but they need a toplevelpage/customchild but the parent URL is now owned by the Structured type.

Can I get Craft or Twig to look for Singles or even Twig magic at that level and then just have a stub structured Generic Page that has is content overwritten?

1

First off, for communication reasons, let's refer to the "Structure" as a section, not a page type. Anytime you create either a Channel or Structure section, you have the ability to create what are called "entry types". You will see them listed on the far right of Settings > Sections in Craft.

The Solution in a nutshell

In your case, you will want to create a new Entry Type in your Structure to accommodate this custom deviant page, which will allow you to choose different fields for that type and its own template for its own custom code.


What is an "Entry Type"?

Ok, so what is an Entry Type? You know when you go to post a status on facebook and you have the ability to just write text, or you could choose to upload an image, or you might want to check in? All of these different types of status posts may share the same fields (like the text field to post your status), but some have additional or completely different fields (like uploading an image), but they are all "Status Posts" to Facebook, just different types of status posts. This is a very over-simplified example, I know, but it's kind of what Entry Types in Craft are for a Channel or Structure. They're like an inherent built-in category group that allow you to design different custom field layouts for different types of entries within that one Channel or Structure.

So if you make a Structure called "Generic Pages", you could then create different entry types in that Structure like "Single Column", "With Sidebar", "Top Banner", etc, and they can all get their own custom field setup with their own templates. Or you could just use one template for them all. I've created Events channels before using things like "Event", "Concert", "Party", "Meetup", etc as the different types. When I create a new entry in the "Events" channel, I then choose which type of event it is, and by doing so, I'm able to customize which type gets which fields. They all have a "Start Date" field, but maybe the "Concert" type has a "Door Price" field, whereas "Meetup" doesn't. This way you don't have a confusing "Door Price" field for "Meetup" events that shouldn't be or don't need to be filled in.


Templating

Ok, so how do we handle this in the templates? When you create the section in Craft, you'll point to whatever template you want for the section as a whole (let's say /generic-pages/_entry for example). In that _entry template, add just this line:

{% include "generic-pages/" ~ entry.type %}

Then inside the /generic-pages/ directory, you create a different template for each entry type you made in Craft. Name that template exactly how the handle looks like. So if you made a "Single Column" entry type and the handle is singleColumn, then "singleColumn.html" is the name of your template. Customize that template all you want.

Here's a proper article on Craft's site talking about this method: https://craftcms.com/support/entry-type-templates

Even if you're using one template for all entries in this Structure/Channel regardless of entry type, you can still be selective with how you grab your entries like:

{% set entries = craft.entries.section('genericPages').type('singleColumn') %}

Or be conditional based on type:

{% if entry.type == 'concert' %}
    // do something
{% endif %}

Read more about Entry Types in the Craft Docs: https://craftcms.com/docs/sections-and-entries#entry-types

  • Thank you Ryan, I've used this detailed response to build a solution with a custom code block entry type as a workaround. What I originally wanted was to be able to take content from a Singles entry or even static HTML and replace the contents of a structured entry that is needed to maintain URL pattern. – Bill Rafferty Sep 15 '17 at 5:56
  • Ah no problem. So it sounds like you added a Entries relation field to pull content in from the single? Anyway, glad it helped and hope this explanation helps others down the road! – Ryan Sep 15 '17 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.