Jump to content

Help understanding render and renderValue


Tom.
 Share

Recommended Posts

I've always used wireRenderFile() and this has been fine for me. However I find it much cleaner to use $page->render() or $page->renderValue(). I'm just trying to get my head around it. 

Usually I would do:

<?=wireRenderFile("ui/file", array("value" => $page->field, "closing" => "..."))?> 

However how do I achieve the same with render() ?

I've tried:

<?=$page->render("file", "ui", array("closing" => "..."))?> 

but accessing $options['closing'] returns blank. 

 

For a better example of my code, this is what I'm trying to achieve: 

<div class="uk-grid uk-grid-small">
  <?php
    $new = $pages->findOne("template=activity, sort=-created, limit=1");
    $event = $pages->findOne("template=event, sort=-created, limit=1");
  ?>
  <div class="uk-width-large-1-3">
    <?=$page->renderValue($new, "large/activity", array("title" => "New"))?>
  </div>
  <div class="uk-width-large-1-3">
    <?=$page->render("activity", "large", array("title" => "Staff Pick"))?>
  </div>
  <div class="uk-width-large-1-3">
    <?=$page->renderValue($event, "large/activity")?>
  </div>
</div>

 

Also it makes sense to me for renderValue to be?

<?=$page->renderValue($new, "activity", "large", array("title" => "New"))?>

In large/activity.php I've tried <?=$options['title']?> but nothing is output. 

Link to comment
Share on other sites

hi Tom,

i was also using wireRenderFile a lot and tried render() and renderValue() a lot in my current project and its great!

example for my blogitem:

<article class="tm-article uk-margin-large-bottom">
    <?= $config->alfred->render($page) ?>

    <?= $page->render('pic') ?>

    <?= $page->render('date') ?>
    <?= $page->edit('body') ?>

    <?= $page->render('files') ?>
    <?= $page->render('gallery') ?>

    <hr>
    <div class="uk-grid blognav">
        <div class="uk-width-1-1 uk-width-large-1-2 uk-text-left uk-text-center-medium">
            <?php if($page->prev->id) echo '<a href="' . $page->prev->url . '"><i class="uk-icon-arrow-left"></i> ' . $page->prev->title . '</a>'; ?>
        </div>
        <div class="uk-width-1-1 uk-width-large-1-2 uk-text-right uk-text-center-medium">
            <?php if($page->next->id) echo '<a href="' . $page->next->url . '">' . $page->next->title . ' <i class="uk-icon-arrow-right"></i></a>'; ?>
        </div>
    </div>
    
</article>

this will render for example the file /site/templates/fields/gallery.php

<?php if(!count($value)) return; ?>

<p class="uk-text-bold">Galerie</p>

<?php foreach($value as $item): ?>
<figure class="uk-overlay uk-overlay-hover tm-padding-gallery">
    <img class="uk-overlay-spin" src="<?= $item->size(120, 120)->url ?>" alt="<?= $item->description ?>">
    <div class="uk-overlay-panel uk-overlay-icon tm-overlay-icon-zoom uk-overlay-background uk-overlay-fade"></div>
    <a class="uk-position-cover" href="<?= $item->maxSize(800, 800)->url ?>" data-uk-lightbox="{group:'gallery<?= $page->id ?>'}" data-uk-lightbox title="<?= $item->description ?>"></a>
</figure>
<?php endforeach; ?>

 

Example for renderValue:

    <div id="tm-page-heading" class="uk-container uk-container-center">
        <div class="uk-flex uk-flex-middle uk-flex-center">
            <h1><?= $page->renderValue($page, 'title') ?></h1>
        </div>
    </div>

this will render the file /site/templates/fields/title.php (because of the "title" inside the <h1>[...]</h1>):

<?php
if($page->template == "person") echo $page->personFullName;
else echo $page->get('headline|title');

in this file you will have the $page variable available, because that was the first parameter of your rendervalue call. other examples could be:

$page->renderValue($yourarray, 'your-array-view1');
$page->renderValue($yourarray, 'your-array-view2');
$page->renderValue($booking, 'your-booking-view');

 

ps: found this one but didn't check if that answers your questions: 

 

  • Like 1
Link to comment
Share on other sites

6 hours ago, Tom. said:

I've tried:


<?=$page->render("file", "ui", array("closing" => "..."))?> 

but accessing $options['closing'] returns blank. 

$page->render("some_field") is an alias for $page->renderField(), and passing in an array of your own variables is not supported currently.

I agree that it would be useful and have opened a feature request on GitHub.

  • Like 1
Link to comment
Share on other sites

11 hours ago, bernhard said:

 


<div class="uk-overlay-panel uk-overlay-icon tm-overlay-icon-zoom uk-overlay-background uk-overlay-fade"></div>

 

Offtopic: no offense, but this is one reason why I stopped using bootstrap, uikit, foundation et al. Looks tempting at first but maintenance becomes a nightmare later.

  • Like 2
Link to comment
Share on other sites

9 hours ago, Robin S said:

$page->render("some_field") is an alias for $page->renderField(), and passing in an array of your own variables is not supported currently.

I agree that it would be useful and have opened a feature request on GitHub.

Ah, I saw it here:

 I thought maybe it was support since this example was given by @Soma 

 

EDIT: 

Added a github request: https://github.com/ryancramerdesign/ProcessWire/issues/1970

Link to comment
Share on other sites

Just for the record, $page->render('some_field') does indeed call renderField(), but only if the given param is a field name. Take a look at PageRender for more details. If you provide a filename as a param, $page->render() will use that file to render given page.

If you provide a filename (actual filename, not just something like "file") to $page->render() as the first param, you can provide an array as the second param, and the values stored in that array can then be accessed via $options.

It seems to me that you're trying to use $page->render() to something it's really not meant for: $page->render() is intended for rendering a specific page (optionally using a specific file), while wireRenderFile() renders a specific file without the need for a Page object. Those are two different use cases.

I might've gotten your intention wrong, but that's the impression I'm getting :)

  • Like 1
Link to comment
Share on other sites

3 minutes ago, Robin S said:

I think you access the array with $options rather than $page->options.

Right, corrected. We're dealing with a TemplateFile here, so it's indeed either $options or $this->options :)

Link to comment
Share on other sites

Using $page->render() to render the page with its template file (or another file you specify) has been around much longer than the $page->render('some_field') introduced in PW3 but the documentation has been a bit lacking. The cheatsheet doesn't help much and it's not covered in the new API reference - the best explanation I have found is this post from Ryan.

Personally I think it would be clearer if individual fields were rendered exclusively with $page->renderField(). Using the existing $page->render() method when that was previously used for rendering whole pages with different options (e.g. the ability to pass your own array of variables) leads to some confusion.

  • Like 1
Link to comment
Share on other sites

39 minutes ago, teppo said:

Just for the record, $page->render('some_field') does indeed call renderField(), but only if the given param is a field name. Take a look at PageRender for more details. If you provide a filename as a param, $page->render() will use that file to render given page.

If you provide a filename (actual filename, not just something like "file") to $page->render() as the first param, you can provide an array as the second param, and the values stored in that array can then be accessed via $options.

It seems to me that you're trying to use $page->render() to something it's really not meant for: $page->render() is intended for rendering a specific page (optionally using a specific file), while wireRenderFile() renders a specific file without the need for a Page object. Those are two different use cases.

I might've gotten your intention wrong, but that's the impression I'm getting :)

The Page object being accessed is the exact reason for me wanting to move over to render. Currently I'm using wireRenderFile and passing the page object as a variable. 

Link to comment
Share on other sites

You could always access the $page object using wireRenderFile

if you put the namespace in Processwire 3.x

<?php
namespace Processwire;
?>
<title> <?php echo $page->title ?> </title>

If you are in the 2.x branch use wire('page').

 

Link to comment
Share on other sites

14 minutes ago, Tom. said:

The Page object being accessed is the exact reason for me wanting to move over to render. Currently I'm using wireRenderFile and passing the page object as a variable. 

If that's the case, it seems like you could just use $page->render('filename.php', array('closing' => 'whatever')). Please let me know if I'm getting this wrong, though.

Link to comment
Share on other sites

35 minutes ago, Robin S said:

Using $page->render() to render the page with its template file (or another file you specify) has been around much longer than the $page->render('some_field') introduced in PW3 but the documentation has been a bit lacking. The cheatsheet doesn't help much and it's not covered in the new API reference - the best explanation I have found is this post from Ryan.

Personally I think it would be clearer if individual fields were rendered exclusively with $page->renderField(). Using the existing $page->render() method when that was previously used for rendering whole pages with different options (e.g. the ability to pass your own array of variables) leads to some confusion.

Agreed and agreed. To be honest I tend to prefer code over documentation (documentation gets stale, code is always relevant), but it would be nice to have these things properly documented.

While it's true that using $page->render() for multiple purposes depending on the formatting of the first param can get pretty confusing (and in my opinion that isn't really good design practice), the thing is that it can still be used just like earlier, you just need to be specific with the params you use. In other words: nothing was removed, new features were just added.

Link to comment
Share on other sites

I use wireRenderFile because it lets me organize and edit html/php code (in pocketgrid) for the front in a very easy way. Just an example: in the templates folder I have a subfolder called views where I keep the files that hold the html/php to show the webpages or parts of the webpages on the front, for example the header, topnavigation, subnavigation, picture sliders, body content, footer, etc. etc. I also have a folder like slider where I keep the html/php code for any picture slider used. Here is a simple example where I have the html/php code for the body content on the home page inside the file _home.php. And this is what I have in the homepage template file:

$view = "views/_home.php";
$layout = wireRenderFile($view, array("homepage" => $homepage, "sitepage" => $sitepage, "content" => $content));

Another advantage is that I keep my code clean and easy to view inside pocket grid. For example for a used picture slider I keep all the html/php code inside the file _slider1.php that resides inside the subfolder slider in the templates folder. This way I have no picture slider html/php code flooding my pocketgrid setup and my pocketgrid setup stays easy to over view.

<div id="pictures" class="w80 block">
<?php include($config->paths->templates . "slider/_slider1.php"); ?>
</div>

Like I said this way I can keep html/php very well organized. When I need to edit or change something I know exactly where to find and edit the code.

I dont think you can keep the same easy way of organizing your html/php in separated files _name.php when using $page->render() or $page->renderValue().

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