I'm currently using the Extended Template Parser Library for CodeIgniter. I'm running into issues when trying to template-parse nested arrays. Here is the array I am feeding the view:
Array
(
[users] => Array
(
[0] => Array
(
[id] => 42
[username] => jordanarseno
[posts] => Array
(
[0] => Array
(
[id] => 1354
[title] => foo
)
开发者_高级运维 [1] => Array
(
[id] => 1350
[title] => bar
)
)
)
)
)
I am able to render the users details...{username}
, {firstname}
etc. But I am not able to render the posts details.
My template code is straightforward and the following:
{users}
{username}
{posts}
{title}
{/posts}
{/users}
PHP throws the following errors:
A PHP Error was encountered Severity: Warning Message: preg_match_all() [function.preg-match-all]: Compilation failed: nothing to repeat at offset 2 Filename: libraries/Parser.php Line Number: 474
I'm beginning to think that the library does not support nested arrays insofar as multiple sub arrays as I need. The library comes with many tests. There is a specific test that takes:
[posts] => Array
(
[0] => Array
(
[title] => first post
[paras] => Array
(
[main] => foo
[short] => bar
)
)
And renders it in the view properly. However, as you can see, My requirements are that it go in one layer further.
Does anybody else use this library and have you run into similar issues? If not, what do you use for CodeIgniter template parsing? How nested will it go? I'm open to all suggestions and will move to another library if needed.
Codeigniter and parsers is something I personally had trouble with. Finding a well supported library isn't easy but there are some decent ones out there.
The one I use is Dwoo, It was the only one I tried that worked straight out the box. It's reasonably well supported, from my time using it; it does support nested loops and has a pretty decent caching system.. I personally don't find it as weighty as Smarty either.
The Dwoo Codeigniter wrapper is written by a guy call Phill Sturgeon and you can find it here:
https://github.com/philsturgeon/codeigniter-dwoo
You can find more information about Dwoo here:
http://wiki.dwoo.org/index.php/Main_Page
And heres a link to some of the function's it supports ( I had trouble finding this page originally, even though it's right there in the navigation pane haha )
http://wiki.dwoo.org/index.php/Plugins
And if it doesn't do what you want it to, extending it is pretty simple... It's all there in the Wiki.. Good luck!
Unfortunately, it seems that this is a bug in CodeIgniter. The earliest reference I've seen to it is 2009 (somehow I copied the wrong link here originally (and I can't find the link now)... chalk it up to exhaustion? Anyway, thanks to Wesley Murch for pointing it out), but I don't seem to see it in any bug fixes.
So, what is the problem? The numeric indexes are bothersome. There are several ways to fix it. First, you could load the view into a string, and manipulate it as well as your array, and then output the results... (sounds wretched)
Or... you can try to fix the version of CI that you have. In MY_Parser, try this (totally untested, I'm actually about to log off for the night):
function _parse_pair($variable, $data, $string)
{
$num = is_numeric($variable);
if (!$num && FALSE === ($match = $this->_match_pair($string, $variable)))
{
return $string;
}
$str = '';
foreach ($data as $row)
{
$temp = $num? $match['1']: $string;
foreach ($row as $key => $val)
{
if ( ! is_array($val))
{
$temp = $this->_parse_single($key, $val, $temp);
}
else
{
$temp = $this->_parse_pair($key, $val, $temp);
}
}
$str .= $temp;
}
if(!$num) return str_replace($match['0'], $str, $string);
return $str;
}
There is simple way to fix it: Put arrays as first variables in model
This functionality is included in the built-in template class. (I don't know if it existed when you first posted the question, but it is in there now, I use it myself.) The trick is in creating the proper array structure to pass to the parser. I tested this example in a live page just now, works 100% in CI 2.1.3.
As a specific example, consider this code which displays (with minimal formatting) a series of rooms available at a hotel, and shows, per room, the rate(s) per day for each day of your travel. (2 days, in this example.)
{available_rooms}
<p>{room_type} {total_rate}</p>
{room_rates}
<p>${room_rate} {room_date}</p>
{/room_rates}
<hr>
{/available_rooms}
With out put similar to:
1 King Bed 119.98
59.99 2013-03-23
59.99 2013-03-24
1 King Bed 119.98
59.99 2013-03-23
59.99 2013-03-24
2 Double Beds 139.98
69.99 2013-03-23
69.99 2013-03-24
2 Double Beds 139.98
69.99 2013-03-23
69.99 2013-03-24
This is the array I used successfully in this example.
> available_rooms > 0 > room_type = 1 King Bed
> available_rooms > 0 > total_rate = 119.98
> available_rooms > 0 > room_rates > 0 > room_rate = 59.99
> available_rooms > 0 > room_rates > 0 > room_date = 2013-03-23
> available_rooms > 0 > room_rates > 1 > room_rate = 59.99
> available_rooms > 0 > room_rates > 1 > room_date = 2013-03-24
> available_rooms > 1 > room_type = 1 King Bed
> available_rooms > 1 > total_rate = 119.98
> available_rooms > 1 > room_rates > 0 > room_rate = 59.99
> available_rooms > 1 > room_rates > 0 > room_date = 2013-03-23
> available_rooms > 1 > room_rates > 1 > room_rate = 59.99
> available_rooms > 1 > room_rates > 1 > room_date = 2013-03-24
> available_rooms > 2 > room_type = 2 Double Beds
> available_rooms > 2 > total_rate = 139.98
> available_rooms > 2 > room_rates > 0 > room_rate = 69.99
> available_rooms > 2 > room_rates > 0 > room_date = 2013-03-23
> available_rooms > 2 > room_rates > 1 > room_rate = 69.99
> available_rooms > 2 > room_rates > 1 > room_date = 2013-03-24
> available_rooms > 3 > room_type = 2 Double Beds
> available_rooms > 3 > total_rate = 139.98
> available_rooms > 3 > room_rates > 0 > room_rate = 69.99
> available_rooms > 3 > room_rates > 0 > room_date = 2013-03-23
> available_rooms > 3 > room_rates > 1 > room_rate = 69.99
> available_rooms > 3 > room_rates > 1 > room_date = 2013-03-24
精彩评论