Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Can Theme class help me in this case?
  • Hi,

    I'm having a function, which is automatically generating some HTML code for me with classes and such:

    It is hardcoded in the Model class, so when I have to display I append it to my layout content:

    $this->theme->set_partial( 'content', 'common/categories' )
                ->set( 'categories', Model_Category::generateHtml(), FALSE );
    $this->theme->set_partial( 'content', 'common/index' );

    Then in my layout.php I do simply echo $partials['content'];

    (I basically output everything from my controllers in the partial content).

    What I dislike about this, is that the HTML is hardcoded in the function, so when the time will come for the layout to be changed I would probably have to edit out the hardcoded functions as well.

    Can I somehow make the function HTML generated output configureable in my layout template so I wouldn't have to mess with hardcoded things deeply in my code?

  • HarroHarro
    Accepted Answer
    Normally, you would pass View objects to set_partial(). If the (raw) data you need to send to the view needs pre-processing, create a Presenter for that View, and pass the Presenter object to set_partial().

    So something like:

    $this->theme->set_partial(
        'content', 
        Presenter::forge('common/categories')
    )->set(
       'categories', Model_Category::getmydata(), false
    );

    Technically you can even move the model call to your presenter code, but I personally think that makes it less transparent. But you could if you want to contain it all within the "layout" part of the application.
  • This is an option, however it will still require to touch hardcoded functions but I'm assuming that is something we can't run away from.
  • If you want to process data, you have to put your code somewhere? How else do you want to do it?
  • Idk, was just wondering. Some time ago I have been looking on a bigger forum software source code, though it had it's templates stored in db. IIRC they solved it by splitting up the code onto small pieces and used it like that.

    I was just thinking of ideas to make it as much easly-configureable as possible. 
  • You can do that with Fuel as well, you only need to write a View extension for it. 

    I personally think it's a pointless excercise given your initial question, you are just moving the code snippets elsewhere, but you still have to maintain your layout and snippets in two different locations.

    The only way to have it in one place, is to use PHP templates, and add the code to the top of the file (so a mixed HTML and code file), which is really not done. And then I won't mention js files and css files, which are also elsewhere.

    You  could have both your Presenters and template views in a separate package, so you can pass that to a designer as one source package to work on. As long as the raw data input is properly documented (which could be inside the Presenter class), that should work fine.

Howdy, Stranger!

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

In this Discussion