Cakephp 3多个定制模板formhelpers [英] Cakephp 3 multiple custom template formhelpers

查看:426
本文介绍了Cakephp 3多个定制模板formhelpers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在工作(使用敏感数据,我可以为后人的缘故添加),以及决定我们需要使用Cakephp 3.0(目前为beta)所有功能强大且记录最少的新工具的能力。

So I'm at work (working with sensitive data might I add for posterity's sake), and the powers that be decide we need to use the all powerful and least documented new tool by Cakephp 3.0 (beta at this time).

编辑:我的目标是为表单创建几个不同的模板,通过formhelper模板或输入法调用。真的没有太多的一个很好的例子。
自定义模板FormHelper使用:
从书中可以看出(互联网上的任何地方都没有),因此非常简短的文档是:
http://book.cakephp.org/3.0/en/core- libraries / helpers / form.html#customizing-the-templates-formhelper-uses

My goal is to create several different templates for forms to call through the formhelper template or input methods. There really isn't much of an a good example for this. Customizing the Templates FormHelper Uses: As seen in the book(and nowhere else on the internet anywhere) the very short documentation is thus: http://book.cakephp.org/3.0/en/core-libraries/helpers/form.html#customizing-the-templates-formhelper-uses

网站表示您可以使用模板方法, use:

The site says you can use the template method and then give a vague "use":

$myTemplates = [
    'inputContainer' => '<div class="form-control">{{content}}</div>',
];

$ this-> Form-> templates($ myTemplates);

$this->Form->templates($myTemplates);

然后它说,你可以使用input()方法,它没有给它没有例子。
最后但并非最不重要的自定义模板FormHelper应该允许你make或创建许多这些自定义formhelpers你想要的,但他们没有给出示例使用如何做到这一点! lulwut?

Then it says you can use the input() method for which it gives no example. And last but not least the custom template FormHelper should allow you to "make" or "create" as many of these custom formhelpers as you wish, but they give no example use of how to do that!? lulwut?

我可以像他们的例子一样轻松地使用它,但在一个自定义模板中的强大在哪里?

I can easily use it once like their example, but where's the power in a single custom template? That doesn't do me anygood.

所以通过一个新的可能的解决方案,我试着得到一个新的错误。

So by a new possible solution I try and get a new error.

我得到这个错误(在我的看法)(从下面的代码):

I get this error(within my view)(from the following code):

致命错误
错误:类' >

Fatal Error Error: Class 'Configure' not found

//within bootstrap.php
Configure::write('templates', [
    'shortForm' => [
        'formstart' => '<form class="" {{attrs}}>',
        'label' => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>',
        'input' => '<div class="col-md-4"><input type="{{type}}" name="{{name}}" {{attrs}} /></div>',
        'select' => '<div class="col-md-4"><select name="{{name}}"{{attrs}}>{{content}}</select>    </div>',
        'inputContainer' => '<div class="form-group {{required}}" form-type="{{type}}">{{content}}    </div>',
        'checkContainer' => '',],
    'longForm' => [
        'formstart' => '<form class="" {{attrs}}>',
        'label' => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>',
        'input' => '<div class="col-md-6"><input type="{{type}}" name="{{name}}" {{attrs}} /></div>',
        'select' => '<div class="col-md-6"><select name="{{name}}"{{attrs}}>{{content}}</select>    </div>',
        'inputContainer' => '<div class="form-group {{required}}" form-type="{{type}}">{{content}}    </div>',
        'checkContainer' => '',],
    'fullForm' => [
        'formstart' => '<form class="" {{attrs}}>',
        'label' => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>',
        'input' => '<div class="col-md-10"><input type="{{type}}" name="{{name}}" {{attrs}} />    </div>',
        'select' => '<div class="col-md-10"><select name="{{name}}"{{attrs}}>{{content}}</select>    </div>',
        'inputContainer' => '<div class="form-group {{required}}" form-type="{{type}}">{{content}}    </div>',
        'checkContainer' => '',]
]);

//within my view
<?php
    $this->Form->templates(Configure::read('templates.shortForm')); 
?>

旧更新:我添加了

use "Cake\Core\Configure;" 

在我看来,一切都很好,但我想添加到相应的文件层次结构,所以我不必添加到每个视图,

in my view and everything works great, but I would like to add this to the appropriate file in the hierarchy so that I don't have to add that to every view,

这是除非当然会导致整个应用程序的整体效率问题。有谁知道应该进入哪个文件?问候和TIA!

that is unless of course it causes efficacy issues for the entire app as a whole. Does anyone know which file it should go in? Regards and TIA!

最新更新:我只是想出来。很简单!检查我的答案下面!希望这帮助了某人

Newest Update: I just figured it out. So simple! check my answer below! Hope this helped someone

推荐答案

这个修复是允许你有自定义模板形式(从cakephp 3 !!!!)使用引导。如果你想使用窗体助手和所有的好处(安全和什么不是)设置大小。

What this fix does is allow you to have custom template forms (from cakephp 3!!!!) using bootstrap. If you want to set sizes using the form helper and all of it's goodness (security and what not).

Cakephp的Jose Zap告诉我尝试bootstrap插件和小部件,什么不是,但是真正的方法应该是这样:

Jose Zap of Cakephp told me to try bootstrap plugins and widgets and what not, but the real way to do this should have been this:

步骤1:创建config / templatesConfig.php并添加您的自定义表单内容。

Step 1: Create config/templatesConfig.php and add your custom form stuff.

<?php
$config = [
    'Templates'=>[
        'shortForm' => [
            'formStart' => '<form class="" {{attrs}}>',
            'label' => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>',
            'input' => '<div class="col-md-4"><input type="{{type}}" name="{{name}}" {{attrs}} /></div>',
            'select' => '<div class="col-md-4"><select name="{{name}}"{{attrs}}>{{content}}</select></div>',
            'inputContainer' => '<div class="form-group {{required}}" form-type="{{type}}">{{content}}</div>',
            'checkContainer' => '',],
        'longForm' => [
            'formStart' => '<form class="" {{attrs}}>',
            'label' => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>',
            'input' => '<div class="col-md-6"><input type="{{type}}" name="{{name}}" {{attrs}} /></div>',
            'select' => '<div class="col-md-6"><select name="{{name}}"{{attrs}}>{{content}}</select></div>',
            'inputContainer' => '<div class="form-group {{required}}" form-type="{{type}}">{{content}}</div>',
            'checkContainer' => '',],
        'fullForm' => [
            'formStart' => '<form class="" {{attrs}}>',
            'label' => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>',
            'input' => '<div class="col-md-10"><input type="{{type}}" name="{{name}}" {{attrs}} /></div>',
            'select' => '<div class="col-md-10"><select name="{{name}}"{{attrs}}>{{content}}</select></div>',
            'inputContainer' => '<div class="form-group {{required}}" form-type="{{type}}">{{content}}</div>',
            'checkContainer' => '',]
    ]
];

步骤2:从正确的视图的控制器里面调用这一行。

Step 2: From your controller inside the method for the correct view call this line.

不要忘记在控制器顶部添加它

Don't forget add this on the top of your controller

use Cake\Core\Configure;

$this->set('form_templates', Configure::read('Templates'));

步骤3:在bootstrap.php文件中添加此文件

Step 3: Add this within the bootstrap.php file

// Load an environment local configuration file.
// You can use this file to provide local overrides to your
// shared configuration.

Configure::load('templatesConfig','default'); //fixed

步骤4(最后):添加此行与您想要的模板名称Bam!

Step 4(finally): Add this line with the template name you want Bam!.

<?php $this->Form->templates($form_templates['shortForm']); ?>

这篇关于Cakephp 3多个定制模板formhelpers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆