Welcome Guest, Not a member yet? Register   Sign In
Can you load data into a view, then parse it?
#1

[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>
<p>{Content}</p>

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!
#2

[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.
#3

[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.
#4

[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)
    {
        $CI =& get_instance();
        
        if($is_string == FALSE)
        {    
            $template = $CI->load->view($template, $data, TRUE);
        }
        
        if ($template == '')
        {
            return FALSE;
        }
        
        foreach ($data as $key => $val)
        {
            if (is_array($val))
            {
                $template = $this->_parse_pair($key, $val, $template);        
            }
            else
            {
                $template = $this->_parse_single($key, (string)$val, $template);
            }
        }
        
        if ($return == FALSE)
        {
            $CI->output->append_output($template);
        }
        
        return $template;
    }

It's working now, thanks for the suggestion!!!
#5

[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)
{
    return $this->parse('', $data, $return, TRUE);
}

... 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.




Theme © iAndrew 2016 - Forum software by © MyBB