I have a listing item template as below:
<div class="listing-item">
<div class="photo">{$thumbnail}</div>
<div class="title">{$name}</div>
<div class="price">{$price} {$currency}</div>
<div class="location">{$city}/{$town}</div>
</div>
I'm including this template from another template file, and assign it into a variable named listing_item, as shown below:
<div class="listing-box-container clearfix">
{include file="common/product/listing_item.tpl" assign=listing_item}
</div>
Now i have var a variable named listing_item that holds template for single listing item.
I want to assign value to variable inside listing_item like:
{assign var="$listing_item.thumbnail" value="Sometown"}
I'm not passign values while i'm including the template because i want to use listing_item multiple times so i dont want to load it everytime i need it.
The idea here is include the template once, and assign values & echo where it needed.
So, how can i assign value to variable inside a template which has already been included and assigned to a variable?
Or, what is the best practice to archieve my needs in smarty?
Any help will be highly appreciated
If You are using smarty3 You can use this constuct:
{$listing_item = [
'thumbnail' => 'some_thumbnail',
'title' => 'some_title',
'price' => 'some_price'
]}
More about variables syntax here http://www.smarty.net/docs/en/language.syntax.variables.tpl
Now pass it to the template:
<div class="listing-box-container clearfix">
{include file="common/product/listing_item.tpl" listing_item=$listing_item}
</div>
And within the template use:
<div class="listing-item">
<div class="photo">{$listing_item.thumbnail}</div>
<div class="title">{$listing_item.name}</div>
<div class="price">{$listing_item.price} {$listing_item.currency}</div>
<div class="location">{$listing_item.city}/{$listing_item.town}</div>
</div>
Related
I have a paging setup, which can be shown as such:
<div class="page__A4">
</div>
<div class="page__A4">
</div>
<div class="page__A4">
</div>
Data is then passed from my SQL database using PHP, through a for loop, e.g:
#foreach($data as $value)
<div class="element">
{{$value}}
</div>
#endforeach
which would leave the final markup:
<div class="page__A4">
#foreach($data as $value)
<div class="element">
{{$value}}
</div>
#endforeach
</div>
<div class="page__A4">
</div>
<div class="page__A4">
</div>
My $value is dynamic in height, and when the amount of $value's exceeds the first page__A4 element in height, I want it to proceed to the next page.
The real issue relies in that I am unable to use Javascript. I need to print these pages to PDF, which is done by combining a laravel view with a SASS styling file - meaning javascript wont be loaded in my final printed product.
Is there a way to achieve this, using a combination of laravel/php/SASS?
You have to class="A_4" dynamic too if you want to use it in javascript.
foreach($values as $ value){
<div class="'example'.$value.">
</div>
}
I am a beginner of Laravel. I reading the discussions on implementing global variables. But in my case, I want to set the value of the global variable from the view that can be used in the controller.
My sample code in button from adminHome.blade.php
<!--CAMPUS BUTTON-->
#foreach($campuses as $campus)
<div class="col-md-4">
<div class="card card-user box">
<div class="image" style="background-color:#00D27F;"></div>
<div class="content bg">
<div class="author">
<br><br><br><br><br><br>
<img class="" src="{{ asset('images/icon-school.png') }}" alt="..."/><br><br><br>
<h4 class="title"><b>{{$campus->campus_name}} Campus</b></h4>
{{--SOMEWHERE HERE I'LL SET THE VALUE OF THE GLOBAL VARIABLE. THE VALUE I SHOULD SET IS {{$campus->id}}--}}
<h5>{{$campus->name}} Campus</h5>
<hr>
<h5 class="title text-center">
</h5>
<a href="{{ route('dashboard') }}">
<p>VIEW</p>
</a>
</div>
</div>
</div>
</div>
#endforeach
As my code shown above, when I click that button, it would also set the value of the global variable so that in order to proceed on the next page, all the data related on that campus will only be shown. How can I do this? Thanks
May I got your Point#
there have no way to access variable from view to controller.
you can put variable in session. so that you can use from anywhere.
session(['your_key' => 'your_value']);
I think what you're looking for is when the user presses VIEW it opens the campus details yes?
In that case you can do:
{{ route('dashboard',['campus_id' => $campus->id]) }}
You are using a named route dashboard and you can pass in vars to that route with the above code.
Source: Laravel Named Routes Docs
You could either create and submit a form that will store the value in the back-end, in a database for example and later send the variable to the next view return view( 'next_page', ['variable' => $campus_id] );.
Or, you could just get the value from the form with some JS or jQuery magic and use GET variables to send it to the next page. http://example.com/next-page?variable=campus_id and echo it with {{ $_GET['variable'] }}
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>
In
#dektrium/user/views/admin/_account.php
#dektrium/user/views/admin/_info.php
#dektrium/user/views/admin/_profile.php
there are
<?php $this->beginContent('#dektrium/user/views/admin/update.php', ['user' => $user]) ?>
'the rest codes'
<?php $this->endContent() ?>
and in #dektrium/user/views/admin/update.php there're
<div class="col-md-9">
<div class="panel panel-default">
<div class="panel-body">
<?= $content ?>
</div>
</div>
</div>
the $content will be replaced by code between 'beginContent' and 'endContent', how to implement this kind of layout in my new backend model 'Rayon'? I tried write a similar CRUD code, but keep getting an error 'Undefined variable content'.
Thank you for your help.
the row with code
<?php $this->beginContent('#dektrium/user/views/admin/update.php', ['user' => $user]) ?>
tells to the "view" which is, to take the code from the reference and add it to the place where the call is invoked ..
It performs an equivalent action to "include" within more the possibility of pass the variables to use in the "content"
then you should create the part of view that you want to reuse .. and the views in which you want to call to add this type of call
You can do something similar directly reusing the render () function inside de view and indicating which (other) view and which variables to use.
for (simple) example
view container_view.php in yourapp/views
<div>my container test</div>
<?= $content ?>
then
You want that the code in container_view is placed in a way that the code inside beginContent and endContent of the
create.php in yourapp/views/ is place in the same place you have $content in the container
<?php $this->beginContent('yourapp/views/container_view.php', ['model' => $model]) ?>
<div>this code is placed in $container</div>
<div>and the value of the var model is passed</div>
<br />
<?= $model->name '>
<?php $this->endContent() ?>
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