Php templating - Getting the html right - php

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?

Related

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

PHP is not recognizing my '<del>' tag in index.view.php and removing it from the outputted html

I am trying to make the PHP code in my index.view.php file put a strike through the task description if completed is true. I have been following a tutorial on Laracasts introducing me to classes and he uses <strike></strike>, however that didn't work and I saw on w3schools that HTML5 doesn't support that tag anymore and was advised to use <s></s> or <del></del> instead. This, however, didn't work for me.
I have tried using Firefox instead of Google Chrome, however, the output was exactly the same. The problem still persisted.
The following is my index.php file:
<?php
class Task {
public $description;
public $completed = false;
public function __construct($description)
{
$this->description = $description;
}
public function complete()
{
$this->$completed = true;
}
public function isComplete()
{
return $this->$completed;
}
}
$tasks = [
new Task("Go to the store"),
new Task("Finish my screencast"),
new Task("Finish PHP Course")
];
$tasks[0] -> complete();
require 'index.view.php';
The following is my index.view.php file:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Document</title>
</head>
<body>
<ul>
<?php foreach ($tasks as $task) : ?>
<li>
<?php if ($task->completed) : ?>
<del><?= $task->description; ?></del>
<?php else: ?>
<?= $task->description; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
The following is the HTML that gets outputted on Chrome:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Document</title>
</head>
<body>
<ul>
<li>
Go to the store
</li>
<li>
Finish my screencast
</li>
<li>
Finish PHP Course
</li>
</ul>
</body>
</html>
Your get and set methods are wrong. You have a dollar sign too much. This is what you should do.
public function complete() {
$this->completed = true;
}
public function isComplete() {
return $this->completed;
}
Also, a tip: Try to use the isComplete() getter instead of accessing the completed property if you write a getter. Then you also can make the property private.

Codeigniter best way to handle controller calling views

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>

how to make background css URL in codeigniter?

I'm trying to display some images using the background url in CSS but is not working. I put this code in one of my CSS selector
background:url(../assets/images/teksture2.png);
and view code
<!doctype html>
<html lang="en">
<head>
<meta content-type: "text/css" charset="UTF-8">
<title>Aplikasi Inventori Barang</title>
<link rel="stylesheet" href="<?php echo base_url("assets/css/layout.css");?>">
</head>
<body>
<div id="header">
<div class="image"><img src="<?php echo base_url("assets/images/logo.png");?>" alt="logo"></div>
</div>
</body>
</html>
and in controller file is
<?php
/**
*
*/
class Home extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->helper('url');
$this->load->view('index');
}
}
?>
can you help me solve this problem?
Hope css and images folders exist inside the asset folder. Then in your css file, you have to use below code.
background:url(../images/teksture2.png);

interpreting php as plain text when using php function require

I have main site to which i put code using require function, but when i add php functions into it, they appear as plain text. The php code is rather long so i won't paste it here. Could anyone help me with that?
Thanks
ok i am adding some code:
require part with only one function:
<?php
$content=<<<EOF
echo 'hello';
EOF;
require 'whatever.php';
?>
and main part:
<?php
echo <<<CONT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
...
</head>
<body id="body">
<div id="container">
$content
</div>
<div id="add_func">
</div>
</body>
</html>
CONT;
?>
Make sure that You wrap your code in <?php.....?> tags.
Update:
No need to use the heredoc syntax for what you are doing, you could modify you code like this:
<?php
echo 'hello';
require 'whatever.php';
?>
And:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
...
</head>
<body id="body">
<div id="container">
<?php echo $content; ?>
</div>
<div id="add_func">
</div>
</body>
</html>
just get rid of heredoc
require part
<?php
echo 'hello';
require 'whatever.php';
?>
main part
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
...
</head>
<body id="body">
<div id="container">
<?php echo $content ?>
</div>
<div id="add_func">
</div>
</body>
</html>
that's all
PHP is not Perl, heredoc is useless here, even if properly used.
Just never use this ugly syntax.

Categories