Jump to content

How do I output lists of content, images, etc?


ryan
 Share

Recommended Posts

I think I got the basic idea on how to publish content, but I don't understand how to output lists of content. With most of the projects I'm working on I have a list of recent news, a list of portfolio entries, an image gallery, a list of posts within a category or tagged with a certain tag. I tried to understand what you did in your skyscraper example in SkyscraperList.php, but I've absolutely no idea.

The skyscraper example is probably not the best one to look at. I built that SkyscraperList.php just to maximize the efficiency and appeal to coders, but it probably made it a lot less clear to designers. It's not necessary to take that approach. The templates that come with the basic/default site are probably a better place to start.

As far as how to output lists of content, typically you'd follow a process like this:

1) Find the pages you want to output, typically with $pages->find(...)

2) Loop through the results and output the fields you want.

Example:

<?php
$news_items = $pages->find("parent=/news/"); 
echo "<ul>";
foreach($news_items as $item) 
    echo "<li><a href='{$item->url}'>{$item->title}</a></li>";
echo "</ul>";

If you wanted ProcessWire to do the above for you, you could do this:

<?php echo $pages->find("parent=/news/")->render(); 

(basically added the "render" to the end of the pages find)

I prefer to generate my own markup for clarity and control, so don't recommend the shortcuts (like above), especially when you are learning. But just wanted to let you know they are there. These two pages provide more detail and examples of how to output lists:

http://processwire.com/api/templates/

http://processwire.com/api/selectors/

With the documentation, I've tried to balance it between the needs of designers and developers, so if you see something you don't understand, don't feel bad about skipping it ... it will probably make sense later, or feel free to reply with questions or post in the forum.

With regard to lists of images, I haven't yet finished this part of the documentation, but it is similar to working with pages. However, you would need to have an images field to work with in your page's template. I will assume you have one called "images" (like in the demo site). If we wanted to print the images from the current page as 100x100 pixel thumbnails linked to full size versions, we would do this:

<?php
foreach($page->images as $img) {
$t = $img->size(100, 100); 
echo "<a href='{$img->url}'><img src='{$t->url}' alt='{$t->description}' /></a>";
}
Link to comment
Share on other sites

  • 4 months later...

Hey ryan,

found the time finally to try out PW and I really like it. But now I’ve to get more familiar with the possibilities through playing around with some code.

So, my question in the context of this thread is how I can output the complete rendered code of a page. I got a list of page objects returned by a ->find(); call and as I loop through it, I want every single page’s html output (as defined in each page’s template) to be echoed. I saw the ->render() method in your post above but wasn’t able to find any documentation on how to use it or whether it has anything to do with the soultion of my problem.

Thanks in advance for your help.

Link to comment
Share on other sites

$page->render() isn't listed in the Page API right now because I've not used it much beyond rendering the requested page (one page per request). But it should also be fine to call from your own code, and should be able to render as many pages as you want it to in one request. I will be adding this to the API in the future once tested in more contexts, so it's not going anywhere. It's not been tested much in the context you are asking about, so please let me know if you run into any issues with it, and we should be able to resolve them quickly.

If you view the PHP source (/wire/core/Page.php), you won't find the render() method in the Page class because it's added by a module (/wire/modules/PageRender.module). There aren't any arguments to this function, so nothing to it other than calling $page->render(). This render method is available on all Page instances (like the pages returned from a find() for example).

If you are rendering pages in this manner, and the templates used by those pages are defining functions or classes (whether in your template or by including them from another file), PHP may throw a fatal error about duplicate definitions. As a result, your template files should include files that have function/class definitions in them with include_once() or require_once() rather than include() or require(). If functions are defined directly in the template file (as opposed to include files), you'll want to use PHP's function_exists() function to check if your function already exists before defining it again. For regular header/footer includes, you would want to continuing using include() or require() since you want them rendered on every page you call the render() method for. It's only the ones where you are defining functions/class that you have to consider. Many people don't make their own functions and classes, so it may not be a consideration at all, but I just wanted to mention it.

Link to comment
Share on other sites

Thanks for your fast response. I tried to use the render()-method. The result was a blank screen. Even with a pure html template without any php syntax in it the result stays the same. Any idea?

Link to comment
Share on other sites

Edit /site/config.php and set $debug = true (located near the bottom of the file). Then try that again and you should get an error message. I'm going to try here too. What version are you using btw? 2.0 (stable) or 2.1 (dev) ?

Link to comment
Share on other sites

After testing here, I was only able to get a blank screen if I put PW into an infinite loop. I unintentionally did this when I attempted to render a page within a page where they were both using the same template, and each tried to render the other indefinitely. So I would say that the first thing to look at is to make sure that you don't have a possible infinite loop where the two pages may be trying to render one another.

While looking into this, I also discovered an issue where Page::render() sending the template the wrong $page object... giving it the one requested on the URL rather than the one you provided it. The result was attempting to render a $page object in the wrong template. PW still went ahead and rendered it just fine in my case, but with the wrong content showing up. In your case, if the unexpected $page in the template produced a fatal error, it's possible that could have led to the blank screen as well (assuming no debug or superuser).

Thank you for helping me to find this bug. This issue has been fixed in the latest commit in both the 2.0 and 2.1 source. Please grab the latest commit and let me know if the $page->render() now also works as expected from your side?

Thanks,

Ryan

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