Forum

Thread tagged as: Question

Conditionals in templates?

Appologies for a simple “how do I?” post but much has changed since I last played with perch and I wanted to check I was trying to proceed in the best way. Maybe I should just get my hands dirty and see how it pans out but:

I want an editor to be able to add videos to a site and those videos could be an mp4 file, a vimeo link or a youtube link. Is there a way for a template to deal with those options so that I can have a field for “video source” that will handle it without any danger of the editor trying to say, both upload a file AND paste a youtube link?

I had a similar issue when I was thinking about how to design a “projects” page for them. It’s a list/detail situation but sometimes the “detail” is just a link to an external site. I want the template to require either that link OR the detail page (or a checkbox to publish the detail to be ticked, more likely) but not both... can templates do this?

In my second case, the checkbox solves the problem as it will override the link to the external site but the question of what is “required” in the template is still an issue. Can requirement be conditional?

Owain Browne

Owain Browne 0 points

  • 9 months ago

Thinking further about this and what I can understand from my history with perch (which was pre pages and categories) essentially the functionality I’m imagining could be achieved by blocks but with a limit for the editor to chose 1 block... which I believe isn’t possible at the moment?

Could this be achieved with categories - I have to confess I don’t yet understand how categories work? Or is the best way forwards to simply guide the editor through the interface with careful help text?

Blocks are fantastic as they allow you to get the editor to select part of your template at edit time which as I understand it is the only way this can be done in perch. Perhaps I’m thinking about this the wrong way and I could let my client build their list of videos entirely as blocks and only allow one “item” on their videos page. This maybe the solution I go for but I’d still be curious to know if there was another way...

Drew McLellan

Drew McLellan 2633 points
Perch Support

I'm not sure how or what to answer here, but I'll give it a go.

Is there a way for a template to deal with those options so that I can have a field for “video source” that will handle it without any danger of the editor trying to say, both upload a file AND paste a youtube link?

I would focus on what you output. If you've got an unreliable person adding content, who knows what they will do, but you can control what gets output to the template. If they add a video and a YouTube link, only output one of them.

I want the template to require either that link OR the detail page (or a checkbox to publish the detail to be ticked, more likely) but not both... can templates do this?

No, but again, you control what gets output.

Can requirement be conditional?

No, if it's not always required it's can't be required.

which I believe isn’t possible at the moment?

It's not possible to stop the editor adding more content than is used, but it's possible to use less content than is added.

Could this be achieved with categories - I have to confess I don’t yet understand how categories work?

Categories are as simple as they sound - they're for categorising content. I'm not sure how they'd apply to what you're asking, but I'm also not really sure what you're asking.

Thanks for the reply - I’m much clearer on how to proceed in this case but more importantly in general how I should be thinking about templates within perch.