Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Calling a View Function
  • Hi all- quick question. I have a view which outputs titles. I want to constrain the titles with a function I have that limits the characters. I have placed this function in my controller, however, I need to call the function in my view. How do I go about this? I did attempt to use a ViewModel, but to no avail. Any guidance would be greatly appreciated. Jeremy
  • I would have a library for common functions call it helper.php and placed it \app\classes\ then in the view you could do this:
    <?php \Helper::title('title');?>
    
  • Exactly ...
    or, if you want, call controller function directly from view with :
    <?php echo \Controller_Test::excerpt('bla bla bla'); ?>
    

    but Huzzi method is more elegant
  • Sounds great! Thanks guys. Is the process to call a model function within a controller relatively the same?
  • Davide Bellini wrote on Tuesday 17th of May 2011:

    Well, the documentation doesn't really cover making a function in the model and then returning it to the controller. For example, let's say I have a function in my model that finds a specific use based upon their ID. In my controller, I would like to pass a variable representing the user number to a function in the model, then return the result of that function in the controller to then send to the view. However, I am not sure how to do this, even after reviewing the documentation. Thanks!
  • In a model :
    <?php
    
    class Model_Test extends Model {
        
        public static function get_user($id)
        {
            $query = \DB::select('username')->from('users')->where('id', '=', $id)->execute();
            return $query->as_array();
        }
        
    }
    

    In a controller :
        public function action_test($user_id)
        {
            $user = \Model_Test::get_user($user_id);
            ...
        }
    
  • you could do this with one line of code in the controller if you use orm.
    public function action_index()&#123;
    $user = Model_User::find($id);
    }
    

    *Edit*
    Assuming you've setup the Model_User :)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion