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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Similar Content

    • By Robin S
      Repeater Easy Sort
      Adds a compact "easy-sort" mode to Repeater and Repeater Matrix, making those fields easier to sort when there are a large number of items.
      The module also enhances Repeater Matrix by allowing a colour to be set for each matrix type. This colour is used in the item headers and in the "add new" links, to help visually distinguish different matrix types in the inputfield.
      Screencasts
      A Repeater field

      A Repeater Matrix field with custom header colours

      Easy-sort mode
      Each Repeater/Matrix item gets an double-arrow icon in the item header. Click this icon to enter easy-sort mode.
      While in easy-sort mode:
      The items will reduce in width so that more items can be shown on the screen at once. The minimum width is configurable in the field settings. Any items that were in an open state are collapsed, but when you exit easy-sort mode the previously open items will be reopened. You can drag an item left/right/up/down to sort it within the items. The item that you clicked the icon for is shown with a black background. This makes it easier to find the item you want to move in easy-sort mode. You can click an item header to open the item. An "Exit easy-sort mode" button appears at the bottom of the inputfield. Configuration
      In the field settings for Repeater and Repeater Matrix fields you can define a minimum width in pixels for items in easy-sort mode. While in easy-sort mode the items will be sized to neatly fill the available width on any screen size but will never be narrower than the width you set here.
      In the field settings for Repeater Matrix you can define a custom header colour for each matrix type using an HTML "color" type input. The default colour for this type of input is black, so when black is selected in the input it means that no custom colour will be applied to the header.
      Exclusions
      The easy-sort mode is only possible on Repeater/Matrix fields that do not use the "item depth" option.
       
      https://github.com/Toutouwai/RepeaterEasySort
      https://processwire.com/modules/repeater-easy-sort/
    • By Joachim
      Long time user and huge fan of PW, but this time I can't find an answer to my question this time:
      For my social media buttons, I have a Repeater field called var_link_web with two fields: one is for the URL, and the other is an Images field containing two images that are used as a background-image for a <div>, of which the second is the ':hover' version (although activated through JavaScript here). 
      There are four instances of this Repeater, of which two are 'turned off'.
      I use the following PHP in my _main.php to call them, wrapped in <p><?php == ?></p>:
      $s_m_button = $variables->var_link_web; foreach($s_m_button as $button){ $button_image = $button->var_link_image->first->height(80); $button_image2 = $button->var_link_image->eq(1)->height(80); echo " <a href='$button->var_link_url'> <div class='image_link' style='background-image:url({$button_image->url})' onMouseOver='this.style.backgroundImage=url({$button_image2->url})' onMouseOut='this.style.backgroundImage=url({$button_image->url})'> </div> </a>"; }; ($variables leads to an unpublished page with several fields I want to have easy access to, and is defined in _init.php.)
      However, this gives me the following result:

      I have no idea where the extra <p>'s come from. The URL field has the 'HTML Entity Encoder' turned on. What's even weirder is that the HTML source file seemingly renders correctly:
      <p> <a href='https://www.facebook.com/'> <div class='image_link' style='background-image:url(/site/assets/files/1045/icons-facebook-square.0x80.png)' onMouseOver='this.style.backgroundImage="url(/site/assets/files/1045/icons-facebook-square2.0x80.png)"' onMouseOut='this.style.backgroundImage="url(/site/assets/files/1045/icons-facebook-square.0x80.png)"'> </div> </a> <a href='https://www.instagram.com/'> <div class='image_link' style='background-image:url(/site/assets/files/1046/icons-instagram-square.0x80.png)' onMouseOver='this.style.backgroundImage="url(/site/assets/files/1046/icons-instagram-square2.0x80.png)"' onMouseOut='this.style.backgroundImage="url(/site/assets/files/1046/icons-instagram-square.0x80.png)"'> </div> </a> </p> Removing the JavaScript has no effect. I'm probably missing something obvious, but am at a loss here.
       
      Thank you in advance!
    • By joeck
      Hi,
      I'm facing the issue where i have a repeater field with a multi language textarea (german & english, german beeing default). In german more blocks should be displayed as in english.
      The textarea field is configured as inherit default language if blank (I don't want to change this since it is the wanted behaviour for all other pages...).
      Now I want to access the unformatted value of the textarea field in the repeater and only show it if it is not empty. Something like this:
      $body = ""; $page->of(false); foreach($page->blocks as $block){ //blocks is repeater field, each block has title and textarea if ($block->textarea->getLanguageValue($user->language)){ $body .= <<<EOD <div> <div class='uk-card uk-card-default uk-card-body'> <h3 class='uk-card-title'>$block->title</h3> $block->textarea </div> </div> EOD; } } $page->of(true); print $body; However this doesn't work: Call to a member function getLanguageValue() on string.
      I also tried the other syntax for getLanguageValue:
      $page->getLanguageValue($language, $field) But I didn't manage to get a unique name of the textarea field in the repeater.
      I looked into the API of FieltypeRepeater but couldn't find anyhting that fixed my issue.
      Any ideas?
    • By sebr
      Hi
      On Processwire 3.0.179
      Create a fieldtype image named "img_test" with option "Use custom fields" On the template "field-img_text" add a title field On a repeater matrix, add a new type with the field "img_test" Add the repeater matrix on basic-page Create a new page and add an image in the repeater matrix, in the field img_test and fill in the title field from the image field Save and reload : the title field from img_test in the repeater matrix is empty In older Processwire versions, the first time I saved this custom fields were not saved, but on the second one, yes.
      Any idea about it ? A bug or a bad setting ?
    • By opalepatrick
      I see old posts saying that repeaters are not the way to go in Custom Process Modules. If that is the case, when using forms (as I am trying to do) how would one tackle things like repeat contact fields where there can be multiple requirements for contact details with different parameters? (Like point of contact, director, etc) or even telephone numbers that have different uses?
      Just for background I am creating a process module that allows me to create types of financial applications in the admin area (no need to publish any of this, pure admin) that require a lot of personal or company information.
      Maybe I am thinking about this incorrectly?
×
×
  • Create New...