fliwatuet

Delayed output and many fields

Recommended Posts

Hello!

I dropped out on trying to use processwire, but I will give it a new try. So after reading some tuts (again) and much better understanding of the concept, there are of course new questions.

I am using the latest pw 3.0 and the intermediate profile, so it is "delayed output" for me.

My first question is how to output all the fields in a page? (Ryans planet tutorial)

When I use

$content=$page->planet_summary;

then I get the content of this field. But how to output the fields "planet_age" and "planet_type" in the template file "planet.php"?

So, I did some research and found "How to Create an AJAX Driven Theme for ProcessWire".

Ok, let's concatenate the fields:

$content=$page->planet_age . "<br/>" . $page->planet_type . "<br/>" . $page->planet_summary;

So far so good, all is fine and working. But what to do when I use let's say 50 fields? There has to be a better method, also to arrange the fields with e.g. bootstrap.

How can I do this?

Share this post


Link to post
Share on other sites

Thanks! Output bufferings seems to be the easiest solution,  but ain't this an objection to the delayed output? Are there any other drawbacks?

The Wire Render Pattern also looks pretty easy to understand and implement.

Share this post


Link to post
Share on other sites
6 hours ago, fliwatuet said:

Output bufferings seems to be the easiest solution,  but ain't this an objection to the delayed output?

Output buffering works great with delayed output.

Set variable default in your auto-prepended init.php:

$sidebar = $page->sidebar;

Override in your template as needed:

<?php ob_start(); // $sidebar ?>
    <div class="special-promo">
        <h2>My special promo sidebar</h2>
        <?= $page->special_promo ?>
    </div>
<?php $sidebar = ob_get_clean(); ?>

Output in auto-appended main.php:

<div class="sidebar">
    <?= $sidebar ?>
</div>

I have never entirely understood the benefit of $files->render(), aka wireRenderFile(). Seems to be similar to an include, but with limited variable scope. Personally I'd rather use includes for shared partials so I can get and set any variable inside the partial without needing to explicitly pass variables back and forth from template to rendered file. I've never had to deal with so many variables that I'm losing track of their names and accidentally overwriting them or anything like that.

 

7 hours ago, fliwatuet said:

My first question is how to output all the fields in a page?

It is possible to get all fields in a page and loop over them:

foreach($page->fields as $field) {
    $field_content = $page->get($field->name);
    // output $field_content
}

But there are few situations where this would be useful, because usually you are using several different types of field on a page which return different types of data (string, WireArray, PageImage, etc) and need different markup output. So most of the time you want to get individual fields by name.

Share this post


Link to post
Share on other sites
On 11/19/2016 at 3:11 AM, Robin S said:

I have never entirely understood the benefit of $files->render(), aka wireRenderFile(). Seems to be similar to an include, but with limited variable scope.

Just for the record:

"I also tend to store additional properties in $page rather than creating global variables (e.g., $page->foo = 'bar' instead of $foo = 'bar'), which makes it a lot easier to use wireRenderFile(). As long as you pass in $page, you know you've got everything you need, and you can add more properties later without worrying about updating your "view bag."

  • Like 1

Share this post


Link to post
Share on other sites

Delayed output and WireRenderFile() seems to be such a returning topic, especially lately, it should get a dedicated place in the forum.

Share this post


Link to post
Share on other sites
Quote

As long as you pass in $page, you know you've got everything you need, and you can add more properties later without worrying about updating your "view bag."

The wire render pattern (by clsource) is just another example of how you can use the amazing potential of Processwire.
Instead of the $viewBag you can use any other $dataBag as you see fit that not need to be updated inside _main.php
Processwire now also has the region() function and fields can use a template for even more file and code organization.

  • Like 2

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.