Welcome Guest, Not a member yet? Register   Sign In
trouble with views folder
#11

[eluser]Mr-H[/eluser]
ok ,thank you boys for help
appreciate that
#12

[eluser]Kenshiro[/eluser]
CI has a very nice templating system, sure it may not be the best, but it's powerful enough.. you can use pseudo-variables, php code... and normal CI view behavior !

If i get what you said, you want to have one default template where the content changes depending on what is feed to your controller ?

Based on the principle of what pickup man said (Method 2 is the best imo, because it allows to change the view without modifying the view (albeit the use of variables here is just too hardcoded).

Say you have default_view.php (i am borrowing your code snippet for a second) :
Code:
<html>

    <head><title>{title}</title></head>
    <body>
    {content}
    </body>
</html>

Then you could have a login_view.php : (again borrowing part of your code)
Code:
<?php echo form_open('login/captcha_incor') ?>
            <?  
            echo"sign up";
            echo anchor('login/signup','retry');
            ?>
        <?php echo form_close(''); ?>
        <?php echo form_open('login/incorrectpw') ?>
        <?
         echo "incorect pw";
         echo anchor('login','retry');
        ?>
         <?php echo form_close("login/incorrectpw"); ?>
         thanks;

Then the controller :
Code:
class Myapp extends Controller {
     private $layout;

     function __construct() {
        parent::Controller;
        // For example purpose we want the app to have the same title all the time
        $this->layout['title'] = 'My Awesome APP';
     }

    function index() { // Default page
        // Example 1 : with static content
        $this->layout['content'] = 'Content for index page';
        // Example 2 : using a sub view/template, the 3rd parameter will return the content to the variable
        // and not to the output class so nothing gets displayed when using as a 3rd parameter
        $this->layout['content'] = $this->parser->parse('name_of_view', $someeventualdata, TRUE);

        // Now the "common" part to all your controller functions, that is the global output
        $this->parser->parse('default_view', $this->layout);
        // Invoking the parser here will feed the view with the global data and all {pseudo_variables} will be parsed with the correct content !
    }

    function login () {
        // Using the knowledge from the index() function now we will :
        $this->layout['title'] = 'Login Page'; // Changing the title
        $this->layout['content'] = $this->parser->parse('login_view', $someeventualdata, TRUE);
        $this->parser->parse('default_view', $this->layout);
    }

}

The part for index() is just to show that with a templating system you can just keep a main view and change content on the fly as needed (or even build sub views of sub views if you wish to do so). The main difference though then using the normal $this->load->view, is that pseudo variables are available and thus allow to be more versatile (no hardcoded variables in the view, and the pseudo variable can be anything computed on the fly)

There is a little loss of pure speed when using the parser class (which is loaded by default by the way) but it allows cleaner code in the views IMO, CI being very versatile itself it still allows php to be executed from the template !

The principle of MVC is to separate actions in different functions, by trying to have different views inside index() function might be a bad idea, as it adds cluttering.

And finally (sorry for this very long post btw), if you wish to have different output inside the same function, you should use a model for that, depending on what method was called from the model the output can then be built and fed to the parser !

PS: I am not totally sure it was what you really wanted, but i hope it helped none the less, in case you need to do some advanced templating one day !
#13

[eluser]Mr-H[/eluser]
thank you very much Kenshiro
now i know what that all of this mean
ps: that exactly what i wante thanks...




Theme © iAndrew 2016 - Forum software by © MyBB