Jump to content

Reusable Content - Best Practice?


renobird
 Share

Recommended Posts

Hello All,

I'm just starting my first PW build (and I love it so far.) I'm looking for some ideas on how others create reusable content, like sidebars or other content that may get displayed in any number of templates. I'm a long time Textpattern user, and reusable content like this is handled fairly easily. It appears to be just a simple with PW, but I'm still getting my head around the API. I thought I'd show my current thinking, and ask for any feedback or suggestion on alternate methods.

Basic example of reusable content:

On a home page I want to display the latest news item, and on another page I want to display the latest 3.

My current method is to call a template named news_posts.inc like so:

<?php $news = $pages->get("/news/")->children("limit=1");
include("./news_posts.inc"); ?>

To show more posts on another page, I just increase the limit.

For the sake of this example, let's say news_posts.inc just shows the title of each post:

<?php foreach ($news as $item)
echo "<p>{$item->title}</p>";
?>

This works fine, and is pretty straight-forward, I'm just curious if there are other methods or if I'm on the right path.

Link to comment
Share on other sites

@renobird I also am a TXP person and also have just started with PW, and I'm mightily impressed.

I'd already bookmarked the page noted by @formmailer for a re-read — the first time I ran through it it left me a bit confused, I think party due to my limited intellect ;) and also as it's discussing likely a very powerful way to use PW.

As far as static reusable content goes, I believe it's a widely adopted approach to use pages for all sorts of stuff, from selects to static content. E.g. you could create a top level page called Statics and under it have a page per lump of static content you may wish to reuse around the site. The page with the content could have any fileds you wish as it can use any template.

Sorry I'm not more up on PW to give more advice and I hope that's not been too obvious and is useful. Enjoy PW, I am!

Link to comment
Share on other sites

Hey Tom,

I really like Ryan's approach in the thread linked above. For one, it's well-documented (and gets better toward the bottom of the post), but I like how it lets you modify items in <head> in a really elegant way. Heck, the whole thing is really elegant compared to the standard include method.

I'd love to see some of Soma's code from his example though, since I didn't grasp it entirely.

Link to comment
Share on other sites

This works fine, and is pretty straight-forward, I'm just curious if there are other methods or if I'm on the right path.

I think you are on the right path here. The page that Jasper and Marc linked to is good when working with larger scale stuff (and smaller too), but I'm not usually that disciplined on smaller stuff. One alternative would be to bundle your news_posts.inc into a function, and keep a file with that function and any others you might need. I usually call mine tools.inc. The function would look like this:

/site/templates/tools.inc

function listNews($news) {
   $out = '';
   foreach($news as $item) $out .= "<p>{$item->title}</p>";
   return $out; 
}

Your template files that need to list news (or use your other functions) would include('./tools.inc'); at the top, and then they would call the function when/if they needed to list news:

echo listNews($pages->get('/news/')->children('limit=5')); 
  • Like 2
Link to comment
Share on other sites

Wow. Thanks everyone! There's a lot to wrap my head around in the post that Jasper and Marc linked.

I can certainly see the benefit for larger projects though. I'll spent some time digesting those methods tonight.

Ryan, creating a tools.inc to store functions is great! I'm a hack at PHP (at best), so some of my learning curve is more than just the API.

:)

Link to comment
Share on other sites

Hey Steve,

Long time eh? I'm glad to see you here as well. I still keep tabs on the TXP forums, but I tend to build the same kinds of sites with TXP lately, as a result I don't have a lot to ask about or contribute.

I have a fairly involved design/build coming up at my new job — and ProcessWire fits the bill perfectly. I think you'll be seeing a lot of me around here. :)

  • 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...