![]() |
Dynamic pseudo variables template parses - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22) +--- Thread: Dynamic pseudo variables template parses (/showthread.php?tid=44403) |
Dynamic pseudo variables template parses - El Forum - 08-13-2011 [eluser]MaartenDeGroote[/eluser] Hi all, This is probably kinda simple but I can't seem to get the hang of it. I want to make use of the template parsing class and the pseudo-variables. Now, let's say I had the following code: Code: {blog_entries} Instead of using the word "blog_entries", I want to make use of a dynamic key. For example: if I had a table with image gallery names and each gallery contains images, then I wanted the "blog_entries" variable to be replaced by the name of the gallery. Like this: Code: {vacations} Where "variations" and "portraits" are variable names, selected from a database table. I know it is a bit confusing, but I hope I get the message across! Thanx alot! Dynamic pseudo variables template parses - El Forum - 08-13-2011 [eluser]Aken[/eluser] There's two ways I've found you can do this: The first way is kind of a fun trick, but may not be the most reliable. The template parser library goes through the loop of data to parse in order. You can double brace a variable and it will be parsed twice, as long as you put your data variables in the correct order. Example: Code: // VIEW What happens is the variable {varname} is replaced, leaving the variable structure {variable} left in the view. Then, on the next iteration, {variable} is replaced with the appropriate content. I'm not sure how fool proof this is, but it does heavily rely on having your variable put in a specific order, which may or may not be difficult in your situation. The other method is to set any variables you want to use and pass them to a view. You then return the view as a string instead of loading it, and use $this->parser->parse_string() instead of parse(). Code: // VIEW Hope that helps you out. |