![]() |
Can you load data into a view, then parse it? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Can you load data into a view, then parse it? (/showthread.php?tid=22924) |
Can you load data into a view, then parse it? - El Forum - 09-23-2009 [eluser]Braden Schaeffer[/eluser] Having some trouble trying to figure this out. Let's say you have something like this stored in a database: Code: <h1>{Title}</h1> How would you go about loading that string from the database into another view file, then parsing that view file? This is not the method I am using to deliver html to my view files, I just want to know how to do this (or if you even can). I've been trying for a while off and on and I can't seem to figure out how to get it to work. Any suggestions? I'd really appreciate any help you could pass on! Thanks in advance! Can you load data into a view, then parse it? - El Forum - 09-23-2009 [eluser]skunkbad[/eluser] I think it's the wrong way to store data, and the wrong way to use views. 1) The data goes in the database. 2) The HTML goes in a view If you need a view in a view, then load the inner view, and set the 3rd parameter to TRUE. Can you load data into a view, then parse it? - El Forum - 09-23-2009 [eluser]Colin Williams[/eluser] I would overload the Parser class and add a method like parse_string() that will do this for you (unless an undocumented method like this already exists! I'm too lazy to look). All the code is right there, you'll just need to abstract it a bit. Best of luck. Can you load data into a view, then parse it? - El Forum - 09-23-2009 [eluser]Braden Schaeffer[/eluser] Thanks for the quick replies. @Colin That's what I was thinking... so I looked at the Parser library and it's so easy to implement I can't believe its not an option. All you have to do is remove/change the line where $template is loaded as a view. I added a parameter called $is_string so you still have the option of parsing a view file. Here's the edited Parser.php parse function: Code: function parse($template, $data, $return = FALSE, $is_string = TRUE) It's working now, thanks for the suggestion!!! Can you load data into a view, then parse it? - El Forum - 09-23-2009 [eluser]Colin Williams[/eluser] I figured it would be as such. I'm averse to adding parameters to functions, though. I'd leave your code there alone, but write a parse_str($data, $return) method that essentially just returns the call to your modified function. Code: function parse_str($data, $return = FALSE) ... simply for the sake of an intuitive API. And thanks for sharing your result. Maybe you will consider a Wiki entry with your ultimate solution? This isn't the first time I've seen this question asked here. |