Template loop with same argument, over and over

Look at this first:

<?php foreach( $page->children() as $item ) : ?>
  <div class="test">
    <?php my_function('haha', $item); ?>
    <?php my_function('hehe', $item); ?>
    <?php my_other_function('hoho', $item); ?>
    <!-- 10 more functions like this -->
  </div>
<?php endforeach; ?>
my_function($str, $item) {
  // MAKE ITEM WORK WITHOUT SENDING IT ALL THE TIME
  return $item->title() . $str;
}

Maybe it’s not so Kirby specific, but it’s a Kirby template/snippet.

Let’s say I have functions that I want to run over and over with different arguments, but I want to use $item in all of them.

What is the most elegant way of having $item sent to the function without the need to send it over and over? What I can think of is global variable or singleton, but both feels like bad practice.

What would you do?

Possible solution. What do you think? Good or bad practice?

Template

<?php $page->data = $item; ?>
<?php my_function('hehe'); // I don't need to send an object here ?>

Function

my_function( $str ) { // I don't need to have an object as param here
  $item = page()->data();
  return $item->title() . $str;
}

In short, I use the $page object and fill that with whatever I need.

I don’t think, that I’m a very experienced programmer, but I think, that functions should not have an internal state. Im my view, objects and it’s methods have an internal state, but not functions.

So I would stick with passing the same object every time, the function is called. It also shouldn’t make a difference in performance, of you pass the object per reference.
Otherwise I would create my own class and object with all my functions as methods.

In your solution, it is not clearly visible, which dependencies / real parameters your functions have. I don’t think, that this is a good practice.

1 Like

While I’m far from being an experienced programmer, I agree with @jbeyerstedt on that. After a while you don’t know what the function does anymore.

I also don’t see an advantage of not passing the object as parameter, apart from having a bit less to type.

1 Like

Otherwise I would create my own class and object with all my functions as methods.

That’s what I ended with doing. Having classes that do most of the hard work.

I also don’t see an advantage of not passing the object as parameter, apart from having a bit less to type.

Maybe I will have 200 functions on different places on the site and I want to keep the template as clean as possible, keep things DRY.

I also have many arguments with every function so I try to keep them down as much as possible.

I use classes and my own solution for have the $item object magically in my function, as well as a $blueprint object.

It may not suit everyone, but I’ll see if I like this when I’m done with it.

Thanks @jbeyerstedt and @texnixe! :slight_smile:

What about page methods? :wink:

1 Like

@lukasbestle: I knew you can’t live without the forum - not even on a bday :joy:

1 Like