I'm new to using Plates template library for PHP. I have a few questions about how to use it.
I'm looking through the docs I don't see a way to set a global layout. Is there not a way to do that?
I'm using it inside of Codeigniter. Ideally, I'd like to set the layout in the MY_Controller file for most of the site and change when needed in a controller extending MY_Controller; for instance, setting the main site layout and then for the admin panel setting the admin layout in the Auth_Controller that all of the other admin controllers extend.
Changing the layout of a particular set of templates I'd have to go through and edit all of those files. This doesn't seem ideal. Or even just passing a sidebar data for a particular layout has to be done by passing the sidebar data to each template and from each template to the layout in every file. This seems very redundant. Am I missing something?
To clarify Plates is the template system/library, http://platesphp.com/
Example of what I'm talking about.
The admin layout has a sidebar of all the admin URLs. This comes from a config file that's loaded and it has it's own template/view file.
I call the template and pass the data from the controller
// I created a library file that extends the Plates library so it can be easily loaded from the CI loader class
$data['sidebar_data'] = array(
'navigation'=>'Navigation',
'assets'=>'Assets',
'config'=>'Config'
);
$data['controller_name'] => 'Users';
$this->plates->render('admin/whatever_page', $data);
Inside of the template file/current page
<?php $this->layout('layouts/admin', ['title' => $controller_name, 'sidebar_data' => $sidebar_data])
// stuff for this page
?>
Inside of the sidebar navigation template that gets loaded in the admin layout file
<ul class="menu vertical">
<?php foreach($sidebar_data as $url => $label):?>
<li>
<?= $label ?>
</li>
<?php endforeach; ?>
</ul>
Then the layout
// there's a HEAD method in here that has the $title variable
<header class="row expanded collapse">
<div class="column">
<div class="top-bar">
<div class="top-bar-left">
<ul class="menu">
<li>
<strong>Site Name</strong>
</li>
</ul>
</div>
<div class="top-bar-right">
<ul class="menu dropdown align-right" data-dropdown-menu>
<li>
<?= $current_user ?></a>
</li>
<li class="divider"> | </li>
<li>
<?= $this->insert('auth/logout_form') ?>
</li>
</ul>
</div>
</div>
</div>
</header>
<section id="content">
<div class="left-panel">
<?= $this->insert('adminnav', ['sidebar_data' => $sidebar_data])?>
</div>
<div class="main-panel">
<?= $this->section('content')?>
</div>
</section>
<div id="adminmodal" class="reveal" data-reveal>
<button class="close-button" type="button" data-close aria-label="Close modal">
<span aria-hidden="true">×</span>
</button>
<div class="modal-body"></div>
</div>
<script type="text/javascript">$(document).foundation()</script>
<?= $this->insert('_blocks/googleanalytics')?>
Having to pass the same data from every controller method to the controller's view/template then from that to the layout/other templates is very redundant.
Convert the array to json
for example:
$this->plates->render('admin/whatever_page', ["data" => json_encode($data)]);
Related
im new at Laravel, so i have following problem:
I have db with issue table, that contains 15 names of diseases with it's description. This is my model
class Issue extends Model
{
use HasFactory;
use SoftDeletes;
protected $guarded = false;
public function gender() {
return $this->belongsTo(BodyPart::class);
}
}
And here's controller:
use App\Http\Controllers\Controller;
use App\Models\Issue;
class IndexController extends Controller
{
public function __invoke()
{
$eye_issues = Issue::all();
return view('app.issues.man.eyes.index', compact('eye_issues'));
}
}
And finally below my blade file:
<main>
<section id="schedule" class="section-with-bg">
<ul class="nav nav-tabs" role="tablist" data-aos="fade-up" data-aos-delay="100">
#foreach($eye_issues as $issue)
#if($issue->body_part_id == 2 and $issue->gender_id == 1)
<li class="nav-item">
<a class="nav-link" href="#issue-{{ $issue->id }}" role="tab"
data-bs-toggle="tab">{{ $issue->issue }}</a>
</li>
#endif
#endforeach
</ul>
<div class="tab-content row justify-content-center" data-aos="fade-up" data-aos-delay="200">
#foreach($eye_issues as $issue)
#if($issue->body_part_id == 2 and $issue->gender_id == 1)
<div role="tabpanel" class="col-lg-9 tab-pane fade show active" id="issue-{{ $issue->id }}">
<div class="row schedule-item justify-content-center">
<div class="col-md-10" style=text-align:center>
<h4>{{ $issue->description }}</h4>
Test yourself
</div>
</div>
</div>
#endif
#endforeach
</div>
</div>
</section>
</main>
So problem is, after loading the page, navigation tabs should show all disease names (as it does) and the description section must be empty and show according description of disease that selected from nav-tabs and change description if disease is changed from nav-tabs. But after loading page, in the description section, there are all disease description, even nav-tabs selected to none onload. Only after selecting all diseases from nav-tabs, page works properly.
P.S I use MySQL if case u wanted to know. i tried to use all JS and CSS hide and show functions and other methods from internet, but no one works, and i guess it depends only on laravel it self. if you dont understand what i mean, comment it, i'll leave link for demo video of the issue
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 have a datatable for social links. a user can edit the link whenever he wants.
Just link any other crud it is very simple. I am trying to load all the links in blade view with icons in header of my app. I created as function and added a for each loop in the snippets.
here's my controller:
public function socials()
{
$socials = Social::all();
return view('inc.subheader')->with('socials', $socials);
}
Here's blade snippent:
<div class="col-md-3 justify-content-center social-links">
<ul class="nav d-flex justify-content-center">
#foreach ($socials as $social)
<li class="">
<i class="{{ $social->icon }} fa-2x"></i>
</li>
#endforeach
</ul>
</div>
The error I'm getting is "$socials is undefined" and when I go on localhost/inc/social I only see three dots and no icons with links.
Change this line:
return view('inc.subheader')->with('socials', $socials);
to this:
return view('inc.subheader', compact('socials'));
You can try all combination :
return view('inc.subheader', compact('socials'));
return view('inc.subheader', [ 'socials'=> $socials]);
I am using codeigniter to make a web application, When the user registers with invalid credentials I am trying to reload my registration tab which is located on my authentication view, when the authentication view reloads my log in tab is displayed first because by default this is set to the active tab.
authentication view
<div class="row">
<div class="medium-12 columns">
<ul class="tabs" data-tabs id="authentication_tab">
<li class="tabs-title is-active">Login</li>
<li class="tabs-title">Sign Up</li>
</ul>
<div class="tabs-content" data-tabs-content="authentication_tab">
<div class="tabs-panel is-active" id="panel1">
<?php $this->load->view('authentication/login');?>
</div>
<div class="tabs-panel" id="panel2">
<?php $this->load->view('authentication/reg');?>
</div>
</div>
</div>
</div>
<?php $this->load->view('templates/footer');?>
loading my authentication view after validation has taken place
public function reg()
{
$data['title'] = 'Home Page';
$this->load->view('templates/header',$data);
$this->load->view('authentication/authentication#panel2');
}
In whatever controller you are using just decide (depending on the actions taken) which tab you want opened. Let us say the tab you want is 'thisOne', then pass a variable to your view, something like $open_tab='thisOne'.
In your view, before each tab, simply check the variable to see if it equals the current tab, and then set a class.
Use this:
<?php echo ('name_of_tab' == $open_tab) ? 'is_active' : ''; ?>
In your tab LI
<li class="tabs-title <?php echo ('name_of_tab' == $open_tab) ? 'is_active' : ''; ?>">Blah Blah</li>
In your controller you can set the default tab first, then overide that depending on users posts or actions etc.
Hope that helps,
Paul.
I have a <h1> tag in my template blade, its duplicate in all pages of my website. I want to do a condition if the page is Homepage then use this <h1> else don't write it.
This is my header code
<div class="col-sm-7">
<div class="slogan">
<h1 id="h1" class="tlt">my h1 Tag</h1>
<span id="span" class="tlt">" {{$slogan[0]->text}} "</span>
</div>
</div>
Thanks
You can use Request::is('/'), given that the "homepage" is on this url. The is() method can also accept wildcards which can be useful for stuff like Request::is('admin/*');.
If you have a route name for your routes, you can do like this in your view:
Route:
Route::get('/', ['as'=>'home', 'uses'=>'HomeController#index']);
In view:
#if(request()->route()->getName() == 'home')
// <h1> Text for home page </h1>
#else
//text for other pages.
#endif