Jump to content

Question regarding Blog template


totoff
 Share

Recommended Posts

Hi all,

I'm trying to adapt the blog profile to my needs. In my template blog-list.php I have the following:

$content = renderPosts("limit=10", true);

which populates $content in main.php.

renderPosts() I have taken as it is from blog.inc in the blog profile:

/**
 * Given a PageArray of blog entries generate and return the output.
 *
 * @param PageArray|Page $posts The entries to generate output for
 * @param bool $small Set to true if you want summarized versions (default = false)
 * @return string The generated output
 *
 */
function renderPosts($posts, $small = false) {

	if(!$posts instanceof PageArray) {

		if($posts instanceof Page) {
			// single page
			$post = $posts; 
			$posts = new PageArray();
			$posts->add($post); 

		} else if(is_string($posts)) {
			// selector string
			$selector = $posts; 
			$posts = wire('pages')->find("template=blog-post, sort=-date, $selector"); 

		} else {
			throw new WireException('renderPosts requires a PageArray, Page or selector string'); 
		}
	}

	foreach($posts as $page) {

		if(empty($page->summary)) {
			// summary is blank so we auto-generate a summary from the body
			$summary = strip_tags(substr($page->body, 0, 450));
			$page->summary = substr($summary, 0, strrpos($summary, ' '));
		}

		// set a couple new fields that our output will use
		$page->set('authorName', $page->createdUser->get('title|name')); 
		$page->set('authorURL', wire('config')->urls->root . 'authors/' . $page->createdUser->name . '/'); 
	}

	$t = new TemplateFile(wire('config')->paths->templates . '/markup/posts.php'); 
	$t->set('posts', $posts); 
	$t->set('small', $small); 
	$out = $t->render();

	// if there are more posts than the specified limit, then output pagination
	if($posts->getLimit() < $posts->getTotal()) $out .= $posts->renderPager(); 
	
	return $out;	
}

As you can see, the function makes use of /markup/posts.php. Within posts.php I have the following:

// display a headline indicating quantities
	$start = $posts->getStart()+1;
	$end = $start + count($posts)-1;
	$total = $posts->getTotal();

	if($total) echo "<h3>" . sprintf(__('Posts %1$d to %2$d of %3$d'), $start, $end, $total) . "</h3>";

This echoes the quantities headline as expected to $content but I would like to have it echoed to my $sidebar variable instead which currently doesn't work as it is part of renderPosts().

My question: What would be the best strategy/solution to assign $total to my $sidebar variable instead to $content?

Thanks

Link to comment
Share on other sites

Use the following in your blog-list.php if that is where you have your $sidebar

$posts = $pages->find('template=blog-post, limit=10');//or whatever the template of a 'post' is

$start = $posts->getStart()+1;
$end = $start + count($posts)-1;
$total = $posts->getTotal();

if($total) echo "<h3>" . sprintf(__('Posts %1$d to %2$d of %3$d'), $start, $end, $total) . "</h3>";

Note:

  • getStart() and getTotal() have nothing to do with renderPosts(). They are both PageArray methods. So, you can use them with any $pages->find() operation. 
  • renderPosts(): The first parameter accepts either a PageArray, a Page or a selector string. So, the following should work...
$sidebar = renderPosts($posts, true)//our $posts above...which fetched 10 posts

However, I am not sure I fully understand your question as renderPosts() with $small=true should already return the counts headline?

Edited by kongondo
  • Like 1
Link to comment
Share on other sites

Hi Kongondo,

many thanks.

However, I am not sure I fully understand your question as renderPosts() with $small=true should already return the counts headline?

It does, but I would like to have the counts headline in my sidebar ($sidebar) while the rest (the posts) should remain in the main content area ($content).

I tried nearly what you suggest before but got an error - maybe because I didn't set the $posts variable with pages->find. Will give your solution a try.

Link to comment
Share on other sites

OK, I get you. In that case you will have the headline counts rendered twice :-). First on top of your 'rest of the posts' ($content) and secondly in your $sidebar with the code I showed you. You have a couple of options to deal with the first/unwanted headline counts:
 
1. Use CSS to hide the count in your 'rest of the post'
2. Modify the renderPosts() function to do what you want.
3. Modify /markup/posts.php to do what you want (only send $total if....blah blah -see pseudo code below)
4. Use my Blog module instead :P  ;)  :-) with 'post_count' = 0 to not output the posts' count
 
for #3 suggestion..[see edit below; maybe not really necessarily if you want to completely get rid of the counts in renderPosts()

if($total && $page->template !='posts') echo "<h3>" . sprintf(__('Posts %1$d to %2$d of %3$d'), $start, $end, $total) . "</h3>";
//OR
if($total && $page->template =='blog-list') echo "<h3>" . sprintf(__('Posts %1$d to %2$d of %3$d'), $start, $end, $total) . "</h3>";
Edited by kongondo
See post below
Link to comment
Share on other sites

I was sleepy yesterday :-)....Ignore some of the above...

If you will never use headline counts anywhere else but in your $sidebar, then you can just uncomment the  /markup/posts.php code in renderPosts() in blog.inc. This (Blog) is a site profile so you can pretty much do with it as you want...Then, use the code I wrote above to get the total counts in your $sidebar. The only thing to ensure is that you use the same PageArray for $posts for your $sidebar (total count) and your renderPosts() ($content/rest of the post) - hope this makes sense.

  • Like 2
Link to comment
Share on other sites

Hi Kongondo,

many thanks for your replies! This code in blog-list.php does the trick:

	$posts = $pages->find('template=blog-post, limit=10');//or whatever the template of a 'post' is

	$start = $posts->getStart()+1;
	$end = $start + count($posts)-1;
	$total = $posts->getTotal();

	if($total) $sidebar = "<h3>" . sprintf(__('Posts %1$d to %2$d of %3$d'), $start, $end, $total) . "</h3>";

However, I'm not yet sure if it will play nice with the pagination this way, but I will test with more dummy content. Do you think it is safe to remove the limit=10 considering site performance?

you can just uncomment the  /markup/posts.php code in renderPosts() in blog.inc.

Not sure if this is a misunderstanding. /markup/posts.php is required to render the markup for both, the post list and the single posts itself. You can't remove it from the function. Did you mean /posts.php/. From there it's safe to remove the code and I've already done it in order not to have the counts headline twice :-)

Again, many thanks for your support. Much appreciated.

Link to comment
Share on other sites

Glad you got it sorted..

Pagination: That's why I recommended to ensure you use the same PageArray for your totals and the renderPosts(), i.e. the $posts PageArray. Anyway, test and see.

Limit: It is always good to limit results. With pagination, users can always get to the next results. What you set as a limit depends on other factors such as site traffic. So, it's up to you really but rule of thumb is to limit results if you don't need to show all of them at once.

Code to remove: I meant to uncomment just the code for the totals count but you have already sorted that out, no worries.

  • 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

×
×
  • Create New...