Welcome Guest, Not a member yet? Register   Sign In
View Component
#1
Wink 
(This post was last modified: 10-22-2022, 04:52 AM by SubrataJ.)

is there any way we can make a reusable component of a specific part of  HTML and use it throughout the application? 

I have heard rumors that it is possible but I have had a hard time finding an article on the same on the web.

Thank you in Advance, Keep Grinding  Cool
Learning Codeigniter 
Reply
#2

(This post was last modified: 10-22-2022, 09:35 AM by captain-sensible. Edit Reason: forgot to add some points )

if you use the approach of "layout" in your views , then you can create for want of a better word , chunks of html which can be inserted into a single main web page

So in my case my view "layout" contains the header(with links to css , bootstrap etc ) , main middle section where different content is inserted and footer(also containing jquery ) in the middle it has
Code:
<?= $this->renderSection('content') ?>


each different "chunk" of html will be in the form :

Code:
<?= $this->extend('layout') ?>

<?= $this->section('content') ?>
<!-- you can even inject content into chunks of html that will extend tyhe main lay out using line below-->

<?= $this->include('carousel') ?>

<!-- in the above i inject a chunk which uses bootstrap carousel-->


<!-- all html content here-->
<?= $this->endSection() ?>
CMS CI4 A CMS system, runs out of the box written on top of CI4
Arch Book  CodeIgniter4 on Apache(pages 92-114) 
Github  
Reply
#3

CI4 has View Cells.
See https://codeigniter4.github.io/CodeIgnit...cells.html
Reply
#4

(This post was last modified: 10-22-2022, 08:26 PM by SubrataJ.)

@captain-sensible thanks for the reply but I am already aware of the rendersection..

Thank you @kenjis for now this looks pretty promising to me after going through the documentation. i will give it a shot
Learning Codeigniter 
Reply
#5

Cells and layouts are both parts of the puzzle. If you don't need any logic with the HTML chunks, you could just store them as a separate view and include them wherever you need them.
Reply
#6

(10-24-2022, 06:52 AM)kilishan Wrote: Cells and layouts are both parts of the puzzle. If  you don't need any logic with the HTML chunks, you could just store them as a separate view and include them wherever you need them.

Thanks for the insights.
Learning Codeigniter 
Reply
#7

I think this would be really helpful to you: Components library for CI4. It allows to encapsulate html and css classes/styles into reusable website building blocks.
See it here: https://packagist.org/packages/dgvirtual...components
It allows to write this in your view:
PHP Code:
<x-green-button onclick="alert('I was clicked!')">
  <?= $clickMeLabel ?>
</x-green-button> 
and after rendering get this sent to your browser:
PHP Code:
<button
    style
="color: white; background-color: green;"
    <?= isset($onclick) ? 'onclick="' $onclick '"' '' ?>
>
    <?= $slot ?>
</button> 
This functionality has been available in Bonfire2 for a while, but I have extracted it and put it into an installable composer package.

Bonfire2 was written mainly by Lonnie Ezell (@kilishan), so most of the credit goes to him.

Hope you will find it as useful as I have.
==

Donatas G.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB