iXanadu's avatar

Include menu.blade multiple times with different variables - help please

I have a blade file, that accepts some variables and builds a dropdown list of action items. The action is an onClick that takes the current menu item, and inserts it into a target field. This will be reused several times in the parent blade file. On each @include, I specify a different target field. The problem I'm running into, is that the included blade file, is exactly the same, as if it isn't be reparsed with new data, each time it is included. This might be "as designed" but that would seem to defeat the purpose of reusable blade components. Thus, I must be doing something wrong. Code snippets below, I can provide more if necessary.

<div class="form-group">
        {{ Form::label('description', 'Description:') }}
        {{ Form::text('description', null, ['class' => 'form-control']) }}
        @include('mergefields', ['insertFldId'=>"description",'mergeFldId'=>"mergefields",'mergefields' => 'mergefields', ])
    </div>
    <!--  Form Input -->
    <div class="form-group">
        {{ Form::label('subject', 'Email Subject:') }}
        {{ Form::text('subject', null, ['class' => 'form-control']) }}
        @include('mergefields', ['insertFldId'=>"subject",'mergeFldId'=>"mergefields",'mergefields' => 'mergefields', ])
    </div>

Here is the relevant part of mergefields.php

                     @foreach($mergeflds as $type => $fields)
                        <div class="btn-group" role="group">
                            <button id="btnGroupVerticalDrop1" type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> {{ $type }}
                                <span class="caret"></span>
                            </button>
                            <ul class="dropdown-menu" aria-labelledby="btnGroupVerticalDrop1">
                            @foreach($fields as $label => $mergefld)
                                <li>
                                    <a href="#" onClick='updateMergeField("{{$insertFldId}}","{{$mergeFldId}}","{{$mergefld}}")'>{{$label}}</a>
                                </li>
                                @endforeach
                            </ul>
                        </div>
                    @endforeach

You can see that "insertFldId" is different for each rendering of the mergefields.blade.php file. But looking at the resultant html, and function, it is using "description" in both instances. What am I doing wrong?

0 likes
3 replies
zachleigh's avatar

Tet using the variable instead of a string in the include statement.

@include('mergefields', ['insertFldId'=>$subject,'mergeFldId'=>$mergefields,'mergefields' => $mergefields ])
iXanadu's avatar

@zachleigh

Thank you for the troubleshooting tip. I did this, and I'm either still goofing it up, or the second include is fetching the cache of the previous call. See code snippet:

<!--  Form Input -->
    <div class="form-group">
        {{ Form::label('description', 'Description:') }}
        {{ Form::text('description', null, ['class' => 'form-control']) }}
        @set($insertFldId,"description")
        <h3>insertFldId: {{$insertFldId}} </h3>
        @include('mergefields', ['insertFldId'=>$insertFldId,'mergeFldId'=>"mergefields",])
    </div>
    <!--  Form Input -->
    <div class="form-group">
        {{ Form::label('subject', 'Email Subject:') }}
        {{ Form::text('subject', null, ['class' => 'form-control']) }}
        @set($insertFldId,"subject")
        <h3>insertFldId: {{$insertFldId}} </h3>
        @include('mergefields', ['insertFldId'=>$insertFldId,'mergeFldId'=>"mergefields",])
    </div>

I borrowed some code to from a recent post here in the discussions to create a set directive. I set a new variable ( one that was not passed in from the controller) to the target insertFldId, prior to calling the include. To ensure that the set command worked, I output the new variable's value in an H3 Tag.

Result:

<h3>description</h3>
menu html with "description"

<h3>subject</h3>
menu html with "description"

The second call should show subject. It would be helpful for me to know if this is expected behavior (I'm troubleshooting the wrong issue), and if it is, can I provide some parameters that says "parse/compile" this with fresh parameters.

iXanadu's avatar
iXanadu
OP
Best Answer
Level 2

Solved - and an apology.

I was on a wild goose chase, and blade really was rendering each instance of the include with different data. The problem was that in the include, the button definition to pop up the menu (a modal div) was the same for both instances. So when I press the button under the "description" field, it called "mymodal". When I clicked the button under the "subject" field, it called mymodal again. When troubleshooting my code, I did "not" view-source like I should have, or I would have saved many hours. I simply "inspected" the element in the modal box, and saw that it was wrong. When I viewed the entire page source, I see two instances of the "mymodal" content, with different variable substitutions but the same div name. My browser just rendered the first one it saw. The fix was very simple, the lesson learned was expensive.

<button class="btn btn-default btn-xs" type="button" data-toggle="modal" data-target="#myModal-{{$insertFldId}}">Merge Fields</button


<div id="myModal-{{$insertFldId}}" class="modal fade" role="dialog">

Please or to participate in this conversation.