Jump to content

Can I choose different templates when writing a page?


Boost
 Share

Recommended Posts

Hi,

I read through the documentation and am now somewhat confused by the concepts of "page" and "template." For example, on my website, I have a blog section where the writer publishes her posts. What I want to know about PW is whether I can create multiple templates (different layouts or designs) for the writer to use in her posts. I don't want her to worry about layout or design; just throw the text in. Everything else should be handled by the "template."

Cheers

Link to comment
Share on other sites

You can create different templates that have different designs (templatename.php)
The author must select which template/design to use when creating a page.

A template defines which fields are available on a page and which template file (/site/templates/templatename.php) is used.

The content/text is then defined on a page.

  • Like 1
Link to comment
Share on other sites

You can use the template name (both external and internal template) to choose any layout for any webpage or any part of the website

I have the layouts stored in a view and an include folder and select them with php logic either on the template file or the view file

This layout strategy is already in the forum posts for many years

Here is an example:

<?php namespace ProcessWire;
// you can add here any business logic if needed to override default settings, values and $variables

// default <head>
include ($includes_path . "head.php");  // <head> is not a view such as the <header> <main> or <footer> - it is the <head> section of a website

// default view <header>
include ($views_path . "header.php");   // the <body> tag and the first <header> tag after the <body> tag

// selected view <main>
include ($views_path . "{$page->template->name}" . ".php");   // the main content view selected with the page template name (internal or external template file name)

// default view <footer>
include ($views_path . "footer.php");

 

Link to comment
Share on other sites

Hey @Boost it sounds like you have done a lot of research but never installed PW for testing? If so than just do it as this might help in understanding everything better ? 

ProcessWire has the concept of Pages, Templates and Fields. Note that the term "template" is very different to other systems like wordpress, where a template is an installable design. In PW the template defines the fields that the user can use for storing content. So when creating a new Page, you can select different Templates which means that on the next step the user can populate the fields that belong to the selected template.

For example a boat template could have fields "title (name)", "year", "length", "cost" and the template blogpost could have fields "title", "body", "images"

The visual representation of the template is done via template files that are stored in /site/templates. So the "boat" template would render the template file "/site/templates/boat.php".

So far to the technical background. I guess that your question is not "can I choose different templates when writing a page" but rather "can I choose different layouts when writing a page"?

So your client would create a new blog post having template "blogpost" for example. But then he/she can choose between layout1/2/3. You could do that by simply adding an options field holding all the possible options and then in your template file (/site/templates/blogpost.php) you do something like this:

<?php
include __DIR__."/layouts/" . $page->layout . ".php";

Which would make it render /site/templates/layouts/layout1.php for the selected option "layout1" of your blogpost...

  • Like 4
  • Thanks 1
Link to comment
Share on other sites

Nothing wrong with @bernhard's way of doing it, but another option might be to name templates like so-

  • blog_standard
  • blog_gallery
  • blog_short_status

and set up fields within those templates as appropriate to its purpose. Then in the parent page's setting (Family tab) set allowed children to those templates.

As with practically everything in PW there are as many different ways to do things as there are users.

Link to comment
Share on other sites

10 hours ago, DaveP said:

Nothing wrong with @bernhard's way of doing it, but another option might be to name templates like so-

I think if all blog posts need the same fields, Berhard's solution is better. So you have to set the fields only once.

But of course your solution works too ? 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...