lonnieezell / Ocular-Template-Library

A template/layout library for CodeIgniter
http://igniteyourcode.com/ocular
89 stars 23 forks source link

Block's Don't Accept Data from the View #12

Closed shaunandrews closed 13 years ago

shaunandrews commented 13 years ago

Hopefully the title here isn't too confusing, and this is more a request than an issue.

Right now, Block's can be rendered using data provided by the Controller, but not by the parent View. For instance, I have the following:

<ol>
<?php foreach ($entries as $entry) { ?>
    <li>
        <h3>$<?= $entry->amount; ?></h3>
        <h2><?= $entry->description; ?></h2>
        <p><?= $entry->budget; ?></p>
    </li>
<?php }; ?>
</ol>

I was hoping to be able to extract the HTML for a single entry, and use it as a Block. I was hoping that I could updated my View to be:

<ol>
<?php foreach ($entries as $entry) { ?>
    <?= $this->template->block('single_entry', 'entry/single'); ?>
<?php }; ?>
</ol>

With my single.php view (partial?) containing:

 <li>
    <h3>$<?= $entry->amount; ?></h3>
    <h2><?= $entry->description; ?></h2>
    <p><?= $entry->budget; ?></p>
</li>

However, it appears that the $entry variable isn't passed to the block. Not sure if I'm missing something.

It would be really great to have support for something similar to rails collections, using the format:

<ol>
<?= $this->template->block('single_entry', 'entry/single', $entries as $entry); ?>
</ol>

Anyways. Great work! I'm loving Ocular!

lonnieezell commented 13 years ago

First - glad you're enjoying Ocular. I almost can't imagine working without it anymore. :)

The times I've done things like this, I haven't needed the blocks feature. Instead I've just used CI's standard:

$this->load->view('single_entry', $entries)

And looped inside of the 'single_entry' file.

What you're recommending make sense, though I'm not sure that I see the need to complicate things by having Ocular perform the loop, when a simple PHP (or even a template parser) call will do the trick.

I will add in the ability to pass data to the block, though.

lonnieezell commented 13 years ago

Version 2.13 (available in just a few minutes) includes two items for you:

First: you can now pass an array into the block() method.

Second: A new function, called themed_view(), which acts like a helper function, just like the breadcrumb function. It takes the path to a view, relative to the theme folder, and a data array, then renders the view based on the current theme. This was a function that I needed recently while working on a mobile version of a site where blocks felt too heavy handed.

That should handle most of your needs, right there. Let me know if something broke. I ran some quick tests and everything appears to be working, but you never know. :)

shaunandrews commented 13 years ago

Awesome stuff, Lonnie!

However, the updates to the block don't seem to do what I need—I'm passing an object to the block, but it appears that it will only work with an array. In any event, I took your original advice and use the Loader class to call the view and passing an object. I originally tried this, but was unaware that this ran a PHP extract, and output the object fields as variables, So $object->field becomes $field. Cool stuff.

Again, thanks for the great work!