从控制器获取树枝的属性

时间:2018-12-27 20:58:39

标签: php symfony drupal twig

我正在使用Drupal,并为html模板设置了树枝。我是这个项目的新手,看到有一个控制器,该控制器具有查询数据库的功能,然后它们返回该查询以获取课程页面属性,并且我看到属性在相应的course.twig.html中显示的位置。

我看不到它们是如何连接的以及twig.html如何知道其属性从何而来。我正在尝试向页面添加更多属性,但看不到该项目如何将mysql查询传递给树枝模板。

{#
/**
 * @file
 * Default theme implementation to display a node.
 *
 * Available variables:
 * - node: Full node entity.
 *   - id: The node ID.
 *   - bundle: The type of the node, for example, "page" or "article".
 *   - authorid: The user ID of the node author.
 *   - createdtime: Time the node was published formatted in Unix timestamp.
 *   - changedtime: Time the node was changed formatted in Unix timestamp.
 * - label: The title of the node.
 * - content: All node items. Use {{ content }} to print them all,
 *   or print a subset such as {{ content.field_example }}. Use
 *   {{ content|without('field_example') }} to temporarily suppress the printing
 *   of a given child element.
 * - author_picture: The node author user entity, rendered using the "compact"
 *   view mode.
 * - metadata: Metadata for this node.
 * - date: Themed creation date field.
 * - author_name: Themed author name field.
 * - url: Direct URL of the current node.
 * - display_submitted: Whether submission information should be displayed.
 * - attributes: HTML attributes for the containing element.
 *   The attributes.class element may contain one or more of the following
 *   classes:
 *   - node: The current template type (also known as a "theming hook").
 *   - node--type-[type]: The current node type. For example, if the node is an
 *     "Article" it would result in "node--type-article". Note that the machine
 *     name will often be in a short form of the human readable label.
 *   - node--view-mode-[view_mode]: The View Mode of the node; for example, a
 *     teaser would result in: "node--view-mode-teaser", and
 *     full: "node--view-mode-full".
 *   The following are controlled through the node publishing options.
 *   - node--promoted: Appears on nodes promoted to the front page.
 *   - node--sticky: Appears on nodes ordered above other non-sticky nodes in
 *     teaser listings.
 *   - node--unpublished: Appears on unpublished nodes visible only to site
 *     admins.
 * - title_attributes: Same as attributes, except applied to the main title
 *   tag that appears in the template.
 * - content_attributes: Same as attributes, except applied to the main
 *   content tag that appears in the template.
 * - author_attributes: Same as attributes, except applied to the author of
 *   the node tag that appears in the template.
 * - title_prefix: Additional output populated by modules, intended to be
 *   displayed in front of the main title tag that appears in the template.
 * - title_suffix: Additional output populated by modules, intended to be
 *   displayed after the main title tag that appears in the template.
 * - view_mode: View mode; for example, "teaser" or "full".
 * - teaser: Flag for the teaser state. Will be true if view_mode is 'teaser'.
 * - page: Flag for the full page state. Will be true if view_mode is 'full'.
 * - readmore: Flag for more state. Will be true if the teaser content of the
 *   node cannot hold the main body content.
 * - logged_in: Flag for authenticated user status. Will be true when the
 *   current user is a logged-in member.
 * - is_admin: Flag for admin user status. Will be true when the current user
 *   is an administrator.
 *
 * @see template_preprocess_node()
 *
 * @todo Remove the id attribute (or make it a class), because if that gets
 *   rendered twice on a page this is invalid CSS for example: two lists
 *   in different view modes.
 *
 * @ingroup themeable
 */
#}

{# {{ kint() }} #}

<article id="node-{{ node.id }}" {{ attributes }}>
{{node}}
  {{ title_prefix }}
  {% if not page %}
    <h2{{ title_attributes }}>
      <a href="{{ url }}" rel="bookmark">{{ label }}</a>
    </h2>
  {% endif %}
  {{ title_suffix }}

  {% if node.field_packaging.value == '1' %}

    {% set image = content.field_image %}
    {% set ce = content.field_tax_credit_hours %}
    {% set goal = content.field_goal %}
    {% set target_audience = content.field_audience %}
    {% set objectives = content.field_objectives %}
    {% set accreditation = content.field_accreditation %}
    {% set disclosure = content.field_disclosure_statement %}
    {# {% set references_old = content.field_references %} #}
    {% set references = content.field_references_par %}
    {% set appendix = content.field_appendix %}
    {% set faculty = content.field_faculty %}
    {% set related_courses = content.field_related_courses %}
    {# set suggested_courses = content.field_suggested_courses #}
    {% set additional = content.field_callout %}
    {% set expiration = node.field_expiration.value %}

    <div class="course-summary row">
      {% if image|render %}
        <div class="course-image small-6 small-offset-3 medium-4 medium-offset-0 columns">
            {{ image }}
        </div>
        <div class="small-12 medium-8 columns">
      {% else %}
        <div class="small-12 columns">
      {% endif %}
        <div class="course-introduction">
          <p>Welcome to <em>{{ label }}</em>.</p>
        </div>
        {% if not resource  %}
          {% if ce.0|render %}
            <small class="credit-hours inline-label">Credit hours: {{ ce.0 }} CE</small>
          {% endif %}
        {% endif %}
        <div class="enroll">
          {% if signIn == "yes"  %}
           {% if regis == true %}
              <a href="appLms/index.php?modname=course&op=aula&idCourse={{ content.field_docebo_course_id.0 }}" class="button large">Go</a>
           {% else %}
              <a href="#" id="regis" rel="{{ content.field_docebo_course_id.0 }}" class="button large">Enroll</a>
           {% endif %}
          {% else %}
            <a href="dashboard/login?lms_id={{ signURL }}" class="button large">Enroll</a>
          {% endif %}
        </div>
      </div>
    </div>

    {% if goal|render or objectives|render %}

      <ul class="tabs some-tabs" data-responsive-accordion-tabs="tabs small-accordion medium-tabs" data-allow-all-closed="true" data-multi-expand="true" id="course-tabs">
        <li class="tabs-title is-active"><a href="#overview" aria-selected="true">Overview</a></li>

        {% if appendix|render %}
          <li class="tabs-title"><a href="#appendix">Appendix</a></li>
        {% endif %}

        {% if references|render %}
          <li class="tabs-title"><a href="#references">References</a></li>
        {% endif %}

        {% if faculty|render %}
          <li class="tabs-title"><a href="#faculty">Faculty</a></li>
        {% endif %}
      </ul>

      <div class="tabs-content" data-tabs-content="course-tabs">

        <div id="overview" class="tabs-panel is-active course-overview">

          {{ additional|render ? additional }}

          {% if goal|render %}
            <h2 class="field-label">{{ node.field_goal.fielddefinition.label }}</h2>

            {{ goal }}
          {% endif %}

          <h2 class="field-label">{{ node.field_audience.fielddefinition.label }}</h2>

          {% if target_audience|render %}
            {{ target_audience }}
          {% else %}
            <p>interested health-care professionals.</p>
          {% endif %}

          {% if objectives|render %}
            <h2 class="field-label">{{ node.field_objectives.fielddefinition.label }}</h2>
            <p>After completing the activities of this module, you will be able to:</p>
            {{ objectives }}
          {% endif %}

          {% if expiration|render %}
            <p>Please note this module expires on <strong>{{ expiration|date('n/j/Y') }}</strong>.</p>
          {% endif %}

          {% if accreditation|render or disclosure|render %}

            <div class="course-supplements">

              {% if accreditation|render %}

                <h2 class="supplement-title"><a href="#accreditation" class="callout-link" data-toggle="accreditation">Accreditation Statement</a></h2>

                <div id="accreditation" class="callout callout-arrow is-hidden" data-toggler=".is-hidden">

                  {% for i, value in accreditation %}
                    {% set acc_item = node.field_accreditation[i].entity %}
                    {% if acc_item %}
                      <h3>{{ acc_item.title.value }}</h3>
                      {% autoescape false %}
                        {{ acc_item.body.value|replace({'{{ #.## }}': ce.0|escape }) }}
                      {% endautoescape %}
                    {% endif %}
                  {% endfor %}

                  <button class="close-button" data-toggle="accreditation">&times;</button>
                </div>

              {% endif %}

              {% if disclosure|render %}

                <h2 class="supplement-title"><a href="#disclosure" class="callout-link" data-toggle="disclosure">Disclosures</a></h2>

                <div id="disclosure" class="callout callout-arrow is-hidden" data-toggler=".is-hidden">
                  {{ disclosure }}
                  <button class="close-button" data-toggle="disclosure">&times;</button>
                </div>

              {% endif %}

            </div>

          {% endif %}

        </div>

        {% if appendix|render %}

          <div id="appendix" class="tabs-panel course-appendix">

            {% if appendix %}

              <ul class="tabs tabs-style-text" data-tabs id="course-appendix-tabs">
                {% for i, value in node.field_appendix.value %}
                  {% set ref_title = node.field_appendix[i].entity.field_title.value %}
                  {% if ref_title %}
                    <li class="tabs-title{{ i == 0 ? ' is-active'}}"><a href="#{{ ref_title|clean_class }}" aria-selected="{{ i == 1 ? 'true' : 'false' }}">{{ ref_title }}</a></li>
                  {% endif %}
                {% endfor %}
              </ul>

              <div class="tabs-content" data-tabs-content="course-appendix-tabs">
                {% for i, value in node.field_appendix.value %}
                  <div id="{{ node.field_appendix[i].entity.field_title.value|clean_class }}" class="tabs-panel{{ i == 0 ? ' is-active'}}">
                    {% autoescape false %}
                      {{ node.field_appendix[i].entity.field_content.value }}
                    {% endautoescape %}
                  </div>
                {% endfor %}
              </div>

            {% endif %}

          </div>

        {% endif %}

        {% if references|render %}

          <div id="references" class="tabs-panel course-references">

            {% if references %}

              <ul class="tabs tabs-style-text" data-tabs id="course-references-tabs">
                {% for i, value in node.field_references_par.value %}
                  {% set ref_title = node.field_references_par[i].entity.field_title.value %}
                  {% if ref_title %}
                    <li class="tabs-title{{ i == 0 ? ' is-active'}}"><a href="#{{ ref_title|clean_class }}" aria-selected="{{ i == 1 ? 'true' : 'false' }}">{{ ref_title }}</a></li>
                  {% endif %}
                {% endfor %}
              </ul>

              <div class="tabs-content" data-tabs-content="course-references-tabs">
                {% for i, value in node.field_references_par.value %}
                  <div id="{{node.field_references_par[i].entity.field_title.value|clean_class}}" class="tabs-panel{{ i == 0 ? ' is-active'}}">
                    {% autoescape false %}
                      {{ node.field_references_par[i].entity.field_content.value }}
                    {% endautoescape %}
                  </div>
                {% endfor %}
              </div>

            {% endif %}

          </div>

        {% endif %}

        {% if faculty|render %}

          <div id="faculty" class="tabs-panel course-faculty">

            <div class="sections">

              {{ faculty }}

            </div>

          </div>

        {% endif %}

      </div>

    {% endif %}

    {% if related_courses|render %}

      {% set ancillary_1 = node.field_related_courses.fielddefinition.label %}
      {% set ancillary_2 = 'Guidance' %}
      {# set ancillary_2 = node.field_suggested_courses.fielddefinition.label #}

      <ul class="sections sections-border accordion mobile-accordion stacked-tabs" data-accordion data-allow-all-closed="true" data-multi-expand="true">

        <li class="section accordion-item" data-accordion-item>

          <a href="#" class="accordion-title show-for-small-only">{{ ancillary_1 }}</a>

          {% if related_courses|render %}

            <div class="tiles mobile-accordion-content" data-tab-content>

              <h2 class="field-label section-title hide-for-small-only">{{ ancillary_1 }}</h2>

              <div class="list-items list-format-3">
                {{ related_courses }}
              </div>

            </div>

          {% endif %}

        </li>

        <li class="section accordion-item" data-accordion-item>

          <a href="#" class="accordion-title show-for-small-only">{{ ancillary_2 }}</a>

          <div class="tiles mobile-accordion-content" data-tab-content>

            <h2 class="field-label section-title hide-for-small-only">{{ ancillary_2 }}</h2>

            <div class="list-items list-format-1 list-type-guidance">
              {{ drupal_view('content_feed', 'guidance') }}
            </div>

          </div>

        </li>

      </ul>

      {{ drupal_view('content_feed', 'guidance_modal') }}

    {% endif %}

  {% else %}

    {{ content.body }}

  {% endif %}

</article>

2 个答案:

答案 0 :(得分:2)

您应该搜索预处理钩子(函数yourmodule_preprocess ...)。如果您使用的是drupal8,则可以这样设置新变量:

"@babel/core": "^7.2.2",
"babel-preset-minify": "^0.5.0",
"babelify": "^10.0.0",
"browserify": "^16.2.3",
"gulp": "^4.0.0",
"tsify": "^4.0.1",
"typescript": "^3.2.2",
"vinyl-source-stream": "^2.0.0"

在树枝上,可以这样称呼

$vars['myvar'] = 'test';

重要提示:它将无法在所有功能中使用。我曾经使用过预处理功能,但是感谢@leymannx,我们知道它也可以与其他钩子一起使用:

  

“不仅预处理钩子可以将变量传递给模板。   hook_theme,然后在您的控制器主题中输出并传递   变量和模板”

答案 1 :(得分:-2)

在控制器中渲染树枝模板时,还可以将一些变量作为数据传递。如果要在树枝模板中显示该数据,请在树枝函数和语言构造内使用它们的名称来命名它们。

就是这样。

由于您的问题很笼统,我无能为力。