Codeigniter best way to handle controller calling views - php

i just want to know if this is a good way in adding fragmented views.
lets say "header.php" is like this
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Admin Theme v3</title>
some links..
</head>
</html>
then "body.php" is like this
<!DOCTYPE html>
<html>
<body>
lots of stuff..
</body>
</html>
lastly "scripts.php"
<!DOCTYPE html>
<html>
<body>
some scripts..
</body>
</html>
then in my "MyController.php"
$this->load->view('header');
$this->load->view('body');
$this->load->view('scripts');

The best way I find is create a default view.
views > default_view.php
views > includes > header_view.php
views > includes > footer_view.php
views > information > contact_view.php
On that view
<?php
$this->load->view('includes/header_view');
$this->load->view($content_page);
$this->load->view('includes/footer_view');
?>
Then on the controller to load view this way you do not have to load the header and footer views all the time.
Following the Codeigniter StyleGuide
Filename: Example.php
<?php
class Example extends CI_Controller {
public function index() {
// Add any other variables
$data['content_page'] = 'information/contact_view'; // This will be your content page example
$this->load->view('default_view', $data);
}
}

There are too many redundant tag like <html>,<body> try separate it
header.php
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Admin Theme v3</title>
some links..
</head>
<body>
body.php
lots of stuff..
scripts.php or footer.php
some scripts..
</body>
</html>

It is not good way. Except in some cases (using frames for example), document should have just one declaration and one pair of open/closed html tag.
It could be something like:
header.php
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap Admin Theme v3</title>
some links..
</head>
<body>
some_page.php
<div class="container">
<div class="row">
//some stuff and forms here
</div>
</div>
footer.php
<div class="footer">
//©2016
</div>
</body>
</html>

Related

Remove script tag from url YII 1.17

I have a yii 1.17 application. Problem is any one can inject scripts in url. Ex: If user puts this after page url ?redir=%3C/SCript%3E%3CsvG/onLoad=prompt(7)%3E application gives popup with given message.
How can i avoid this?
Main Layout:
<html lang="en">
<head>
</head>
<body>
<?php ?>
<app-header></app-header>
<md-content id="wrapper" layout-fill layout-margin layout-padding>
<section class="grid_outer">
<?php echo CHtml::encode($content); ?>
<div class="md-grid-container md-grid-padding">
</div>
</section>
<app-footer></app-footer>
</md-content>
</body>
</html>
View File:
<app-login></app-login>

Add html inside any tag without javascript

Guys, I have a problem. Is there a way to add html inside a tag without using javascript using only php anyway ?. Thank you very much for your help in advance.
For example, there is this code:
<?php
// This part is required here, because she comes another function.
// It's generate from php server, I need to show inside tag body, for example.
$code = "<h1 style='display:none' id='title'>My String</h1>";
echo $code;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div"></div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js'
integrity='sha512-b6lGn9+1aD2DgwZXuSY4BhhdrDURVzu7f/PASu4H1i5+CRpEalOOz/HNhgmxZTK9lObM1Q7ZG9jONPYz8klIMg=='
crossorigin='anonymous'></script>
<script>
$('#my-div').html($('#titulo').html());
</script>
</body>
</html>
The output is this in the source code:
In the browser, the output is this My String:
But, this manipulation is the gift, which uses javascript for this. I don't want it that way. This will not do, because the code will be shown at the top, before the <! DOCTYPE html> tag. Is it possible, on the server, to insert <h1 style ='display:none' id='title'> My String </h1> inside the boby tag, for example?
How I would like it to look:
Example 2
For example, I have this file with code:
file2.php
<?php
include "file2.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div">
I want to show "<h1>My String</h1>" here.
</div>
</body>
</html>
Yes you can do it as simple as this:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<div id="my-div">
<?php
$code = "<h1 style='display:none' id='title'>My String</h1>";
echo $code;
?>
</div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js'
integrity='sha512-b6lGn9+1aD2DgwZXuSY4BhhdrDURVzu7f/PASu4H1i5+CRpEalOOz/HNhgmxZTK9lObM1Q7ZG9jONPYz8klIMg=='
crossorigin='anonymous'></script>
</body>
</html>
PHP is a server side scripting language and will only parse PHP code on the server side.
Yes, you can place HTML code inside PHP variables like you have done, but that will get rendered into the client (your browser).
What you can do is place $code variable inside the target div, like this:
<div id="my-div"><?php echo $code; ?></div>
Give it a try

How to display one view in another view page

i have this below code where i want to display only "Welcome to my 3rd Blog!" of "blogvieww.php" in
"blogview.php" using codegniter. But the below code what i tried with is that, even "Welcome to my 2nd Blog!" of "blogvieww.php" is getting displayed in "blogview.php".
actually i just want to display only "Welcome to my 3rd Blog!", how to do this can any one tel me please im not getting where im going wrong.
Blogcontroller.php(controller file)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Blogcontroller extends CI_Controller {
public function index()
{
$data['blogvieww'] = $this->load->view('blogvieww', '', TRUE);
$this->load->view('blogview', $data);
}
public function blogvieww()
{
$this->load->view('blogvieww');
}
}
?>
blogview.php (view file)
<html>
<head>
<title>My Blog</title>
</head>
<body>
<div>
<div><?php echo $blogvieww; ?></div>
<h1>Welcome to my 1st Blog!</h1>
</div>
</body>
</html>
blogvieww.php (view file)
<html>
<head>
<title>My Blog</title>
</head>
<body>
<div>
<h1>Welcome to my 2nd Blog!</h1>
</div>
<div>
<h1>Welcome to my 3rd Blog!</h1>
</div>
</body>
</html>
Your can do simply using $this->load-view('view_name'), like following :
view1.php
<p>View 1</p>
view2.php
<html>
<head>
<title>My Blog</title>
</head>
<body>
<div>
// Load View 1
<?php $this->load->view('view1'); ?>
<h1>Welcome to my 1st Blog!</h1>
</div>
</body>
</html>
I am not sure what you want, but you can anytime to var_dump the value of $data['blogvieww'], and if you want make a view section, don't use double tag

Phalcon - add css assets collection

I have a problem to add css assets to the collection called from view layout.phtml
This is my code:
<?php echo $this->tag->getDoctype() ?>
<html>
<head>
<?php $this->assets->outputCss('header') ?>
</head>
<body>
<?php $this->assets->get('header')->addCss('test.css'); ?>
<?php $this->assets->get('footer')->addJs('test.js'); ?>
<?php $this->assets->outputJs('footer') ?>
</body>
</html>
And this is output in browser:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript" src="/test.js"></script>
</body>
</html>
Why is not the output of CSS tag in the head?
The Assets component basically works like this:
You add your assets to the service in your controller action (because that's where the application logic is supposed to reside)
In your view, you can then access the Assets component and request the output of single assets or an entire asset group
Suppose the view you mentioned is displayed via this controller:
use Phalcon\Mvc\Controller;
class IndexController extends Controller
{
public function index()
{
// Add your header assets here
$this->assets
->collection('header')
->addCss('test.css');
// Add your footer assets here
$this->assets
->collection('footer')
->addJs('test.js');
}
}
Now your view should look like this:
<?php echo $this->tag->getDoctype() ?>
<html>
<head>
<?php $this->assets->outputCss('header') ?>
</head>
<body>
<?php $this->assets->outputJs('footer') ?>
</body>
</html>

Php templating - Getting the html right

I'm developing a web app and I'm having a problem with the templating fase. The problem concerns the generation o html code in a php object. The code is as follows:
<?php class abcHtml {
function prepare() { ?>
<!DOCTYPE html>
<html>
<head>
<?php echo $this->meta; ?>
</head>
<body>
<div id="bg">
<div id="container_wrapper">
<?php echo $this->cont; ?>
</div>
</div>
</body>
</html>
<?php }} ?>
The goal is to have a php file with the cleanest html code possible(Don't want to use any framework).
<?php
class abc extends abcHtml
{
protected $meta;
protected $cont;
public function render(){
return parent::prepare();
}
public function setMeta($meta){
$this->meta = $meta;
return $this;
}
?>
This class inherites abcHtml to make possible to add diferent html modules. The problem is that when calling render(), the returned html code is note in the right order, the html code in the $meta and $cont variables appear before the html that is inside the prepare function.
For example:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css" href="public/css/base_style.css"/>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="bg">
<div id="container_wrapper">
</div>
</div>
</body>
</html>
This problem obviously means that php is running the php code first and then attaching the html. Is there a solution to fix this?

Categories