乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      php – 如何在symfony表單中選擇顯示字體真棒圖標

       印度阿三17 2019-06-28

      我想在Symfony Form Builder的選擇選項中顯示所有字體真棒圖標.

      我添加選擇字段:

      $choices = $this->getFontAwesome();
      $form->add( $key, ChoiceType::class, array('label' => 'Texte', 'choices' => $choices, 'attr' => array('class' => "fa" ) ) );
      

      我的函數getFontAwesome();

          public function getFontAwesome(){
      
          $webroot = $this->get('kernel')->getRootDir() . '/../web';
          $pattern = '/\.(fa-(?:\w (?:-)?) ):before\s {\s*content:\s*"\\\\(. )";\s }/';
          $subject =  file_get_contents( $webroot . '/assets/vendor/font-awesome/css/font-awesome.css');
          preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);
          foreach($matches as $match) {
              $icons[$match[1]] = '&#x' . $match[2] . ';' ;
          }
      
          return $icons ;
      
      }
      

      但是在選擇字段中,看不到圖標:

      字段顯示代碼而不是圖標

      enter image description here

      我該怎么辦?
      我嘗試htmlspecialschars和其他(htmlentities,..)但不工作.

      解決方法:

      如果您沒有使用任何js插件,如Select2Bootstrap-select,那么您有http:///NyL7d/這種可能性,但我們需要稍微努力才能達到它.

      首先,要說使用< i class =“fa fa-heart”>< / i>因為標簽不是一個選擇,因為<選項> element不能包含任何子元素,只能包含文本. (see related issue)

      為了可重用性,讓我們構建一個名為“IconChoiceType”的表單類型作為“ChoiceType”的子類:

      namespace AppBundle\Form\Type;
      
      use Symfony\Component\Form\AbstractType;
      use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
      use Symfony\Component\Form\FormInterface;
      use Symfony\Component\Form\FormView;
      use Symfony\Component\OptionsResolver\OptionsResolver;
      
      class IconChoiceType extends AbstractType
      {
          /**
           * Cache for multiple icon fields or sub-requests.
           * 
           * @var array
           */
          private $choices;
      
          private $kernelRootDir;
      
          public function __construct($kernelRootDir)
          {
              $this->kernelRootDir = $kernelRootDir;
          }
      
          public function buildView(FormView $view, FormInterface $form, array $options)
          {
              // Pass this flag is necessary to render the label as raw. 
              // See below the twig field template for more details.
              $view->vars['raw_label'] = true;
          }
      
          public function configureOptions(OptionsResolver $resolver)
          {
              $resolver->setDefaults([
                  'attr' => [
                      // It's the key of the solution and can be done in many ways.
                      // Now, the rendered <select> element will have a new font.
                      'style' => "font-family: 'FontAwesome';"
                  ],
                  'choices' => $this->getFontAwesomeIconChoices(),
              ]);
          }
      
          public function getParent()
          {
              return ChoiceType::class;
          }
      
          protected function getFontAwesomeIconChoices()
          {
              if (null !== $this->choices) {
                  // don't to load again for optimal performance.
                  // useful for multi-icon fields and sub-requests.
                  return $this->choices;
              }
      
              // BTW we could configure the path to the "font-awesome.css".
              $fontAwesome = file_get_contents($this->kernelRootDir.'/../web/assets/vendor/font-awesome/css/font-awesome.css');
      
              // this regular  only works with uncompressed version (not works with "font-awesome.min.css")
              $pattern = '/\.(fa-(?:\w (?:-)?) ):before\s {\s*content:\s*"\\\\(. )";\s }/';
      
              if (preg_match_all($pattern, $fontAwesome, $matches, PREG_SET_ORDER)) {
                  foreach ($matches as list(, $class, $code)) {
                      // this may vary depending on the version of Symfony, 
                      // if the class name is displayed instead of the icon then swap the key/value
                      $this->choices['&#x'.$code.';'] = $class;
                  }
              }
      
              return $this->choices;
      
          }
      }
      

      和他們各自的注冊服務:

      # app/config/service.yml
      services:
          app.form.icon_choice_type:
              class: AppBundle\Form\Type\ChoiceIconType
              # Symfony has already a container parameter to the kernel root directory.
              arguments: ['%kernel.root_dir%']
              tags:
                  - { name: form.type }
      

      好吧,到目前為止,沒有任何結果與你的不同.

      <select id="form_icon" name="form[icon]" style="font-family: 'FontAwesome';">
          <option value="fa-glass">&#xf000;</option>
          <option value="fa-music">&#xf001;</option>
          ...
      </select>
      

      enter image description here

      現在問題在哪里? < select>字體系列已準備就緒,但是它們沒有顯示的圖標,為什么?

      默認情況下,在Symfony中,Twig環(huán)境會轉義使用htmlspecialchars(more details)呈現的所有值,因此我們只需要為此表單類型覆蓋此行為.為此,我們在app / Resources / views / form目錄中創(chuàng)建了fields.html.twig模板,并將此代碼復制到:

      {# app/Resources/views/form/fields.html.twig #}
      
      {# 
         here isn't need to create the expected `icon_choice_widget` like shown 
         the documentation, because this looks equal to `choice_widget` from 
         `ChoiceType`, only we need overwrite the block that renders the label. 
       #}
      
      {%- block choice_widget_options -%}
          {% for group_label, choice in options %}
              {%- if choice is iterable -%}
                  <optgroup label="{{ choice_translation_domain is same as(false) ? group_label : group_label|trans({}, choice_translation_domain) }}">
                      {% set options = choice %}
                      {{- block('choice_widget_options') -}}
                  </optgroup>
              {%- else -%}
      
                  {# this line has been overwritten, see {{- block('choice_option_label') -}} to end #}
                  <option value="{{ choice.value }}"{% if choice.attr %} {% set attr = choice.attr %}{{ block('attributes') }}{% endif %}{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{- block('choice_option_label') -}}</option>
      
              {%- endif -%}
          {% endfor %}
      {%- endblock choice_widget_options -%}
      
      {%- block choice_option_label -%}
          {# this block has been called from choice_widget_options block #}
      
          {%- if raw_label|default(false) -%}
              {# the label is rendered as raw when IconChoiceType is used #}
              {{ choice_translation_domain is same as(false) ? choice.label|raw : choice.label|trans({}, choice_translation_domain)|raw }}
          {%- else -%}
              {{ choice_translation_domain is same as(false) ? choice.label : choice.label|trans({}, choice_translation_domain) }}
          {%- endif -%}
      {%- endblock -%}
      

      請注意,{{choice.label | raw}} raw filter將存儲的原始文本(防止被轉義)顯示為標簽,在本例中為圖標字體內容.

      最后,你需要注冊表格主題,如描述documentation

      # app/config/config.yml
      
      {# ... #}
      
      twig:
          form_themes:
              - 'form/fields.html.twig'
      

      結論:

      $form->add('icon', IconChoiceType::class);
      

      enter image description here

      來源:https://www./content-1-275001.html

        本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯系方式、誘導購買等信息,謹防詐騙。如發(fā)現有害或侵權內容,請點擊一鍵舉報。
        轉藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多