Jump to content

To render or not to render - that's the question


charliez
 Share

Recommended Posts

I don't almost ever use the render method, but I assume it might be pretty quick way to get something visible while developing. What it does it outputs the page content.

So if you have some templates that you want to use as a blocks for other pages, then render method would be super useful. That means that those blocks are never used as a single pages, but always as a part of another page.

Truth is, that PW is very flexible tool. There is no "one true method to build PW-sites", like there is no "one true method to build PHP-applications". Some like to echo, others like to save output to variable to pass it around etc..

Link to comment
Share on other sites

Is there any way to hijack the render() method?

e.g. I would load a page and render the 'preview' or 'thumbnail' version [full version is being loaded on page load, apart from render() (hopefully)]

 <?php
   $photos = $pages->get('/example/page/get/')->children('template=photos');
   foreach ($photos as $photo) {
     $photo->render();
   }
Link to comment
Share on other sites

Adam, the page render() method doesn't know if it's being called upon to render a page for a request, or being called by the API somewhere else. Bust since the render() method basically just executes your template and captures the output, you can have your template look for some variable that tells it to render a 'thumbnail' version. In this example, I assign a 'useThumbnail' variable with a value of true to each photo page, and then call its render() method:

$photos = $pages->get('/example/page/get/')->children('template=photo'); 
foreach($photos as $photo) {
   $photo->useThumbnail = true; 
   echo $photo->render(); 
} 

Then in your 'photo' template, check for that 'useThumbnail' trigger:

$img = $page->image; 
if($page->useThumbnail) $img = $image->size(150,80); 
echo "<img src='{$img->url}' alt='{$img->description}' width='{$img->width}' height='{$img->height}' />";
Link to comment
Share on other sites

"since the render() method basically just executes your template and captures the output"...

I'm guessing it does work quite similarly... oops, I can think of a few places where I should have used render()...

would it be better than ajax load for seo you think? I fear it may~

Link to comment
Share on other sites

Using $page->render() doesn't have anything to do with ajax. It's the method that PW uses internally to render a page, but it's exposed in the API in case you want to render some other page. The most obvious use would be using pages as blocks of content for placement in other pages (partials). I used them recently for having sidebar-features. The editor would select from a group of pages in an asmSelect list (page reference field), and those pages would be rendered in the sidebar.

When it comes to ajax, I think you are better off just loading at the URL the page lives at, rather than asking another page to render it, and passing along an ID or something. It's not ideal to ask one page to render another where the other is specified by some dynamic user request (ajax or otherwise) because that could open up security concerns.

Link to comment
Share on other sites

Right, with dynamic user requests that would be a bad idea. I was talking about my amature usage where I have content loaded in the background via ajax to help out with page loading and transition times.

I know they have nothing to do with each other, but I just thought the end results can be kind of compared. They both seem to return the output from the template for a given page (ajax via the page url, and render() via the $wirepage). Forgive me, It wasn't untill I read this thread when I reallized how render() works~

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