Laravel 8 Components without Blade - php

I have created a basic CMS (Content Management System) using Laravel 8. I am not using Blade. All of my views are plain PHP and HTML. At this point everything is working but I have a lot of redundant code. Is there anyway to incorporate component like logic without using Blade? Should I just use PHP include statements? Is there a proper way to do this?
For example, I would like to move my header HTML to a separate header.php file. This would include everything from the <!doctype> tag to the <body> tag.

I'm not sure if this is the proper way to achieve this, but the view() functions works from within a view file:
<?= view('layout.header') ?>
Anyone see any issues with this?

Related

Using a PHP 'template' for html pages

I'm a CS student doing a project, so its really not THAT important. I choose to use php/html as my platform. Anyway, my question is about putting the common html code into a php file, then calling functions from the file to output the html. Is this common practice? This is what I'm doing and it seems to be working OK. My goal (like everyone) is to only have to update one file and have the changes cascade through my website. I'm just curious what the industry standard way of doing this is, and if there are other good ways to do it. Here's a pseudocode example of what I got going now:
<?php
class template
{
function sideBar(){ echo <div>..sidebar stuff that is consistent on all pages..</div>
function head(){ echo css import stuff}
function scripts(){echo <script>javascript stuff</script>} //this one didnt work fyi
}
?>
and then in the html I do something like this:
<?php $template=new template; ?>
<html>
<head> <?php $template->head(); ?> </head>
<body>
<?php
$template->sidebar();
$template->scripts();
?>
</body>
</html>
I see you are learning and even tried to create your own template. I think all of good programmers at some poin was writing spaghetti code, then found out includes to include some part of page (like having the file for header, footer and even menu.
Then we started to create simple templates - like whole .html file with placeholders {{headScript}}, {{PageBody}} and str_replace them. So logic is not contained withing html. (1st mvc?)
Next progress was to create a class that we could easly add dynamic scripts, or set current menu item.
For me who started my ,,war'' with programming from structural languages evolution was like the one mentioned above.
When you already had some ways of making that - you found out that others have done quite the same as you
Zend have its views with some php code to make it dynamic or use if's and while's.
Smarty is template library - with its own code instead of php (quite php alike, but ppl tend not to be so afraid of that code).
MANY OTHERS - there is MANY other library or MVC Frameworks that found ways of splitting actual code from the html part.
And as others in comments stated - dont reinvent the wheel - i would suggest you two things
One is that you continue writing your own code (as you will learn great deal from that)
If you only need templates - use Smarty - its easy and used only as template framework.
The choice is yours, try learning, look how others are doing it and ,,invent'' your own code. It dont need to be as powerful as Smarty is (it have several years of development already - you are just starting). But this will be your code for CS.

PHP layout (template) page

I am new to PHP, lately I was working mostly with .NET MVC 4.
In MVC you can make a layout very easily, end every view knows to use it without telling each view seperatly.
I am trying to figure out what is the best equivalent in PHP.
I have not found a satisfactory answer yet. The best on was to create a 'header.php',
and for each view add a require/include.
I think this breaks DRY, because on each view i need to specify the header. What if I decide to change the file??
Does anyone has a better suggestion?
I am using codeigniter also.
Thank you!!
As you are using CodeIgniter, there is absolutely no single reason to touch require/include commands.
You can load your header views either from the controller or from the main view. If you are worried about file name changes, create a new config file, add it to autoload.php and import your filenames via $this->config->item('config_var_name')

Can I disable PHP within a Laravel (Blade) view file?

I was to understand that MVC templating was used as a means of locking down a view from using any substantial programming logic. Testing the Blade system for Laravel 4, I notice that I am still able to include PHP content into the view.
Can I disable PHP in a Blade template? Essentially turning the file into a .html file with some additional possibilities (eg, Blade looping and sections/includes).
Blade templates compile to php, so you won't be able to eliminate php altogether.
There is something you can work around your project by creating your own compiler, or extending Blade's.
You will need to work your own solution out on how to handle rendering the parts in php that are used in your template, I would just render them as is, for example:
- if someone forgot an <?php echo $example; ?> that's what the engine would render.
If you need programming logic in your views you're probably doing something wrong. Try to do the logic in your controller and inject the variables into your views.

how to isolate the style (css+html) from php

how can i isolate the style (css+html) from php, like put in the php file just some lines including by it the whole style or theme.
ex:
echo
eval_template("header") .
eval_template("body") .
eval_template("footer")
So in future i can change the whole style without touch the php files
any idea ?
there are many ways how you could do this...
Here's a tutorial on templating in plain PHP
http://www.phpro.org/tutorials/Introduction-to-PHP-templating.html
You can also take a look at the many template engines out there.
twig is one of them: http://twig.sensiolabs.org/
Personally, I enjoy to do it manually.
PHP should not return both css and html code, or even better, it shoukd not return client-side code at all but rather dependencies to specific parts. You never want to modify a file that contains a lot of different things.
To separate css+html from the php code, what I usually do is a hierarchy done with include("..."); and include_once("..."). For example : include_once("header.php") > include("menu.php") > html semantic with css classes correctly initialized according to current context.
Then you import your css / js external scripts in header.php so that you never have to modify the whole thing unless everything changes or if you have a complete feature to add to the website. Same is possible for every sections of the website.
Hope this helps, for me it is incredibly reducing debug-time since everything important has to be done only once, then at the top of it, you can seperate as you wish.
There are a lot of template engines you can use to do that, i prefer use twig, that is integrated with symfony2 framework.
Twig is wonderful because is very easy to use and very flexible, you can use inheritance to create a common layout which can be extended and overwriten in some some part using special tags. This is a guide i've find on Symfony website but is very usefull to understand the logic behind twig: http://symfony.com/doc/current/book/templating.html

How to use Zend Framework for layouts only?

I was recently brought on to help with a project that was made up of individual HTML files with the exception of a PHP contact form. So there's not even a hint of object oriented programming, MVC, or layouts (or even PHP for that matter).
The project is quite large, but I wanted to slowly integrate the Zend Framework into this project, mostly to start using layouts. There are so many redundancies that it is such a waste of time to make small updates that should have been made in one file.
In the early days of PHP, you could separate your content blocks by including them in each page (a header and footer for example). Now, using MVC frameworks like the Zend Framework, you can create layout files that include the individual page content (views) using a view helper. I really like this because it means I only have to "include" my header, or footer, in one place.
However, I'm not sure how this would work without dispatching/bootstrapping the application (i.e. using the Zend Framework MVC components as standalone components instead). What would be the best approach to switching the site over to use layouts? How would it work? Is this even a good idea?
I've recently begun a transformation of a mish-mash file structure* to make use of ZF's layout & views. The goal is to move all files containing html into the recommended file structure for layouts and views. This is how to prepare for it:
Extract all markup from each file, keep variables in it but no logic. Name this file /application/views/scripts/page/view.phtml (for example /application/views/scripts/index/login.phtml)
Create a skeleton that fits most pages as /application/layouts/scripts/layout.phtml and let it contain something like this:
<?php echo $this->doctype('XHTML1_STRICT'); ?>
<html>
<head>
<?php echo $this->headLink(); ?>
</head>
<body>
<div id="wrapper">
<?php echo $this->layout()->content; ?>
</div>
</body>
</html>
(Add Zend to your include path and register its Autoloader.) Create a file that will be included in all of your files (unless you have a single point of entry, in that case - place it there!) Create a reference to your layout, equivalent to this:
$view = new Zend_View();
$view->setScriptPath('/application/views/scripts');
$layout = new Zend_Layout();
$layout->setLayoutPath('/application/layouts/scripts');
$layout->setView($view);
Zend_Registry::getInstance()->set('layout', $layout);
Each php file you have left (with logic, not html) needs to have access to the layout, and modifying it:
$layout = Zend_Registry::getInstance()->get('layout');
$view = $layout->getView();
$view->headLink()->appendStylesheet('/css/a_css_file.css');
// most important step, done automatically when using MVC, but now you have to do it manually
$layout->content = $view->render('index/login.phtml');
echo $layout->render();
Most important next step is adding another layout:
// /application/layouts/scripts/blank.phtml
<?php echo $this->doctype('XHTML1_STRICT'); ?>
<html>
<head></head>
<body>
<?php echo $this->layout()->content; ?>
</body>
</html>
and in one of your logic-containg files
$layout = Zend_Registry::getInstance()->get('layout');
$layout->setLayout('blank');
Ideally, in a near future you can start adapting to a MVC-like structure by looking at a design pattern like Front Controller.
*One file: model, controller, html, in no logical order.
You can certainly use Zend_Layout in isolation and it's detailed in the manual:
28.2.3. Using Zend_Layout as a Standalone Component
I believe you would then need to capture you script's output via output buffering, and pass it to the layout to be echoed.
This situation is not ideal, and without moving to the full MVC I'd probably recommend using a different, basic templating system or following the advice in other comments here.
Personally, I'd recommend not using the Zend Framework if you're only planning on using it for template/layout management. There are much better solutions for templating with PHP, with Smarty being the obvious choice (formerly part of the PHP project).
Smarty does provide a reasonably easy integration with the Zend Framework, if you need to do this at a later date, and there are a few articles on it on the Zend DevZone.
I'm not sure if this is what you're looking for, but the bbc has some videos where they use parts of the zend framework without using the whole MVC. I think they are in parts 4 & 5, http://bbcwebdevelopers.blip.tv/
Zend_Layout isn't really about including a header or footer, rather it is best used to insert various bits of content into a single large template. It's pretty sophisticated and as well as common use cases such as inserting the main body of content you can insert HEAD tags such as CSS, scripts or title tags and custom placeholders such as navigation.
From your description I'd go for a simple PHP include header and footer solution. It will be easier to implement and will remove any duplication of common template elements.
If you really want to use this to learn Zend Framework you could use Zend_Layout in the standalone mode as dcaunt suggests. There is no point trying to use the MVC bootstrap system since your site doesn't appear to need it. I'd simply do something like use URL rewriting to send all *.html requests to a single PHP file which then starts Zend_Layout and loads the requested file into the $layout->content variable via something like file_get_contents()
You could then also use ZF for the form processing if you wished.
i dont think you can do this by default, i think you need to use the Zend View as well.
But i am sure you can modify the Zend_Layout to work with your existing setup. Sorry i cant be more specific on how to do this, because it depends a lot of what is currently in place.
As a starting point i recommend looking into Smarty integrations into Zend to see how you can modify it.

Categories