Welcome Guest, Not a member yet? Register   Sign In
Template parser
#1

[eluser]Avril[/eluser]
Hello,

I like that CI has a template parser, it gives a nice code overview when your passing your data to the view.

Now I'm stuck with an array that I want to pass to the view by using the template parser off course Smile.

Here's my array code:

Code:
$data = array(
'years' => $dates['years'],
'months' => $dates['months'],
'days' => $dates['days']
);

The data in the $dates variables are from a Model.
The output of that array:

Code:
Array
(
    [years] => Array
        (
            [2010] => 2010
            [2009] => 2009
        )

    [months] => Array
        (
            [12] => 12
            [11] => 11
            [10] => 10
        )

    [days] => Array
        (
            [28] => 28
            [27] => 27
            [26] => 26
            [25] => 25
            [24] => 24
        )

)

This is the code in the Model:

Code:
function get_dates(){
        
        $sql = "SELECT date FROM dates ORDER BY date DESC";
        $result = mysql_query($sql);
        
        if($result == TRUE){
            
            $years = $months = $days = array();
            
            while($row = mysql_fetch_assoc($result)){
            
                $date_parts = explode("-", $row['date']);
                
                $years[$date_parts[0]] = $date_parts[0];
                $months[$date_parts[1]] = $date_parts[1];
                $days[$date_parts[2]] = $date_parts[2];
                
                $data = array(
                             'years' => $years,
                             'months' => $months,
                             'days' => $days
                             );
            
            }
            
            return $data;
        
        }else{
        
            echo "ERROR";
            exit;
            
        }
    
    }

I would like to pass it to the parser so that I can make 3 select drop-down, how can I do that?

Anyone an idea?
Thanks!
#2

[eluser]Phil Sturgeon[/eluser]
You'll have to create the dropdowns in the controller and assign them to $data['month_dropdown'] which is horribly un-MVC. The CI Template Parser is really just for outputting simple tags and loops, you cannot use helper functions, maths, statements, etc.
#3

[eluser]Avril[/eluser]
For the moment I did it like this, but it's really not "template friendly", but it does the trick:

Code:
<select name="day">
        &lt;?php foreach($days as $key => $val){ ?&gt;
        <option value="&lt;?php echo $val ?&gt;">&lt;?php echo $val ?&gt;</option>
        &lt;?php } ?&gt;
    </select>
    
    <select name="month">
        &lt;?php foreach($months as $key => $val){ ?&gt;
        <option value="&lt;?php echo $val ?&gt;">&lt;?php echo $val ?&gt;</option>
        &lt;?php } ?&gt;
    </select>
    
    <select name="year">
        &lt;?php foreach($years as $key => $val){ ?&gt;
        <option value="&lt;?php echo $val ?&gt;">&lt;?php echo $val ?&gt;</option>
        &lt;?php } ?&gt;
    </select>
#4

[eluser]Phil Sturgeon[/eluser]
Right, thats what most people end up doing. If you need stronger parsing then take a look at codeigniter-dwoo which will give you all the power of Dwoo in place of the CI Parser. Then you can use helper functions, do foreach, etc.
#5

[eluser]Avril[/eluser]
Hmm, I'll try it out, is there some documentation? Or does it uses the same syntax as the usual parser?
#6

[eluser]Phil Sturgeon[/eluser]
It's a Dwoo parser, so it uses Dwoo syntax.
#7

[eluser]Avril[/eluser]
Ah okay, sorry I've never heard of Dwoo, so I didn't knew it Wink




Theme © iAndrew 2016 - Forum software by © MyBB