How to use \Roots\view to include another template instead of get_template_part()

Using get_template_part() does not work with sage 10. How I can use \Roots\view to include the template parts. My current code is below which is not working

<div>
    <div>
        @if(have_rows('section'))
            @while (have_rows('section'))
              <?php
              the_row();
              $layout = get_sub_field('layout_selector')
              ?>
              @if($layout == 'hero-video')
                <?php get_template_part('resources/views/sections/section-hero-video.blade.php'); ?>
              @endif

              @if($layout == 'page-header')
                <?php get_template_part('resources/views/partials/setup/data.blade.php'); ?>
              @endif
            @endwhile
        @endif
    </div>
</div>

Could it be that you are looking for @include()

Including Sub-Views

@include('view.name')

You may also pass an array of data to the included view:

@include('view.name', ['some' => 'data'])

Check out this link

If I update my code to use @include like below

<div>
    <div>

        @if(have_rows('section'))
            @while (have_rows('section'))
              <?php
              the_row();
              $layout = get_sub_field('layout_selector')
              ?>
              @if($layout == 'hero-video')
                @include('sections.section-hero-video')
              @endif

              @if($layout == 'page-header')
                <?php get_template_part('resources/views/partials/setup/data.blade.php'); ?>
              @endif
            @endwhile
        @endif
    </div>
</div>

the page goes into infinite loading mode. Browser keeps on loading, nothing happens. No errors.

Something like this should work:

[...]
@if($layout === 'hero-video')
  {!! \Roots\view('sections.section-hero-video') !!}
@endif
[...]

You can also pass an associative array with data to use in the view as the second argument to view().

I’m fairly certain this is exactly what @include( 'sections.section-hero-video' ) does, just without the syntactic sugar.

You still have get_template_part in here.

Can you elaborate on “not working”? Are you seeing errors? If so, what? Does rendering simply stop at that point? Does the page render, but nothing is shown where you would expect the template part to appear? Something else?

Have you checked your server logs? How long have you allowed it to keep loading? Does it eventually time out?

1 Like

with the updated code below

<div>
    <div>

        @if(have_rows('section'))
            @while (have_rows('section'))
              @php
                echo "before the row";
                the_row();
                echo "after the row";
              @endphp
              <?php
              echo "coming here";
              $layout = get_sub_field('layout_selector');
              echo $layout;
              ?>
              @if($layout === 'hero-video')
                  {{"calling hero video"}}
                  {!! \Roots\view('sections.section-hero-video') !!}
              @endif

              @if($layout === 'page-header')
                  {{"calling page header"}}
                  {!! \Roots\view('sections.section-page-header') !!}
              @endif
            @endwhile
        @endif
    </div>
</div>

Here is how the page keeps loading, no output. Screenshot.. I have to restart my localhost to access the site again. Even no string gets echoed. Was also wondering when the php goes into infinite loop does it output anything before stucking into the loop on the frontend? Sorry for my crazy questions :slight_smile:

The screenshot you posted just looks like it failed to connect to the site. Why are you describing it as an infinite loop?

What’s in your server logs?

This topic was automatically closed after 42 days. New replies are no longer allowed.