Welcome Guest, Not a member yet? Register   Sign In
Template parser and form helper problem
#1

[eluser]esthonwood[/eluser]
Hi, I want to use the template parser in all my views but when I use the set_select function, it doesn't show the selected option. The set_value function works perfectly though. Here's the code:
Code:
<p>
            <label for="email">E-mail:</label>
            &lt;input type="text" id="email" name="email" value="&lt;?php echo set_value('email', '{email}'); ?&gt;"&gt;
        </p>
        <p>
            <label for="admin_type">Admin Type:</label>
            <select name="admin_type" id="admin_type">
                {admin_types}
                <option value="{admin_type}" &lt;?php echo set_select('admin_type', '{admin_type}'); ?&gt; >{type_display}</option>
                {/admin_types}
            </select>
        </p>
#2

[eluser]Aken[/eluser]
PHP is processed before the parser class, so that unfortunately won't work.
#3

[eluser]esthonwood[/eluser]
Here's my work around. I created a function to create the dropdown, assign it in a template variable in the controller, and display it in the template. Here's the function

Code:
public function create_dropdown($select_name, $options) {
        $dropdown = "<select name=\"$select_name\" id=\"$select_name\">";
        foreach ($options as $key => $value) {
            $dropdown .= '<option value="'.$key.'" '.set_select($select_name, $key).'>'.$value.'</option>';
        }
        $dropdown .= "</select>";
        return $dropdown;
    }
#4

[eluser]Aken[/eluser]
CI has a helper function that does that already: form_dropdown(). But glad you found a solution that works.
#5

[eluser]esthonwood[/eluser]
[quote author="Aken" date="1343157773"]CI has a helper function that does that already: form_dropdown(). But glad you found a solution that works.[/quote]

Yes, I used the form_dropdown() function before but then it's not template parser ready. Anyway, this is just me being O.C. lol!
#6

[eluser]Aken[/eluser]
What do you mean? Based on your described solution, it would be exactly the same thing. All form_dropdown() does is return a string, exactly like the function you created.
#7

[eluser]esthonwood[/eluser]
[quote author="Aken" date="1343182458"]What do you mean? Based on your described solution, it would be exactly the same thing. All form_dropdown() does is return a string, exactly like the function you created.[/quote]

You know what, you're right. I just want my views variables to be in this format: {variable_name} but as you can see, I have used the set_value() function which defeats the purpose. I guess the CI template parser cannot yet be used for the purpose I have intended.
#8

[eluser]Aken[/eluser]
As it is, no, the template parser is just for very basic string replacements. And it is not designed to be integrated directly with PHP (that's the point of template processors in the first place, anyway - avoiding PHP code to make things simple).

set_value() can easily be assigned to a template variable. form_dropdown() can be assigned to a variable as a whole, or you can generate your own HTML and assign it where necessary (maybe you'll want to assign only the <option> list to a variable).

You can always extend the template parser as well, to include your own functionality.
#9

[eluser]esthonwood[/eluser]
[quote author="Aken" date="1343183740"]As it is, no, the template parser is just for very basic string replacements. And it is not designed to be integrated directly with PHP (that's the point of template processors in the first place, anyway - avoiding PHP code to make things simple).

set_value() can easily be assigned to a template variable. form_dropdown() can be assigned to a variable as a whole, or you can generate your own HTML and assign it where necessary (maybe you'll want to assign only the <option> list to a variable).

You can always extend the template parser as well, to include your own functionality.[/quote]

I'm rewriting my code to assign the form helper functions to a template variable. it will add more lines of code but that's fine. Thanks Smile




Theme © iAndrew 2016 - Forum software by © MyBB