Jump to content

Similar to Repeater - Custom Fields not fixed to template?


nexflo
 Share

Recommended Posts

One thing i cant seem to find is how to add custom blocks to pages.

I.e. I've got a template which includes several content fields - headline, content1, content2 but i also want to add another teaser... it would be great adding "new field" -> teaser into pages where i want to add them to.

is something like this possible,without creating a new template for everypage?

The ACF plugin for wordpress includes options like this and its very convenient.

http://www.advancedcustomfields.com/add-ons/flexible-content-field/

Link to comment
Share on other sites

Well, ProcessWire is far more flexible than that!

Starting at a very basic level,

you could create fields in your main template that you would then use in a sidebar for instance:

sidebar_title

sidebar_text

sidebar_image

or whatever you wanted.

If those are included in the templates you are using, then those fields are available for all the pages that use those template - if you don't use them, they wont display anything.

Next level up!

You can pull content from any page.

So, you could create a specific page with a specific template (but no associated template file) and use it for some global fields.

For instance it could have a textarea called "extra_info" into which you add something you want to appear on many pages.

In the template file for those pages you would call that field in this way:

echo $pages->get("/my-global-page/")->extra_info;

Getting more complicated

Next up, you can use a Page field to drag in an entire page, or series of pages.

For instance, you create a block template (complete with file) and give it a handful of fields.

In the associated template file, you would only put in the mark-up needed to render that particular block - no header or footer or anything like that.

You can use that template to create a series of blocks.

You now create a page field which you will use to select one or more of those blocks. You can set up the page field to look for pages under one particular parent, or by one particular template - so you can restrict what is selected.

Add that field to the template for your normal pages.

Edit one of those pages and you can select a block.

In the template file for your normal pages, you can now grab the contents of that field as normal, but then render the output:

echo $page->mypagefield->render();

That will output the selected page, rendered in its own template.

From that point you can get really imaginative.

You can use the page field to select multiple pages and then use a foreach loop to render them all out. You can set up your normal page templates so that you can choose blocks for individual pages, or by perhaps putting the page field in a central source (like I did at the head of this answer), maybe make it a global block system

You really can do anything you want, render things as complete pages or just pull particular fields out ... and so on.

This demo site I did is done completely with blocks that are then selected from a central source:
 

http://pwdemo3.stonywebsites.co.uk/

So, yes - it is all possible, and without having to go near a plugin.

Joss


 

  • Like 4
Link to comment
Share on other sites

thanks a bunch joss for your fast and detailed answer.

is it also possible to include "template" parts..instead of prefilled pages? prefilled pages dont show the enduser what exactly it is he's actually including.

Link to comment
Share on other sites

Repeater field is the only option currently. Maybe in future we could try building something out of it to support many repeater templates you could define and choose from when adding new item. Originally this was my idea to have with how repeater work but it turned out different. It's very complicated to build but it may worth trying. We already discussed this and people where asking for it. For now page references work pretty well and is the way to go as it offers the most flexibility and enables to share content and reuse. It's consistent abd fast. Rather also see how far we could push page fields to add features like direkt editing etc.

Link to comment
Share on other sites

Yeah, the flexible content is kind of cool if you have situations where you have a lot of pages that don't all really conform to any single template, but most of the time when you have your content really well structured, and analyze what the output needs to be, you might find that this type of thing is really not necessary;

if your needs are not that complex you could create a sort of generic repeater, where there is a select list for what is contained in the text area, like content, teaser, etc. then you could check the value of that select field and output the markup accordingly.

Link to comment
Share on other sites

  • 1 month later...

After working with blocks in Drupal, I now find it easier and more maintainable in PW to hard code the blocks using includes (or direct code) in the main template and wrapping if/else logic around each block. Sometimes you may want a block to be configurable by the client. So in PW, create a template and render an instance of that template directly in the sidebar of the main template. This way, all the logic of what gets output in your sidebar can be easily traced and version controlled rather than being set in the database. Because PW uses page hierarchy, there are cool things you can do like query which section you're in ($page->rootParent() I think) and output a certain "block" based on that section. You can also use a page reference in a "block" template and and pass your current URL to a query of those template instances and output anything that matches. If nothing matches on that URL, ask the current page's parent if there is a match and do on up the tree until you get to home. That's how I let a client override a header image on let's say Home / About / Our Team. They create an instance of "header_image" template, upload an image to it, and choose Our Team as the page reference. The code checks to see if there is a match on /about/our-team. If there is, output the image. Else ask to see if there is a match on just /about. Else default to the header image for the homepage. You actually use a for loop to do march up the tree and end if you hit a match or the root.

  • Like 1
Link to comment
Share on other sites

Okay, here goes.

This is my blocks demo as a profile - I haven't actually tried to install this, so I am guessing it will work!

Bear in mind that I did not intend this as a profile, so it is uncommented!

http://stonywebsites.co.uk/blocks-profile.zip

Blocks are created under Content Management >Blocks in the pages.

Footer blocks are selected in Footer Settings under settings.

Home page blocks are selected on the home page.

This particular version uses page fields and repeaters. This is designed with Bootstrap so you will see span fields all over the place allowing you to set how wide the blocks are. (remember that in any row it has to add up to 12!)

Anyway, it might help

Joss

PS: It comes with my Bootstrap admin template which you don't have to use

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

×
×
  • Create New...