How assign titles for each page by smarty? - php

i have alot of pages and i want assign title for each page how can id?
i assign home page but cannot assign pages. the variable will add in header.tpl
pages(contact.php,order.php,products.php)
i'm using {$hometitle}
$smarty->assign('hometitle',$hometitle);

Not sure I understood the question:
Does this help?
In your php:
<?php
$smarty = new Smarty();
$smarty->assign('hometitle',$hometitle);
$smarty->display('index.tpl');
?>
In your templates:
<html>
<head>
<title>{$hometitle}</title>
</head>
<body>
<!--body content here-->
</body>
</html>

Related

how to include the dynamic page in the template php?

I have 3 pages template.php and page1.php and page2.php , and a page1 and page2 menu in template, I want when I click on page1 in the template menu it includes the content of page1 in template and when i click on page2 it included contents of page 2.
template.php
<html>
<head>
<title>template</title>
</head>
<body>
<h1>TEMPLATE</h1>
page1
page2
<?php include($content); ?>
<h1>TEMPLATE</h1>
</body>
</html>
page1.php
<h2>page 1</h2>
page2.php
<h2>page 2</h2>
You can use include.
So in your example, it will be:
<html>
<head>
<title>template</title>
</head>
<body>
<h1>TEMPLATE</h1>
page1
page2
<?php echo $content; ?>
<?php include('page1.php') ?>
</body>
</html>
You can also use require. The difference is that if you use require, your code will fail if the file is not found. With include, the code still executes no matter what. So is up to you here.

How to set title after html

For example you have output <head> tag and don't know title at that moment.
You will know title at the end of code.
How to set title from that place?
Simplified template looks like:
<html>
<head>
<title>We don't know title</title>
</head>
<body>
<article>
<!--at this place we include code, that output article and know title-->
</article>
</body>
</html>
Thanks.
First thing to try, the code that is calculating the title I assume occurs after you have outputted the <head> HTML. Are you able to move this code to before the <head> and store any HTML in a variable to be printed after?
Otherwise you would need to set the answer using a script that executes when the page is fully loaded. Something like
//index.php
<head>
<title></title> //blank title
</head>
<body>
<?php
//code that finds title
$title = 'I am set later';
?>
//All other page output
<script>
//This will run at the end of the body after everything else.
document.title = <?php echo $title; ?>
</script>
</body>
</html>
edit: nicely caught

Need to call header and footer also a main div to call content

I'm building a site with html and css and i want to make a index.php to call all the parts for the page. Example Call Header and footer and also when click on nav links replace the content in same page, can i do it with php or what are my options.
Example of my index.php
Get header < ? php include("header.html"); ? >
Replace Content < div class=" MainContent "> < /div>
Get Footer < ? php include(" footer.html ") ;?>
Ditn't tested and didn't put any validation in thiscode but i hope you can get the idea how you can do this. with only index.php file. lmk if any query
header.php
navigation
<html>
<head></head>
<body>
navigation
home
About us
page.php
page.php
$page_name = $_GET['page'];
<?php include( $_SERVER['DOCUMENT_ROOT'] . "/path_or_folder_offile/".$page_name.".php" ); ?>
footer.php
</body>
</html>
index.php
<?php include('header.php'); ?>
<?php include('page.php'); ?>
<?php include('footer.php'); ?>
This is my usual page layout for the index.php
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Page Title</title>
<link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<body>
<div id="page">
<?php include_once('header.php'); ?>
<?php if (isset($_GET['page']) && !empty($_GET['page'])) { include_once('/pages/' . $_GET['page'] . '.php'); } else { include_once('/pages/index.php'); } ?>
<?php include_once('footer.php'); ?>
</div>
</body>
</html>
This uses a GET variable (?page=pageName) to select the page, which I usually use with a redirect rule in Apache.
Alternatively, you can fill the div content from a database. e.g.
<div id="content">
$pageId = $this->getPageId();
$db = new mysqli_connect('','','','');
$stmt = db->prepare("SELECT pageContent FROM pages WHERE page_id='$pageId'");
$stmt->execute();
$stmt->bind_result($curPageContent);
$stmt->fetch();
$stmt->close();
echo $curPageContent;
</div>
For an ajax call, you can do this:
$.get( "/get_page/",
{ page_id: '<?php echo $_GET['page']; ?>' })
.done(function(data) {
$('#content').html(data);
});
The folder structure I used for this is setup as:
root/
css/
js/
pages/
index.php
header.php
footer.php
index.php
Obviously you can use any structure you want, but this may help (although an assets/ folder is usually good :)
Good luck.

file_put_contents in head of another file using php

So, Im creating a library for other uses, but how can I make content from a file specificly go within the <head> or <body> html tag attribute etc...
for example, this is what im trying to make.
<html>
<head>
<?php include('content/starter/library.php'); ?>
<!-- From that included file, theres a script that put content in the head.-->
</head>
<body>
<!-- From that included file, theres a script that put content in the body -->
</body>
</html>
Im just trying to find another way instead of making multiple files for specific sections and do
<html>
<head>
<?php include('content/starter/library_head.php'); ?>
</head>
<body>
<?php include('content/starter/library_body.php'); ?>
</body>
</html>
Which I don't really want to do. Im not very good with javascript so, There no hope of me trying to figure out how to do this with javascript. Thanks for the answers in the future.
If you want to use one file (as your questions suggests) then one method is to create variables or functions in your library.php file and then echo them in your template
// contents of the library.php file...
<?php
$head_content = "put your <head> content here";
$body_content = "put your <body> content here";
?>
// your HTML file...
<?php include('content/starter/library.php'); ?>
<html>
<head>
<?php echo $head_content ?>
</head>
<body>
<?php echo $body_content ?>
</body>
</html>
UPDATE
To answer the question in your comment, here's an example using a function. You can put all of your code in a function and then just echo that anywhere in your document.
<?php
// contents of library.php...
function head() {
$return = '<link href="file.css" rel="stylesheet">';
$return .= '<link href="another_file.css" rel="stylesheet">';
return $return;
}
// your HTML file...
<html>
<head>
<?php echo head(); ?>
</head>
PHP functions explained: http://www.w3schools.com/php/php_functions.asp

How can i call a sub_page.php inside a div container of my main_page.php?

I need to show the content of one of my sub_page.php inside my main_page.php
At the moment im using an iframe to do that, but i would like to show the sub_page.php inside a div, so it will have a more professional look.
How can i do that?
<HTML>
<HEAD>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</HEAD>
<BODY>
<div style="text-align:center;"><img src="faq-banner.png" /></div><br />
<div>
<?php include("faq.php"); ?>
</div>
</BODY>
</HTML>
Inside the div, include the php page with simple include() command.
<div>
<?php include("subpage.php"); ?>
</div>
Try the include() statement which will include and evaluate the specified file:
For example:
vars.php
<?php
$color = 'green';
$fruit = 'apple';
?>
test.php
<?php
echo "A $color $fruit"; // A
include 'vars.php';
echo "A $color $fruit"; // A green apple
?>
I think all you're trying to do is:
include('sub_page.php');
Include this code within the div you're trying to display the subpage in.

Categories