I'm using a View to show my create form that its quite long.
Inside this form i'm using many times the same pieces of code which is mainly divs with class names and it looks like this.
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>**First name:** <span class="text-danger">*</span></label>
{{ Form::text('firstname',null,array('class' =>'form-control required','placeholder' =>'John'))}}
</div>
</div>
</div>
I just want to change dynamically the First name: on my label and <input>
I've tried to reuse this code with #yield and #section but if it exists more than 1 times in the same file i get the same results as the first one.
<!-- First Name -->
#section('form_input_wrapper_label', 'First Name:')
#section('form_input_wrapper_input_area')
{{ Form::text('firstname',null,array('class' =>'form-control required','placeholder' =>'John'))}}
#endsection
#include('theme_components.form_input_wrapper')
<!-- Last Name -->
#section('form_input_wrapper_label', 'Last Name:')
#section('form_input_wrapper_input_area')
{{ Form::text('lastname',null,array('class' =>'form-control required','placeholder' =>'Smith'))}}
#endsection
#include('theme_components.form_input_wrapper')
Is there any Laravel way to approach this issue?
You can use #include directive and pass variables you want:
#include('form.view', ['firstName' => $var])
Also, as #Dmytrechko mentioned in his comment, you can use #each directive if you want to iterate over an array or a collection:
#each('form.view', $names, 'firstName')
Then just move your code to new form.view and use $firstName variable as usual:
<label>First name: <span class="text-danger">{{ $firstName }}</span></label>
Related
I want to display multiple charts in a single page or different pages. How can I reuse the blade file instead of repeating/retyping the code?
I created a plain blade file _chart-widget.blade.php and I want the variable value to change depending on the page, or depending on what I want to set the variable in each <section> of a page
<!--begin::Charts Widget 1-->
<div class="card {{ $class ?? '' }}">
<!--begin::Header-->
<div class="card-header border-0 pt-5">
<!--begin::Title-->
<h3 class="card-title align-items-start flex-column">
<span class="card-label fw-bolder fs-3 mb-1">Recent Statistics</span>
<span class="text-muted fw-bold fs-7">More than 400 new members</span>
</h3>
<!--end::Title-->
<!--begin::Toolbar-->
<div class="card-toolbar">
<!--begin::Menu-->
<button type="button" class="btn btn-sm btn-icon btn-color-primary btn-active-light-primary" data-kt-menu-trigger="click" data-kt-menu-placement="bottom-end">
{!! theme()->getSvgIcon("icons/duotune/general/gen024.svg", "svg-icon-2") !!}
</button>
{{ theme()->getView('partials/menus/_menu-1') }}
<!--end::Menu-->
</div>
<!--end::Toolbar-->
</div>
<!--end::Header-->
<!--begin::Body-->
<div class="card-body">
<!--begin::Chart-->
<div id="kt_charts_widget_1_chart" style="height: 350px"></div>
<!--end::Chart-->
</div>
<!--end::Body-->
</div>
<!--end::Charts Widget 1-->
How can I make the code above dynamic and reusable when I #include it?
You can include views in Laravel blade template.
here you can read more.
Just use like this:
<div>
#include('_chart-widget')
</div>
If you need to pass data to your widget component, just give parameters as an array to your component:
#include('view.name', ['status' => 'complete'])
If you want variables to be different in each page simply pass vairables from Controller.If you are on the same page and including same blade multiple times this can help you:
#include('view.name', ['code' => 'complete'])
This will set different values for $code variable in different sections.
Check out documentation here.
I want to put together a menu and get an icon dynamically.
the result i want is something like this:
Database table:
code:
<div class="card card-dashboard-eight">
<label class="main-content-label mb-1">Categoryes</label>
<span class="d-block mg-b-20 text-muted">Stores All</span>
<div class="list-group">
#foreach($tipos as $tipo)
<div class="list-group-item">
{{$tipo->icono }}
<p>{{$tipo->tipo }}</p>
</div>
#endforeach
</div>
</div>
but it prints the label not the icon
What should I do to show my icons?
I dont know the template engine you are using, but it seems to escape the html chars. maybe it just works by removing the double brackets ("{$tipo->icono}" instead of "{{$tipo->icono }}").
But for app-design reasons: i'd suggest to store just the type (or class) in the database. not an html-tag. meaning: the column "icono" should just contain f.e. "glass-martini" - instead of a complete html-tag. put the html-stuff in your template and add the class from your column there.
You have to use {!! !!} to display the raw unescaped HTML of your icon:
{!! $tipo->icono !!}
This way it doesn't get escaped with htmlspecialchars.
Basically {{ $var }} will be compiled to <?php htmlspecialchars($var); ?> and {!! $var !!} to <?php echo $var; ?>.
See the docs on how to display data in Blade templates for more information.
I am using Laravel 5.4 and I have a blade file that has many Form sections. One of my Form sections pulls items from my database and displays the items as checkboxes for my form. I am trying to manipulate the code so that my checkbox items appear in two columns, side-by-side rather than just being displayed in a line one after the other.
Here is the code I am trying to manipulate:
<div id="field-container" class="form-group {{ $errors->has('categories') ? 'has-error' : '' }}">
{!! Form::label('categories', 'Categories', ['class' => 'req col-sm-offset-1 col-sm-3 control-label']) !!}
<div class="checkbox-group">
#foreach ($dep_dep_cats['categories'] as $oneCategory)
{!! Form::checkbox('categories[]', $oneCategory['id'] ) !!}
{!! $oneCategory['title'] !!}
#endforeach
</div>
{!! $errors->has('categories') ? '<p class="col-sm-8 col-sm-offset-4 text-danger"><strong>' . $errors->first('categories') . '</strong></p>' : '' !!}
</div>
I was thinking that I could throw in an if statement to align items a certain way based on their id but I was not able to get this to work. I also put a <div> around the Blade syntax form items inside of my #foreach loop and this lined my items up into one big column, which is closer to what I am going for. How can I split that large column into two smaller columns?
I am trying to pass data from my master blade to the partial depending where it is open. This is part of my master blade:
<div id="main-section">
#section('topBar')
#include('customer.layouts.partials.top-bar')
#show
#section('header')
#include('customer.layouts.partials.header')
#show
#section('carousel')
#include('customer.layouts.partials.carousel', ['function' => 'drawer'])
#show
</div>
<div id="drawer">
<div id="item-detail">
</div>
<div id="item-detail-carousel">
#section('carousel')
#include('customer.layouts.partials.carousel', ['function' => 'itemDetail'])
#show
</div>
</div>
So, as you can see I am using customer.layouts.partials.carousel in two places.
I am receiving data in my partial like this:
<div class="swiper-container {{ $function }}">
<div class="swiper-wrapper">
#foreach($issues as $issue)
<div class="swiper-slide">
<img
src="/imagecache/large/{{ $issue->first()->image }}"
onclick="{{ $function }}(
'{{ $issue->first()->magazine->id }}',
'{{ $issue->first()->magazine->name }}',
'{{ $issue->first()->magazine->summary ?: '' }}',
'{{ $issue->first()->magazine->image ?: '' }}',
'{{ $issue->first()->image }}'
)"
>
</div>
#endforeach
</div>
</div>
Div #drawer is hidden first, has display:none, and it is shown on click on an image in the slider in the main-section. And then #drawer gets slides over the main-section. But when I inspect the elements in the chrome I see that both in the main-section and in the drawer section, data that was passed is drawer. The #drawer didn't get data ['function' => 'drawer'] as I thought it would.
How can I achieve that?
I think you're misunderstanding how the #section directive is meant to work.
For starters, you shouldn't have multiple #section directives with the same name (think of it like ids with HTML - there should only be one with that name). Also, if your not going to be extending files with #extend there isn't much point in using #section at all.
If you remove the #section directives from around your #include('...') then they should work fine.
Hope this helps!
I'm using Laravel 4.2 and I have multiple bootstrap boxes on my page. I would like to put those in a file that I can include along with some arguments that will be inserted into it, so I don't need to repeat the same code for the boxes every time.
Controller
$box_options = array(
'icon_classes' => 'fa fa-pie-chart',
'box_title' => 'Browser Usage',
'box_footer_text' => 'View More Visitor Data'
);
return View::make('index')->with('box_options', $box_options);
box.blade.php (Include File)
<div class="box box-default">
<div class="box-header with-border">
<h3 class="box-title">
<i class="{{ $icon_classes }}"></i> {{ $box_title }}
</h3>
</div><!-- /.box-header -->
<div class="box-body">
<div class="row">
#include('includes.global.pie_chart', $browser_usage_pie_chart_options)
</div><!-- /.row -->
</div><!-- /.box-body -->
#if($box_footer_text)
<div class="box-footer text-center">
{{ $box_footer_text }}
</div><!-- /.box-footer -->
#endif
</div><!-- /.box -->
I need the contents of the box body to be dynamic, so it could be either a string or another include file or a combination of both of them. If it was only a string, I could easily pass it in with the controller as I have with the other variables, but I need it to be able to place an include file there as well, except that it could a number of different include files.
How can I pass an include as an argument for another include? Or is there another completely different way to do this that I haven't considered?
An #include() is like a php include ""; so the scope of the variables don't change, you can acces the variables of box.blade.php in includes.global.pie_chart without passing it.
because the scope doesn't change, you can do:
return view('box', "browser_usage_pie_chart_options" => $browser_usage_pie_chart_options):
in your view box.blade.php have:
#include('includes.global.pie_chart')
and in includes.global.pie_chart call it like:
{{ $browser_usage_pie_chart_options }}
i recomend if your includes are used many times and sometimes the variable doesn't exist, before print it check if exist #if(isset($browser_usage_pie_chart_options))
like this
#include("part.dateTemplate",["name" => "pr_date"])
// dateTemplate
{{ $name }} // pr_date