renobird Posted March 21, 2012 Share Posted March 21, 2012 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 More sharing options...
formmailer Posted March 21, 2012 Share Posted March 21, 2012 Hi and welcome to the forums! Regarding your question, there are many ways to achieve things in PW, but you might like to check this topic: http://processwire.c...egate-approach/ //Jasper Link to comment Share on other sites More sharing options...
alan Posted March 21, 2012 Share Posted March 21, 2012 @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 More sharing options...
MarcC Posted March 21, 2012 Share Posted March 21, 2012 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 More sharing options...
ryan Posted March 21, 2012 Share Posted March 21, 2012 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')); 2 Link to comment Share on other sites More sharing options...
renobird Posted March 21, 2012 Author Share Posted March 21, 2012 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 More sharing options...
netcarver Posted March 21, 2012 Share Posted March 21, 2012 Great topic! Thanks for posting Tom and good to see you in a forum again. I used to use something similar to Ryan's tools.inc back in Textpattern but called it glue.php and had it live in the plugin cache directory. Link to comment Share on other sites More sharing options...
renobird Posted March 22, 2012 Author Share Posted March 22, 2012 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. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now