简体   繁体   中英

CodeIgniter function form_open() in Smarty template

I recently started working with the Smarty Template system in my CodeIgniter projects. All works fine and I was able to echo strings and dates. But now I have a problem with the 'form_open()' function.

view_login.tpl

<form class="contact-form" action="login/process" method="post">
                        <div class="row">
                            <div class="form-group">
                            <label for="username">USERNAME</label>
                                <input type="text" class="form-control" id="username" placeholder="Enter username">
                            </div>

                            <div class="form-group">
                            <label for="password">PASSWORD</label>
                                <input type="password" class="form-control" id="password" placeholder="Enter Password">
                            </div>

                            <button type="submit" class="btn btn-flat flat-color">LOGIN</button>
                            <button type="button" class="btn btn-flat pull-right">Forgot your password?</button>
                        </div>
                    </form>

As soon as I replace the HTML tags the view doesn't load anymore. I need the form_open() because of the Cross Site Request Forgery (CSRF).

So far I've tried:

{ form_open('login/process') }
{ form url='login/process' }
{{ form_open('login/process') }}
{php} echo form_open('login/process'); {/php}

Anyone else had the same problem or knows how to fix this? Without Smarty I never had problems with this.

Solved!

I autoloaded the form helper file.

$autoload['helper'] = array('url', 'form');

And then I used the following code:

{form_open('login/process')}
                        <div class="row">
                            <div class="form-group">
                            <label for="username">USERNAME</label>
                                <input type="text" class="form-control" id="username" placeholder="Enter username">
                            </div>

                            <div class="form-group">
                            <label for="password">PASSWORD</label>
                                <input type="password" class="form-control" id="password" placeholder="Enter Password">
                            </div>

                            <button type="submit" class="btn btn-flat flat-color">LOGIN</button>
                            <button type="button" class="btn btn-flat pull-right">Forgot your password?</button>
                        </div>
                    {form_close()}

Looking at the code given above, it does not seem that the reason why the error occurred was because of the not loading the helper functions, though that may be one of the reason.

I believe the error is mainly because you added spaces between the delimiters { and } .

The default value in Smarty for the right and left delimiters are { and } respectively. Meaning:

{form_open('')} // Ok!
{ form_open('') } // Not Ok!

So, if you would like to use the second version, you will have to change your smarty settings for the right and left delimiter:

$smarty->left_delimiter = '{ ';
$smarty->right_delimiter = ' }';

Also, when you tried

{php} echo form_open('login/process'); {/php}

I believe it did not work because you are using Smarty 3. The {php} tag is deprecated in Smarty 2 and removed in Smarty 3. If you still want to use the {php} tag, you would have to use SmartyBC.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM