I'm having trouble with my layout and viewModel
My design requires that the title set in my viewModel, eg 'blog categories' to be pushed to the layout.
I'm using $this->headTitle() to set the title of the page but i can't get it anywhere else.
because of a lot of changes in the layout per page, i have been forced to use partial in the layout.
single.phtml
<?php echo $this->partial('lendstudy/partial/basic/top'); ?>
<div class="container_12 clearfix">
<div id="content_wrapper">
<?= $this->partial('lendstudy/partial/basic/title'); ?>
<?php echo $this->content; ?>
<?= $this->partial('lendstudy/partial/sidebar/left'); ?>
</div>
</div>
<?php echo $this->partial('lendstudy/partial/basic/bottom');
I'm setting the header title in the view
index.phtml from category in a blog module
<?php
$title = $this->translate('Categories');
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<p>Add new category</p>
<table class="table table-bordered table-hover">
<tr>
<th>Category</th>
<th> </th>
<th> </th>
</tr>
<?php foreach($categories as $category) : ?>
<tr>
<td><?php echo $this->escapeHtml($category->getName());?></td>
<td>
[Edit]
</td>
<td>
[Delete]
</td>
</tr>
<?php endforeach; ?>
</table>
it would then need to be outputtet in the partial/basic/title.
title.phtml
<div class="grid_12 page_title">
<h1><?= $this->headTitle(); ?><span class="subtitle"><?= $this->translate('More information about the Lend Study association') ?></span></h1>
<div class="page_navigation">
<?php $partial = array('lendstudy/partial/basic/breadcrumbs', 'default') ?>
<?php $this->navigation('navigation')->breadcrumbs()->setPartial($partial) ?>
<?php echo $this->navigation('navigation')->breadcrumbs()->render() ?>
</div>
</div>
how do i do this?
Related
I am currently invoking the method “xyz” using the object manager, however the object manager should not be used in phtml file as mentioned in the Magento 2 documentation. What is the best practice to create an object of other classes in phtml file?
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
ViewModel files can be used to add codes in a template file without using an object manager.
Here Are Steps to create View Model
Step 1:- Create a layout file in Vendor/Module/view/frontend/layout/custom_index_custom.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block name="dummy" template="Sunarc_Custom::details.phtml">
<arguments>
<argument name="view_model" xsi:type="object">Sunarc\Custom\ViewModel\Customer</argument>
</arguments>
</block>
</referenceContainer>
</body>
</page>
Step 2:- Create a view model file under Vendor/Module/ViewModel/Customer.php
<?php
$viewModel = $block->getViewModel();
$collection = $viewModel->getCollection();
if ($collection->count()) {
?>
<div class="table-wrapper customer">
<table class="data table" id="my-custom-table">
<thead>
<tr>
<th scope="col" class="col id"><?php echo __('ID') ?></th>
<th scope="col" class="col name"><?php echo __('Name') ?></th>
<th scope="col" class="col email"><?php echo __('Email') ?></th>
</tr>
</thead>
<tbody>
<?php
foreach ($collection as $item) : ?>
<tr>
<td data-th="<?= $block->escapeHtml(__('ID')) ?>" class="col id">
<?php echo $item->getId() ?>
</td>
<td data-th="<?= $block->escapeHtml(__('Name')) ?>" class="col name">
<?php echo $item->getName() ?>
</td>
<td data-th="<?= $block->escapeHtml(__('Email')) ?>" class="col email">
<?php echo $item->getEmail() ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php
}
?>
Step 3:- Create your custom phtml file in Vendor/Module/view/frontend/templates/details.php
<?php
$viewModel = $block->getViewModel();
$collection = $viewModel->getCollection();
if ($collection->count()) {
?>
<div class="table-wrapper customer">
<table class="data table" id="my-custom-table">
<thead>
<tr>
<th scope="col" class="col id"><?php echo __('ID') ?></th>
<th scope="col" class="col name"><?php echo __('Name') ?></th>
<th scope="col" class="col email"><?php echo __('Email') ?></th>
</tr>
</thead>
<tbody>
<?php
foreach ($collection as $item) : ?>
<tr>
<td data-th="<?= $block->escapeHtml(__('ID')) ?>" class="col id">
<?php echo $item->getId() ?>
</td>
<td data-th="<?= $block->escapeHtml(__('Name')) ?>" class="col name">
<?php echo $item->getName() ?>
</td>
<td data-th="<?= $block->escapeHtml(__('Email')) ?>" class="col email">
<?php echo $item->getEmail() ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php
}
?>
One can also use Block Files to do the same.
good day all,
since i'm new in Code Igniter 4 (i was a native PHP self-learner) and i'm new in MVC, my question is how can i show multiple tables in one page ?
i have a dashboard view with 2 tables inside. one is for display students data and the other is for teachers data. there is no relation between tables, so i can't join the tables.
i have successfully show one of the table (student or teacher) but i can't show both tables in dashboard at once. i think i have to pass 2 models into controller function but i just can't figure it out how to do it.
any help will be appreciated
here is parts of my codes (successfully showing data from 1 table) :
Controller
use App\Models\mStudent;
class Admin extends BaseController
{
public function index()
{
$model = new mStudent();
$data['title'] = 'Students';
$data['getStudents'] = $model->getStudents();
echo view('admin/header', $data);
echo view('admin/top_menu');
echo view('admin/side_menu');
echo view('admin/dashboard', $data);
echo view('admin/footer');
}
Model
public function getStudents()
{
return $this->findAll();
}
public function getTeachers()
{
return $this->findAll();
}
View
<!-- students table-->
<table class="table table-bordered text-sm">
<thead>
<tr>
<th style="width: 10px; text-align: center;">No</th>
<th style="text-align: center;">NAME</th>
<th style="text-align: center;">Class</th>
</tr>
</thead>
<tbody>
<?php $no = 1;
foreach ($getStudents as $students) { ?>
<tr>
<td><?= $no; ?></td>
<td><?= $students['name'] ?></td>
<td><?= $students['class'] ?></td>
</tr>
<?php $no++;
} ?>
</tbody>
</table>
<!-- teacher table-->
<table class="table table-bordered text-sm" id="defaulttable">
<thead>
<tr>
<th style="width: 10px; text-align: center;">No</th>
<th style="text-align: center;">NAME</th>
<th style="text-align: center;">ID</th>
</tr>
</thead>
<tbody>
<?php $no = 1;
foreach ($getTeachers as $teachers) { ?>
<tr>
<td><?= $no; ?></td>
<td><?= $teachers['name'] ?></td>
<td><?= $teachers['id'] ?></td>
</tr>
<?php $no++;
} ?>
</tbody>
</table>
Adding Dynamic Data to the View
In your Controller, right below $data['getStudents'] = $model->getStudents(); add:
$data['getTeachers'] = $model->getTeachers();
Addendum
Then in your model, return the respective result sets. I.e:
Model
public function getStudents()
{
return $this->db->table("students")->get()->getResultArray();
}
public function getTeachers()
{
return $this->db->table("teachers")->get()->getResultArray();
}
this is what i looking for
Controller
use App\Models\mStudent;
use App\Models\mTeacher;
class Admin extends BaseController
{
public function index()
{
$model_student = new mStudent();
$model_teacher = new mTeacher();
$data = array(
'student' => $model_student->getStudents(),
'teacher' => $model_teacher->getTeachers()
);
echo view('admin/header');
echo view('admin/top_menu');
echo view('admin/side_menu');
echo view('admin/dashboard', $data);
echo view('admin/footer');
}
View
Students :
<select name="stdid">
<?php foreach ($student as $std) : ?>
<option value="<?= $std['stdid']; ?>"><?= $std['stdname']; ?></option>
<?php endforeach; ?>
</select>
Teachers :
<select name="tcrid">
<?php foreach ($teacher as $tcr) : ?>
<option value="<?= $tcr['tcrid']; ?>"><?= $tcr['tcrname']; ?></option>
<?php endforeach; ?>
</select>
hope this will help someone out there
I have to make a simple searching in my index view.
The data to be searched in the table is not a grid view but a regular table.
How can I add a simple searching like this?
And this is the code in index view
Search
<table class="table table-striped table-bordered">
<tr>
<th class="text-center">NIP</th>
<th class="text-center">Nama Pegawai</th>
<th class="text-center">Jumlah Dokumen</th>
<th></th>
</tr>
<?php foreach($models as $data): ?>
<tr>
<td><?= $data["nip"] ?></td>
<td><?= $data["nama"] ?></td>
<td align = "center"><?= $data["jumlah"] ?></td>
<td width = "200 px" align = "center"><a class = "btn btn-success" href = "./?r=arsip-sdm/detail&id=<?= $data['nip'] ?>">Detail</td>
</tr>
<?php endforeach; ?>
</table>
And this is the code in controller
help me please
You could use a separate filter form
eg :
<div class="post-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'title') ?>
<?= $form->field($model, 'creation_date') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::submitButton('Reset', ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
perform a render partial in your view and use the normal 'searchModel' => $searchModel, in your you gridView config
you can see some sample here
http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html
http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html#filtering-data
http://www.yiiframework.com/doc-2.0/guide-output-data-widgets.html#separate-filter-form
How to display 5 columns per row in opencart?
What I have tried is Extensions -> Modules ->Featured then edit the module. I have changed the limit to 5 and decreased the width and height but after changing it displays the same. I am not able to get five columns per row. Please suggest me how to acheve it.
Thank you.
Here is my popular.tpl file in catalog/view/theme/promoglasses/template/module
<div class="tab-title">
<h2>Popular Products</h2>
<ul class="tabs tabs-popular-products tab_categorys">
<li class="active" rel="tab129-popular-products">POPULAR Products</li>
</ul>
</div>
<br />
<br />
<br />
<div class="row">
<?php foreach ($products as $product) { ?>
<div class="product-layout col-sm-5ths">
<div class="product-thumb transition">
<div class="image"><img src="<?php echo $product['thumb']; ?>" alt="<?php echo $product['name']; ?>" title="<?php echo $product['name']; ?>" class="img-responsive" /></div>
<div class="caption">
<h4><?php echo $product['name']; ?></h4>
<div class="label-cont"> As low as </div>
<div class="label-price">
<?php if ($product['price']) { ?>
<p class="price">
<?php if (!$product['special']) { ?>
<?php echo $product['price']; ?>
<?php } else { ?>
<span class="price-new"><?php echo $product['special']; ?></span> <span class="price-old"><?php echo $product['price']; ?></span>
<?php } ?>
<?php if ($product['tax']) { ?>
<?php } ?>
</p>
<?php } ?>
</div>
<div class="label-sup">
<b>
<sup>(C)</sup>
</b>
</div>
<br />
<p><?php echo $product['description']; ?></p>
<?php if ($product['rating']) { ?>
<div class="rating">
<?php for ($i = 1; $i <= 5; $i++) { ?>
<?php if ($product['rating'] < $i) { ?>
<span class="fa fa-stack"><i class="fa fa-star-o fa-stack-2x"></i></span>
<?php } else { ?>
<span class="fa fa-stack"><i class="fa fa-star fa-stack-2x"></i><i class="fa fa-star-o fa-stack-2x"></i></span>
<?php } ?>
<?php } ?>
</div>
<?php } ?>
</div>
</div>
</div>
<?php } ?>
</div>
For the CSS part here I have written the code for class col-sm-5ths:
.col-sm-5ths {
float: left;
width: 20%;
min-height: 1px;
position: relative;
}
How can I add a pagination to my isotope portfolio page?
I want each filter work with a max item of 8 and use pagination to check next 8 items.
Below is my custom Template Name: Portfolio
<?php $t =& peTheme(); ?>
<?php $title = get_the_title(); ?>
<?php $pcontent = get_the_content(); ?>
<?php $pcontent = apply_filters( 'the_content', $pcontent ); ?>
<?php $project =& $t->project; ?>
<?php list($portfolio) = $t->template->data(); ?>
<?php $content =& $t->content; ?>
<div class="container">
<div class="row">
<div class="span12">
<h1><?php echo $title; ?></h1>
</div>
</div>
<div class="row">
<div class="span12">
<nav class="portfolio-filter clearfix">
<ul>
<?php $project->filter('',"keyword"); ?>
</ul>
</nav>
</div>
<hr class="main-hr-sm">
<?php echo $pcontent; ?>
</div>
<div class="row portfolio-container">
<?php while ($content->looping()):
$meta =& $content->meta(); ?>
<div class="span3 portfolio-item <?php $project->filterClasses(); ?>">
<a href="<?php echo $t->image->resizedImgUrl($content->get_origImage(),1000,1000); ?>" class="fancybox" data-portfolio="<?php $content->slug(); ?>">
<img src="<?php echo $t->image->resizedImgUrl($content->get_origImage(),460,300); ?>" alt="<?php echo esc_html(get_the_content()); ?>">
</a>
<div class="caption">
<?php $content->content(); ?>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
Pagination isn't built into Isotope so you'd have to build it from the ground up. Here is a good tutorial on how to do that with Wordpress and Isotope v1. Keep in mind the tutorial is using Isotope v1 so you'll have to change it if you want to use Isotope v2 but the general concepts in the tutorial should still be very helpful.