I have created a few php files Following are their names:
standard_head.php (which contains basic standard html code)
header.php
navbar.php
sidebar.php
footer.php
standard_footer.php (which contains closing html tags)
Page content will vary in every page and would also include html content.
I am trying to create a lot of php pages which will use all of the above pages directly.
Now I can use them directly using include statement for each of them, but I was wondering if it was possible to include all of them together with one just statement ?
<?php include('../includes/standard_head.php'); ?>
<?php include('includes/header.php'); ?>
<div id="wrapper">
<?php include('includes/nav.php'); ?>
<div id="content">
<!-- my page content -->
</div> <!-- end #content -->
<?php include('includes/sidebar.php'); ?>
</div> <!-- End #wrapper -->
<?php include('includes/footer.php'); ?>
<?php include('../includes/standard_footer.php'); ?>
You could so this (note that this is a very basic example and that I wouldn't use this myself without taking into account meta tags, page titles etc.)
template.php
<?php include('../includes/standard_head.php'); ?>
<?php include('includes/header.php'); ?>
<div id="wrapper">
<?php include('includes/nav.php'); ?>
<div id="content">
<?php echo $content; ?>
<!-- my page content -->
</div> <!-- end #content -->
<?php include('includes/sidebar.php'); ?>
</div> <!-- End #wrapper -->
<?php include('includes/footer.php'); ?>
<?php include('../includes/standard_footer.php'); ?>
Then, on a page (say index.php, for example):
index.php
<?php
$content = '<h1>Welcome</h1>';
include 'template.php';
?>
P.S: If you go down this route, make sure that you check out the Output Control Functions first.
Just create a PHP file that has a list of them i.e.
<?php include("this file");
include("the other file");
?>
And then just add that file.
Not really an answer to your question, but you could use a template engine like Twig.
http://twig.sensiolabs.org/
Try using more complex template system like Smarty or some MVC framework like Zend (this is not required but would allow you to create complex sources more easily) and then build script like this:
<?php include('../includes/standard_head.php'); ?>
<?php include('includes/header.php'); ?>
<div id="wrapper">
<?php include('includes/nav.php'); ?>
<div id="content">
<?php echo $menu->getCustomEntries(); ?>
</div>
Where $menu will be your custom object containing methods for displaying menus and submenus...
There is no straight way to include multiple files, as include / require functions except only one argument. though you can use following logic.
`#include_all_files.php
include('../includes/standard_head.php');
include('includes/header.php');
...
use above file in other files
include('includes/include_all_files.php');
`
Related
I'm trying to output content from an Advanced Custom Fields (ACF) in my wordpress theme. At the moment though, all I'm getting is the plain text content from the ACF inside double quotation marks, not inside the div and h1 tags.
The code is copied from another theme I made where it worked, which makes me think something is interfering with it somewhere?
<?php $process_title = the_sub_field('process_title'); ?>
<?php if(!empty($process_title)) : ?>
<div class="process-title">
<h1 class="process-heading">
<?php echo $process_title; ?>
</h1>
</div>
<?php endif; ?>
Thanks to #M.Eriksson, the correct code should be:
<?php $process_title = get_sub_field('process_title'); ?>
<?php if(!empty($process_title)) : ?>
<div class="process-title">
<h1 class="process-heading">
<?php echo $process_title; ?>
</h1>
</div>
<?php endif; ?>
I have a website with a template page that is something like this
<html>
<head>
<title>{{TITLE}}</title>
</head>
<body>
<div id = "header"> ... </div>
<?php include 'content.php'; ?>
<div id = "footer"> ... </div>
</body>
</html>
And then content.php would look something like this
<?php
$title = "xx";
#other php code here
?>
<p> more content </p>
My question is whether there is some way to set this up so that I am able to set the title from the file included in the middle of the page (without using javascript). I know that most people suggest including it at the top but if I were to do that the html would be at the top instead of between the header and the footer. I've wracked my brains for a while and I haven't really figured out a good way to do this (and there are a variety of possible files to be included; content.php is just an example, so I really do need some way to do this dynamically). I want to avoid putting too much code outside of the template. Any ideas?
Use output buffering:
<?php
ob_start();
include 'content.php';
$content = ob_get_clean();
?>
<html>
<head>
<title><?=htmlspecialchars($title)?></title>
</head>
<body>
<div id = "header"> ... </div>
<?=$content?>
<div id = "footer"> ... </div>
</body>
</html>
I'm not sure if you're using a templating engine or not (something like Laravel's Blade templates allow for this, I believe), but assuming you are using straight PHP, I have found the most efficient way to do this is to approach from the opposite direction and include the template file in each of my content files.
For example, I may have template.php, which has multiple functions, like this:
<?php
function createHeader($title, $keywords) {
//echo or <<<EOD your header with the set variables
}
function createFooter(...) { ... } //etc
And then, in my 'child' files, I would do:
<?php include('template.php'); ?>
<?php createHeader("My Website Front Page!", "fun, good times, joy"); ?>
<h1>My page content</h1>
<p>Content goes here</p>
<?php createFooter(...); ?>
This is a different structure from what you were attempting, though, and may not retain your intended structure.
I am trying to add an if else statement to a file in Magento.
Basically I want it to display different content depending on the body class.
For example, if the body element had a specific class, such as in:
<body class="specific-class">
Then within that body block, then the contents of it might display:
<section class="banner banner-inner <?php Mage::helper('function')->convert_category_name($category_name); ?>">
<div class="wrapper">
<?php echo $this->getChildHtml('breadcrumbs') ?>
<?php echo $this->getLayout()->getBlock('breadcrumbs')->toHtml(); ?>
</div>
<div class="bottom-rip"></div>
</section>
But if the body had a different class, or no class at all, it would default to:
<div class="no_content">
<?php echo="<h1>No content</h1>"; ?>
</div>
Does anyone know the syntax for performing such an operation?
I was referring the page.tpl.php(Drupal 7 theme) for understanding the code. I found the following code,
<?php if ($site_name || $site_slogan): ?>
<!-- !Site name and Slogan -->
<div<?php print $hgroup_attributes; ?>>
<?php if ($site_name): ?>
<h1<?php print $site_name_attributes; ?>><?php print $site_name; ?></h1>
<?php endif; ?>
<?php if ($site_slogan): ?>
<h2<?php print $site_slogan_attributes; ?>><?php print $site_slogan; ?></h2>
<?php endif; ?>
</div>
<?php endif; ?>
Can you see the code in third line, <div<?php print $hgroup_attributes; ?>> WHY the php code is inside the first div tag of html? Same thing in later part of code also, as you can see h1 and h2 code. So, what is this convention of combining the html and php in so complicated way? and how should I read that?
Combining HTML and PHP code in Drupal templates is actually a very strong feature. In this case, $hgroup_attributes will probably contain some classes that style the div. Printing it in the template results in something like
<div class="SOME_CLASSES"> ... </div>
If you're further interested in the variable $hgroup_attributes, you can inspect by pasting <?php dpm($hgroup_attributes); ?> in your template file after you've installed the Devel module.
I have a template which uses a php include file $content to display the main contents of the page. I want to have the page title change depending on the content. I can declare $pagetitle in the included $content php file but the problem is that in the layout the content is loaded after the pagetitle.
I don't want to have to set $pagetitle from my code every time I load a page, I'd rather have it in the relevant content file so it sets automatically every time I include the page. How can I do this?
<div class="container">
<main class="content">
<div id="ctopspace"><h2><?php echo $pagetitle;></h2></div>
<div id="cleftspace"></div>
<?php include $content;?>
</main><!-- .content -->
</div><!-- .container-->
You can include your file above all other code and echo the respective content and title variables after you did a
$content = file_get_contents( your_content_file );
in your included file.
<?php include $contentFile ?>
//...later in the code
<h2><?= $pagetitle; ?></h2>
<div id="cleftspace"></div>
<?= $content; ?>
Note: The <?= is an open short tag for <?php echo. It may be disabled on your server.
Before
file_put_contents('page-title.txt', $pagetitle);
and in the $content file
<h2><?php echo #file_get_contents('page-title.txt'); ?></h2>
ps: # is just to skip file_exists step )