Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How access to theme variables in template ?
  • Hello !

    I begin with FuelPHP and I have a little problem :

    I have a controller named "private" that all my others controllers extend, like it's explicated by MarcoPace : http://fuelphp.com/forums/discussion/11521/share-your-knowledge#Comment_11798

    In my controller named "Controller_Private", I declare some variables for my theme :

            // Set variables
            $this->data['template_values']['title'] = 'Biarritz By Locals';
            $this->data['template_values']['subtitle'] = 'Default subtitle';
            $this->data['template_values']['description'] = 'Default description';
            $this->data['template_values']['keywords'] = 'Default keywords';
            $this->data['template_values']['segments'] = $segments;
     
            // Set template
            $this->theme->set_partial('header', 'public/_partials/_header');
            $this->theme->set_partial('menu', 'public/_partials/_menu');
            $this->theme->set_partial('footer', 'public/_partials/_footer');

    And in my "after" method, I do :

    $this->theme->set_info('data', $this->data);


    For the partials, in my template, I can access them with : partials['header'] for example, this it's ok.

    But for my variables like title, description or keywords, I don't know how access to them.

    Thank you for your help.
  • HarroHarro
    Accepted Answer
    The info block isn't really for passing data to a theme, it's more meant to contain information about the theme, or info needed to configure the theme (for example, if it comes in multiple colours).

    The theme layout, and all partials, are all View objects, and passing data to them works exactly the same.

    You either pass data to the View when it gets forged, using the set() or set_safe() method of the View object, or set_global() if the data has to be accessable from any view. With set_global(), is doesn't matter on which View object you call it.

    How you get to the View object depends on what data you want to set where.

    You can change the above code too:

    $this->theme->set_partial('header', \View::forge('public/_partials/_header', $this->data));

    if you want to pass data to a specific View partial. You can also do:

    $this->theme->set_partial('header', 'public/_partials/_header');
    $this->theme->get_partial('header')->set($this->data);

    If you want to set it after you have defined the partial.

    Or set it global (in this example on the page template):

    $this->theme->get_template()->set_global($this->data);

    It all depends on your requirements, and how you have set up the request flow in your controller. Check the Theme and View sections in the docs to see what methods are available, and what they do.
  • Ok, I understand perfectly.

    Thank you very much for your quick answer !

Howdy, Stranger!

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

In this Discussion