Twig variables in slim frame work - php

I want to pass data from my controller to the twig view.
this is my controller code:
class HomeController extends Controller
{
public function index($request, $response)
{
return $this->view->render($response, 'home.twig');
}
public function numbers($request, $response){
return $this->view->render($response, 'home.twig', ['test' => '1221']);
}
}
This is my home.twig code:
{% block content %}
<div id="wrapper">
<div class="content-wrapper container">
<div class="row">
<div class="col-sm-12">
<div class="page-title">
<h1>Numbers for {{ test }} <small></small></h1>
<ol class="breadcrumb">
<li><i class="fa fa-home"></i></li>
<li class="active">Data</li>
</ol>
</div>
</div>
</div><!-- end .page title-->
<div class="row">
<div class="col-sm-12">
{{ abs }}
</div>
</div>
</div>
</div>
{% endblock %}
The value 1221 that is set under test is not shown on the page when rendered.
What am I missing?

Related

return user to a view with success message after registration laravel 9.x

I am trying to return a view with a success message with a link to the login page after registering a user, however the session message isn't passed along with the view.
Here is my registred() method
protected function registered(Request $request, $user)
{
return view('frontend.pages.register-success')->with('message', 'Your registration was successful. Click the button below to login to your account');
}
Here is the view page
#extends('frontend.layouts.main')
#section('title', 'Registration successful')
#section('content')
<!-- Main Content -->
<div id="main-content" class="site-main clearfix">
#if (Session::has('message'))
<section class="popup">
<div class="popup-inner">
<div class="popup-content">
<div class="img">
<img src="assets/img/popup/popup.jpg" alt="Image">
</div>
<div class="content text-center">
<p class="">{{ Session::get('message') }}</p>
<h6 class="heading">REGISTRATION SUCCESSFUL!</h6>
LOGIN TO YOUR ACCOUNT
</div>
</div>
</div>
</section>
#endif
</div><!-- /#main-content -->
#endsection
We can use session() global helper instead of Session:
// flash message create
session()->flash('message', 'This is a message!');
session()->flash('alert-class', 'alert-danger');
get flash message by key from your blade file
#if(session()->has('message'))
<section class="popup">
<div class="popup-inner">
<div class="popup-content">
<div class="img">
<img src="assets/img/popup/popup.jpg" alt="Image">
</div>
<div class="content text-center">
<p class="">{{ session('message') }}</p>
<h6 class="heading">REGISTRATION SUCCESSFUL!</h6>
LOGIN TO YOUR ACCOUNT
</div>
</div>
</div>
</section>
#endif
If you are using "laravel/ui": "^4.2", add this method in your controller "RegisterController":
// ADD CUSTOM FLASH MESSAGE
protected function registered(Request $request, $user)
{
$request->session()->flash('flash-alert', "¡Bienvenido a Winter-APP, <b>$user->name!</b> 🎉");
}
Now, add this code in your file "register.blade.php":
<div class="container">
#if(session()->has('flash-alert'))
#php
$alert = session()->get('flash-alert');
#endphp
{{--USANDO EL COMPONENTE ALERTA--}}
<x-alerta type="success" :message="$alert"></x-alerta>
#endif
</div>
And the message will show 👇:

how to add pagination to search product page in laravel

I want to add pagination to my searching product item view using my show controller
This is my controller
public function show($id)
{
$categories = Item::all();
$products = Item::find($id)->products->paginate(3);
return view('category.index', compact('categories', 'products'));
}
This is my view
#extends('layout.front')
#section('page')
<div class="row" id="portfolio">
<!-- Page Content -->
<div class="container">
<div class="row">
<div class="col-lg-3" style="margin-top:20px;">
<nav class="main-nav">
<ul class="main-nav-ul">
<li style="background-color: #343A40; color: #ffffff; font-weight: 600;" id="sidebar-header"><a>Product List</a></li>
<li>Computer<span></span>
<ul>
#if(!empty($categories))
#forelse($categories as $category)
<li>{{ $category->name }}</li>
#empty
<li>No data found</li>
#endforelse
#endif
</ul>
</li>
<li>CCTV</li>
<li>Gaming</li>
</ul>
</nav>
</div>
<!-- /.col-lg-3 -->
<div class="col-lg-9">
<div class="row" style="margin-top:20px;">
#if(!empty($products))
#foreach($products as $key=>$product)
<div class="col-md-4 col-sm-6 portfolio-item" id="items">
<div class="card h-100">
<img class="card-img-top" src="/storage/{{ $product->image }}" alt="Product Image">
<div class="card-body">
<h4 class="card-title">
{{ $product->category_name }}<br />{{ $product->item_name}}
</h4>
<p class="card-text" style="color: #A9A9A9;text-decoration: line-through;">LKR {{ $product->old_price}}</p>
<h4 style="text-align: center; color: #fff;"><a class="waves-effect waves-light btn btn-dark btn-block">LKR {{ $product->new_price}}</a></h4>
{{--#endforelse--}}
</div>
</div>
</div>
#endforeach
#endif
</div>
<!-- /.row -->
</div>
<!-- /.col-lg-9 -->
</div>
<!-- /.row -->
</div>
<!-- /.container -->
</div>
<div class="col-lg-12">{!! $products->links() !!}</div>
#endsection
i used laravel default pagination and customize my bootstrap pagination to laravel.
But in my show function in controller pagination not working. how to fix searching products pagination.
i used links method. but it's not working for this view.
<div class="col-lg-12">{!! $comproducts->links() !!}</div>
You are not using pagination anywhere in the code. Please check the documentation on how to add pagination. You need to use paginate function when making the db call, and then use paginator links.
https://laravel.com/docs/5.5/pagination
{{$products->render()}}
put it below the end of the foreach loop.
Hi Please find pagination code here
Table:stocks PK:id,Varchar:name
StockController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
use App\Http\Controllers\Controller as Controller;
class StockController extends Controller {
public function usaPage() {
$stocks = DB::table('stocks')->orderBy('name', 'ASC')->paginate(50);
return view('stock.usa', ['stocks' => $stocks]);
}
?>
usa.blade.php
#section('content')
<div class="row">
<div class="col-md-12">
<table class="table">
<tr>
<th scope="col">Company Name</th>
<th scope="col">ID</th>
</tr>
#foreach ($stocks as $stock)
<tr><td> {{ $stock->name }}</a></td><td> {{ $stock->id }}</td></tr>
#endforeach
</table>
</div>
</div>
#endsection

Is my code wrong, or there is a bug in opencart

I've created extremely simple module for Opencart, just displaying hello world in the admin panel. Here is my controller:
<?php
class ControllerExtensionModuleHelloworld extends Controller {
public function index(){
$this->load->language('/extension/module/helloworld');
$this->document->setTitle('Hello World');
$data['heading_title'] = $this->language->get('heading_title');
$data['helloworld'] = $this->language->get('helloworld');
$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');
$this->response->setOutput($this->load->view('extension/module/helloworld', $data));
}
}
Here is my view:
{{header}}
<div class="container">
<div class="row>
<div class="col">
{{column_left}}
</div>
<div class="col">
<h1>{{heading_title}}</h1>
<p>{{helloworld}}</p>
</div>
</div>
</div>
{{footer}}
The problem is that the h1 and the p tags are displayed under the the column-left like this:
I changed the col classes with col-sm-4 and col-sm-8 like this:
{{header}}
<div class="container">
<div class="row>
<div class="col-sm-3">
{{column_left}}
</div>
<div class="col-sm-8">
<h1>{{heading_title}}</h1>
<p>{{helloworld}}</p>
</div>
</div>
</div>
{{footer}}
But this didn't work, finally I added col-sm-offset-2 class to the col-sm-8 div, and it shifted the text left, like it's suppose to be.
Any ideas where the problem may be?
{{ header }} {{column_left}}
<div id="content">
<div class="page-header">
<div class="container-fluid">
<h1>{{ heading_title }}</h1>
</div>
</div>
<div class="container-fluid">
<!-- content here -->
</div>
</div>
{{ footer }}
Try this, it should work.

How do I call a component inside a component [OctoberCMS]

I want to call a component inside a component with a variable, like this:
Here's the code of the default.html->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<div class="container">
<div class="row">
{% partial __SELF__ ~ "::category" category=__SELF__.category childscategory=__SELF__.childscategory%}
<div class="col-xs-3">
<strong>DATA</strong>
<ul class="list-group text-center">
{% partial __SELF__ ~ "::dates" files=__SELF__.files %}
</ul>
</div>
<div class="col-xs-3">
<strong>Nome do Ficheiro</strong>
<ul class="list-group text-center">
{% partial __SELF__ ~ "::files" files=__SELF__.files %}
</ul>
</div>
<div class="col-xs-3">
<strong>Descrição</strong>
<ul class="list-group text-center">
{% partial __SELF__ ~ "::description" files=__SELF__.files %}
</ul>
</div>
<div class="col-xs-1">
<strong>{{__SELF__.labelpresentation}}</strong>
<ul class="list-group text-center">
{% partial __SELF__ ~ "::download_1" files=__SELF__.files %}
</ul>
</div>
-> I WANT TO CALL THE COMPONENT HERE <-
</div>
</div>
If you want me to post more code like the .php, it's ok
Example: use the fileUploader component in my ApplicationForm component. In ApplicationForm class, add this:
public function init()
{
$component = $this->addComponent(
'Responsiv\Uploader\Components\FileUploader',
'fileUploader',
[
'deferredBinding' => true,
'maxSize' => $this->property('maxFileSize'),
'fileTypes' => $this->property('fileTypes'),
'placeholderText' => $this->property('placeholderText'),
]
);
$component->bindModel('cv', new Application());
}
And in the view (default.htm) of the ApplicationForm component use the initialized component like so:
{% component 'fileUploader' %}

Relational variables are null while trying to print them in twig file on an AJAX call

I'm working on a wordpress plugin using herbert. I have two controllers, the one (ClientController) which is called via panel and the second one(TimelineController) is an API controller for saving new data on the time line and is called via AJAX request using routes.
Here are my two controllers
ClientController
public function edit(Http $request)
{
$client = $this->clientsRep->find($input["id"]);
$timeline = $client->timeline;
return view('#AdminViews/edit.twig',compact('client', 'timeline'));
}
TimelineController
public function store(Http $request)
{
if($request->ajax())
{
if (is_user_logged_in())
{
$class = ucfirst($request->get('type'));
$c = 'Testing\\HelpedClasses\\TimelineLib\\'.$class;
$activity = new $c();
$activity = $activity->saveActivity($request);
if (is_array($activity))
{
$type = $activity["type"];
$activity = Activities::find($activity["activity_id"]);
$returnHTML = herbert('twig')->render('#AdminViews/edit/timeline_partials/timeline-row.twig', [
'activity' => $activity,
]);
return new JsonResponse(['success' => true, 'html' => $returnHTML, 'type' => $request->get('type'));
}
}
}
}
Here are the Models with their relations
class Client extends Model
{
public function timeline()
{
return $this->hasOne('Test\Models\Admin\Timeline', 'timeline_client_id', 'ID');
}
}
class Activities extends Model
{
public function clientNote()
{
return $this->hasOne('Test\Models\Admin\Notes', 'notes_timeline_activities_id', 'ID');
}
}
Here are the views
edit.twing
<div class="wrap">
<h1>Edit: {{client.clientMeta.name.value }} {{client.clientMeta.name.lastname }}</h1>
<div class="postbox ">
<ul class="nav nav-pills">
<li class="active">Account</li>
<li>Activity</li>
</ul>
<div class="inside">
<div class="tab-content clearfix">
<div class="tab-pane active" id="1a">
{% include '#AdminViews/edit/edit.twig' %}
</div>
<div class="tab-pane" id="timeline">
{% include '#AdminViews/edit/timeline.twig' %}
</div>
</div>
</div>
</div>
</div>
timeline.twig
<h3>Activity</h3>
<div class="timeline row">
<div class="col-sm-12 col-md-4 col-md-push-8">
Add Note <i class="fa fa-sticky-note"></i>
</div>
<div class="col-sm-12 col-md-8 col-md-pull-4 timeline-section">
{% if timeline is not null %}
<ul class="timeline">
{% for activity in timeline %}
{% include '#AdminViews/edit/timeline_partials/timeline-row.twig' %}
{% else %}
<li class="empty-list">Looks like you have no addresses added.</li>
{% endfor %}
</ul>
{% else %}
<p class="empty-list">Looks like you don't have any timeline yet for this customer.</p>
{% endif %}
</div>
</div>
timeline-row.twig
<li>
<div class="direction-l"><span class="time-wrapper"><span class="time">{{activity.updated_at}}</span></span></div>
<div class="direction-r">
<div class="flag-wrapper">
<span class="flag">
{% include '#AdminViews/edit/timeline_partials/timeline-row-'~ activity.timeline_meta_key ~ '.twig' %}
</span>
</div>
</div>
</li>
and here is a sample of activity.timeline_meta_key value: timeline-row-note.twig
<span class="timeline-key"> <i class="fa fa-bell-o"></i>{{activity.timeline_meta_key}}</span>
<div class="clearfix"></div>
{% if activity.clientNote %}
<p class="note-subject">{{activity.clientNote.notes_subject}}</p>
{% endif %}
{% if activity.clientNote %}
<p class="note-desc">{{activity.clientNote.notes_description}}</p>
{% endif %}
Now if I call the ClientController it shows me fine the values {{activity.clientNote.notes_subject}} and {{activity.clientNote.notes_description}}
If now I call the TimelineController is not printing the variables that I already mentioned, I did a dd() in the twig file and it says that the variable is null, but if I do dd() before
$returnHTML = herbert('twig')->render('#AdminViews/edit/timeline_partials/timeline-row.twig', [
'activity' => $activity,
]);
and after Activities::find($activity["activity_id"]); the two variables have the correct value. Does anyone knows why is not showing me the values of the relation activity.clientNote in the twig files if I'm do an ajax call?
Thank you

Categories