Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
about theme in fuelphp
  • hiiii;
    i try to use theme in fuelphp , but i'don't know how to use it, where i should place my thems??
    please i need help , it's urgent ...;)
  • ilNotturnoilNotturno
    Accepted Answer
    You can put it wherever you want, the folder of the themes must be setted up in the "config/theme.php" file in the "paths" variable.

    I suggest you to put them in the app folder, at the same level of the "view" folder.

    Themes work in the same way of views, except with theme you can use "partials": you can think about them as "views in views".

    I suggest you to read the documentation, it is a very big argument.
  • thanks ilNotturno ,
    but the link documentation dose'nt work .



  • Thanks, I've edited the answer
  • ilNotturno  plz , give me example for how to use themes in fuelphp, i have 4 days to complete my project .. 
  • Have you read the documentation? Because there there is a complete example of theme... Never mind, follow these steps:

    1) Setup main theme configuration in your theme.php file under the config folder. A complete list of variable is here (scroll down to Configuration topic).

    2) Create a Controller that extends the base controller. Here is an example from documentation:
    <span class="preprocessor">&lt;?php</span>
    <span class="keyword">class</span> Homepage <span class="keyword">extends</span> \Controller
    {
    <span class="comment">/**
    * load the theme template, set the page title and the menu's
    */</span>
    <span class="keyword">public</span> <span class="keyword">function</span> before()
    {
    <span class="comment">// load the theme template</span>
    <span class="variable">$this</span>-&gt;theme = \Theme::instance();

    <span class="comment">// set the page template</span>
    <span class="variable">$this</span>-&gt;theme-&gt;set_template(<span class="string">'layouts/homepage'</span>);

    <span class="comment">// set the page title (can be chained to set_template() too)</span>
    <span class="variable">$this</span>-&gt;theme-&gt;get_template()-&gt;set(<span class="string">'title'</span>, <span class="string">'My homepage'</span>);

    <span class="comment">// set the homepage navigation menu</span>
    <span class="variable">$this</span>-&gt;theme-&gt;set_partial(<span class="string">'navbar'</span>, <span class="string">'homepage/navbar'</span>);

    <span class="comment">// define chrome with rounded window borders for the sidebar section</span>
    <span class="variable">$this</span>-&gt;theme-&gt;set_chrome(<span class="string">'sidebar'</span>, <span class="string">'chrome/borders/rounded'</span>, <span class="string">'partial'</span>);

    <span class="comment">// set the partials for the homepage sidebar content</span>
    <span class="variable">$this</span>-&gt;theme-&gt;set_partial(<span class="string">'sidebar'</span>, <span class="string">'homepage/widgets/login'</span>);
    <span class="variable">$this</span>-&gt;theme-&gt;set_partial(<span class="string">'sidebar'</span>, <span class="string">'homepage/widgets/news'</span>)-&gt;set(<span class="string">'users'</span>, Model_News::latest(<span class="number">5</span>));

    <span class="comment">// call the user model to get the list of logged in users, pass that to the users sidebar partial</span>
    <span class="variable">$this</span>-&gt;theme-&gt;set_partial(<span class="string">'sidebar'</span>, <span class="string">'homepage/widgets/users'</span>)-&gt;set(<span class="string">'users'</span>, Model_User::logged_in_users());
    }

    <span class="comment">/**
    * A simple example. A normal action method would probably have code to
    * retrieve data from models and pass this to a partial view...
    */</span>
    <span class="keyword">public</span> <span class="keyword">function</span> action_index()
    {
    <span class="comment">// the homepage has a flash image banner</span>
    <span class="variable">$this</span>-&gt;theme-&gt;set_partial(<span class="string">'banner'</span>, <span class="string">'homepage/banner'</span>);

    <span class="comment">// a block of static content</span>
    <span class="variable">$this</span>-&gt;theme-&gt;set_partial(<span class="string">'banner'</span>, <span class="string">'homepage/content'</span>);

    <span class="comment">// and two link lists and a copyright block</span>
    <span class="variable">$this</span>-&gt;theme-&gt;set_partial(<span class="string">'footerleft'</span>, <span class="string">'homepage/shortcuts'</span>);
    <span class="variable">$this</span>-&gt;theme-&gt;set_partial(<span class="string">'footercenter'</span>, <span class="string">'homepage/links'</span>);
    <span class="variable">$this</span>-&gt;theme-&gt;set_partial(<span class="string">'footerright'</span>, <span class="string">'homepage/copyright'</span>);
    }

    <span class="comment">/**
    * keep the after() as standard as possible to allow custom responses from actions
    */</span>
    <span class="keyword">public</span> <span class="keyword">function</span> after(<span class="variable">$response</span>)
    {
    <span class="comment">// If no response object was returned by the action,</span>
    <span class="keyword">if</span> (<span class="keyword">empty</span>(<span class="variable">$response</span>) <span class="keyword">or</span> ! <span class="variable">$response</span> instanceof Response)
    {
    <span class="comment">// render the defined template</span>
    <span class="variable">$response</span> = \Response::forge(\Theme::instance()-&gt;render());
    }

    <span class="keyword">return</span> <span class="keyword">parent</span>::after(<span class="variable">$response</span>);
    }
    }
    Don't change the after methods, you have to modify only the before and index method:

    A) Put in your before method every "global" part of the template; for example, if you have a footer, set it as "partial" of the template, so it can be showed in the page.

    B) In the index action get the data you need (from db, file or more) and use them as data of the view; for example, if you are creating a blog, here you can get the content and put them in a partial.

    3) Print the view: create a view for every partial you setted up and put them in the folder you have specified at point 1): here in the views you can print and show the data you save in the index action.
  • Do you have a link to this 'example' you talk about? The code you posted is messed up by the forum and i an not find it in the documentation. There is only information about configuring the Theme, using function of a theme but there is no explanation about where to put the files (in app/views or in app/classes/view or in /app/themes/ ?? There is no working example to back-engineer from. How does a theme file look like? How does a theme.info file look like? Kinda frustrating.
  • You can place the files where you want.

    You tell the Theme class where they are using the 'paths' and 'assets_folder' configuration items in the theme config file. In addition to that, Theme behaves a bit different depending on whether or not your theme folders are inside the DOCROOT or not. This is mainly relevant to assets, and documented on that same page.

    If you use modules, you can throw that in the mix too using the 'use_modules' configuration item, which will automatically prefix theme views you load with the name of the current module.

    In my applicaties, I have themes split in two:
    - assets live in DOCROOT/themes/<themename>/img, js, css, ...
    - views live in APPPATH/themes/<themename>
    - I use 'use_modules'

    So my theme.php config file contains

        'paths' => array(
            APPPATH.'themes',
        ),
        'assets_folder' => 'themes',
        'use_modules' => true,

  • As for your last question: a theme info file follows the same structure and rules as a config file. So if it's a .php file, it should return an array.

    The Theme class itself doesn't use it at all. It's there to facilitate installable themes, so you can use it to store stuff like author, name, any optional configuration items (perhaps a color selection?), etc.
  • Ok, great, thank you.

    I still have one question regarding modules. At the moment, i have a module called main. Inside that module i have created a view called index (that is used by the main controller / index action).

    Is it possible to overrule this index view file by the theme? For example; 

    /themes/default/modules/main/main/index.php 
    overrules
    /modules/main/views/main/index.php

    Or, next example for a (dynamic) page controller in the main module:
    /themes/default/modules/main/page/index.php 
    overrules
    /modules/main/views/page/index.php

    This way i can keep the default view files for fallback and use the theme's view files for complete control inside a theme?
  • Yes, that's how the theme works.

    But only if you use the Theme class, the View class is unware something like a theme exists, and will only load from the views folder. The Theme class will search the theme folder structure first, and will fall back to regular views if nothing could be found.
  • The problem is that at the moment i have a module called main with a controller called main. The view 'index' is also inside the main module folder app/modules/main/views/main/index.php. Now, when i create a app/themes/default/modules/main/main/index.php file, it is not being picked up. Fuel still picks the one from the module. This is what i do in the main controller.

    public function action_index()
    {
    return $this->theme->set_partial('content','main/index');
    }

    Then, in my layout i call the partial just like i should (the partials are stored inside  $partials):
    <?php echo $partials['content'].PHP_EOL; ?>
  • Have you enabled "use_modules" in the theme config? It is required to have the Theme class prefix the view path with the module name, so to change "main/index" to "main/main/index".

    Note that this will not work if you do cross module calls (calling a controller in a module from app or another module instead of using HMVC). The Theme class uses the request context to determine the current module name (if any).
  • Thanks for your answer.

    I changed
    return $this->theme->set_partial('content','main/index');

    to
    return $this->theme->set_partial('content','main/main/index');

    But still no effect. 

    While i have a index.php file in /themes/default/modules/main/main/index.php .. 
    i get the requested view could not be found error. use_modules is set to true. I've tried many different configurations (putting index.php a level lower), changing the partial view path .. it's all not working :(

    Yes, i do extend Controller_Frontend from the app inside the Controller_Main of the main module.
  • But Controller_Main is the one called from the URL (like http://yourhost/main/main/method)? Or is it called some other way?
  • Yes, it is called like that :), for the sake of simplicity; here is my GIT repository:

  • Hmm, weird, I use it exactly like you, using 'main/index', and use_modules = true.

    Perhaps it's time for some debugging to figure out where it goes wrong. Theme has a method called find_file() which does all the forensic work, figuring out which file to load.

    Add some var_dumps or logging to see which paths are checked, perhaps that can point to where it goes wrong?
  • Thanks for all your time. I found out what the problem is. 

    My theme files for modules where inside
    /themes/default/modules/<module_name>/.... 

    while it should be just in
    /themes/default/<module_name>/....

    Would've been great if i could put them inside the modules folder but it is ok for now.
  • Glad you've found the issue.

    You worry about conflicts with app views?

    I've actually never thought of that, I don't have (user accessable) controllers in my app, so I've never bumped into that issue.

    I've just commited this feature to 1.6/develop.
  • No problem, no worries. I'm not planning to make user accessible controllers in my app. I tend to use modules (portability). But if someone wants to, then there would be a conflict anyway (between app and module).
  • How to call theme from module controller?

    My Module
    /fuel/app/modules/blog

    My Controller
    /fuel/app/modules/blog/classes/controller/admin/index

    My code in this controller.
    $theme = \Theme::instance();
    return $theme->view('admin/blog_v', $output, false);

    my theme folder structure
    /fuel/app/modules/blog/views/admin/blog_v.php << this one works!
    /public/theme/system/modules/blog/admin/blog_v.php << this does not work! file not found.

    How to make theme folder structure correctly or how to call theme from current module?
  • I think it's your theme config file,

    you need to set :

    'active' => 'system',
    'view_ext' => '.php',
    'paths' => array(
    DOCROOT.'themes',
    ),
    'use_modules' => 'modules',
  • Oh Yes!

    thank you Syntaxlb

Howdy, Stranger!

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

In this Discussion