1

Has anyone ever tried using Structures instead of Singles? I'm developing something that's kind of like multiple sites in one, they share the same central design, just with content differences and a few colour changes. I thought if I could use a Structure instead of Singles, I could contain each section to a specific area (making it easy to maintain), and use Entry Types to decide on the content.

The problem is with using a Structure is I keep getting Variable "entry" does not exist. errors. Any suggestions?

If anyone can suggest a fix, or a better way of handling something like this, I'd love to hear it!

Thanks

2

I'm doing this at the moment. It's not a perfect solution, but nor is it terrible. In a lot of ways, I'd quite like Craft to just burn Singles and Channels and use nothing but Structures. You can do everything with Structures that you can with the other two, and you get better flexibility and much nicer groupings in the CMS that make more sense to clients (the one confusing bit of Craft clients seem to have is why editing pages is spread out over three areas they don't see any real logic behind). But, that's not how things are. Anyway...

If you're getting "entry does not exist" then you've got something broken in your routing. Every page inside a Structure is an Entry, and if you've got URLs resolving to entries in your Structure then you've got access to the entry variable in your template.

Check your Structure and see what template it's actually loading. The only thing I can think of at the moment with the little detail you've given is that you've got templates in a path that match the Structure, and instead of routing theough the database it's routing theough the files.

i.e., if you've got templates/my-structure/my-entry.twig and also an Entry with a URI of /my-structure/my-entry then you might be skipping the database because your filesystem is matching first.

  • I think it's the routing that I'm having issues with, but is something I often get confused by. Because there's not set "homepage" like there would be if I had a Single (__HOME__), it throws an error. If I append /index to the URL, I see the page I expect to see. So how do I use this structure's entry as the homepage? – Chrish Jul 18 '17 at 15:56
  • 1
    I don't think Craft will let you have a non-Single as a homepage. A work-around would be to manually grab the Entry in the /templates/index.twig template. Just put something like {% set entry = craft.entries.id('1') %} at the top of the template - where the ID is the page ID of whatever Entry in your Structure you're wanting to treat as the homepage. – Matt Wilcox Jul 19 '17 at 7:36

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.