dongqintong8972 2016-04-14 02:33
浏览 64
已采纳

Twig / Symfony表单模板语法

I'm trying to modify someone else's website setup (a Craft CMS with a Laravel plugin) but I can't decipher the syntax of this Twig form, I was hoping someone more familiar with Twig/Symfony form templating could tell me what's going on or point me to the right documentation.

Specifically, I have a variable I want to render as a placeholder in an input. So {{ myvariable }} should be <input type="text" placeholder="{{ myvariable }}" ..., but I'm unfamiliar with how the inputs are being rendered.

Here's what's in the Twig template:

{% include "dir/content_type/_field" with {
   control: { handle: 'location_address_1', value: 'address_1'},
    element: content_type,
    errors: errors is defined ? errors : null
 } only %}

The _field I assume is a nearby html template, which has:

{% set field = craft.fields.getFieldbyHandle(control['handle']) %}
{% set fieldtype = craft.fields.populateFieldType(field, element) %}
{% set value = element ? element[control['value']] : null %}
{% set required = required is defined ? required : false %}
{% set input = fieldtype.getInputHtml(field.handle, value) %}
{% set label = no_label is defined and no_label ? '' : field.name|t|e%}
{% set errors = errors is defined and errors[control['value']] is defined ?     errors[control['value']] : null %}
{% include "_includes/forms/field" with {
  label: label,
  id: field.handle,
  errors: errors,
  input: input,
  required: required
} only %}

Any insight is appreciated.

  • 写回答

1条回答 默认 最新

  • douzhulv1699 2016-04-14 08:13
    关注

    The _field template is setting some parameters for the field based on the initial include call. The _field processes some of those parameters and then calls the _includes/forms/field with the processed values and field html in the input variable.

    craft.fields.getFieldbyHandle() gets your field object. The from that object It looks like fieldtype.getInputHtml(field.handle, value)` is generating the actual HTML for the input. So you need to find where getInputHtml is defined and pass through your placeholder to there. Change that function to build the html with you placeholder attribute.

    Something like so

    final template

    {% include "dir/content_type/_field" with {
        control: { handle: 'location_address_1', value: 'address_1', placeholder: "[Place holder value]"},
        element: content_type,
        errors: errors is defined ? errors : null     
     } only %}
    

    _field

    ...
    {% set input = fieldtype.getInputHtml(field.handle, value, control['placeholder']) %}
    

    getInputHTML(handle,value, placeholder)

    <input type="text" placeholder="{{ placeholder }}" ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#linux#的问题(输入输出错误):出现这个界面接着我重新装系统,又让修电脑的师傅帮我扫描硬盘(没有问题)用着用着又卡死(相关搜索:固态硬盘)
  • ¥15 cv::resize不同线程时间不同
  • ¥15 web课程,怎么做啊😭没好好听课 根本不知道怎么下手
  • ¥15 做一个关于单片机的比较难的代码,然后搞一个PPT进行解释
  • ¥15 python提取.csv文件中的链接会经常出现爬取失败
  • ¥15 数据结构中的数组地址问题
  • ¥15 maya的mel里,怎样先选择模型A,然后利用mel脚本自动选择有相同名字的模型B呢。
  • ¥15 Python题,根本不会啊
  • ¥15 会会信号与系统和python的来
  • ¥15 关于#python#的问题