Welcome Guest, Not a member yet? Register   Sign In
Grabbing and echoing only fragments of views
#1

[eluser]Dave Stewart[/eluser]
Hi All,

I'm still at the "poking around" stage of using CI, so apologies in advance that I'm not backing up this question with concrete development just yet (that will come).

Obviously, using load->view(), one can load separate elements of a page to build up a full page, eg:

Code:
load->view('header', $data);
load->view('body', $data);
load->view('footer', $data);

And you would expect to store these as separate HTML fragments in individual files.

However, what I want to know is if it's possible at the moment (or if would have to extend to View class to add my own functionality) to add fragments of an individual HTML / PHP page, for example:

Code:
load->view('master', 'header', $data);
load->view('master', 'body', $data);
load->view('master', 'footer', $data);

My reasoning for this is that I would like to store complete HTML pages (with full CSS etc) that I can edit in a WYSIYWG editor, then only extract the body of each one perhaps when loading views.

Thanks in advance for your help,
Dave
#2

[eluser]Seppo[/eluser]
If its gonna be HTML pages edited with WYSIWYG I would use something like..
Code:
$this->output->append_output(file_get_contents('/path/to/file'));
#3

[eluser]Dave Stewart[/eluser]
Hi Seppo,
Thanks for the reply, but perhaps I wasn't clear enough Smile

file_get_contents() won't work for me, as I still want to use CI's code parsing features via load->view(), I just want to be able to grab a section of a view template, perhaps using delimiters, or the contents of a tag (ie, <body>) rather than an entire view file.

What I would need to do could be something like:

1 - calling a file_get_contents(), then
2 - grab the section I needed using substr(), then
3 - pass that to the view to parse tags / variables

I just want to get some ideas from more experienced CI developers before I rush out and code something up.

Cheers,
Dave
#4

[eluser]flojon[/eluser]
Why don't you use the whole file as a single view instead? I see no reason to tear the file apart just to put it back together again. Just because you can split views into subparts doesn't mean you have to...

Just my $0.02

Regards,
Jonas
#5

[eluser]Dave Stewart[/eluser]
Well, the most obvious answer would be hard-coded items, such as static text, and file references such as CSS.
I like to design in a page that actually works, rather than some bastardised fragment.

Granted, I could design in another file / folder and when done I could grab the snippet I need, and re-save as view fragment, but the separation of the HTML from its formatting information seems brutal to me.

Why not enable the view to live as a stand-alone document if it needs be, and provide the option to just lift what's necessary out of it?

On top of that, one view could hold multiple elements (which perhaps could be cached somehow):

Code:
load->view('master', 'header', $data);
load->view('master', 'main', $data);
load->view('master', 'sidebar', $data);
load->view('master', 'feature', $data);
load->view('master', 'footer', $data);

Am I really being odd here!?

Cheers,
Dave
#6

[eluser]flojon[/eluser]
Have you looked at the template parser?
Template Parser Class
#7

[eluser]Dave Stewart[/eluser]
Hi flojon,

Thanks for the reply, but I think you're not quite understanding what I'm trying to do.

What I want to do is to allow just a portion, or slice, of a view to be loaded into a controller, rather than the whole thing (using a template is just a PHP-free method of rendering a view).

Cheers,
Dave
#8

[eluser]flojon[/eluser]
To do that you would have to have some sort of HTML parser, which you probably have to create yourself.

What do you want do with the piece or slice after you've loaded it?


Regards,
Jonas
#9

[eluser]Dave Stewart[/eluser]
Hi Jonas,

[quote author="flojon" date="1214593748"]you would have to have some sort of HTML parser[/quote]

That's right. It's fairly trivial to create something like that anyway.

Once I have my slice, I want to hand it to load->view() (or some derivative I create) to parse any variables, then have it added to the output as normal.

I'm poking around in Loader.php, and it looks like I'd have to extend it and override the following functions:

Code:
view()
_ci_load()

Hopefully it will work out OK. I'll have a tinker over the weekend (hopefully I won't break anything). If you know why this may not work, let me know now!

Cheers,
Dave
#10

[eluser]flojon[/eluser]
It may work, I just wasn't sure you know what you wanted... :-)

Good luck!

regards,
Jonas




Theme © iAndrew 2016 - Forum software by © MyBB