I'm new at the scene and I'm using Bootstrap 3 and this template for the first time. I need help with the navbar. In the template each content.html has their own navbar.
I need the navbar in a separate file, because I do not want to make changes in every single content.html file.
In my old site without Bootstrap I worked with:
<?php
include("includes/navigation.html");
Can someone help me?
You need configure to put your repeated html data's in a single file and include that one into another.
Lets simple,
if your Project structure like this
project(folder)
db(folder)
Dbconnection.php(file)
js(folder)
jquery.js(file)
css(folder)
bootstrap.css(file)
index.php(file)
page1.php(file)
page2.php(file)
Create a new file with contents of your navbar html tags alone.
For ex. in your nav-menu.php contains
<navbar>
......
......
</navbar>
and save them into new includes folder in your project. So now your project structure will be like this one,
project(folder)
db(folder)
Dbconnection.php(file)
js(folder)
jquery.js(file)
css(folder)
bootstrap.css(file)
includes(folder)
nav-menu.php(file)
index.php(file)
page1.php(file)
page2.php(file)
Now you should include this file(nav-menu.php) in all the template files using php include method, like
include("includes/nav-menu.php");
Add this above code in all of your common files.
You can use
<?php include("includes/navigation.php") ?>
in this project too.
Fastest way that comes to my mind is to just include the code you need to change for every page in a separate switch/case and call the different cases at the top of the page that is including the navbar.
Just note your navigation file needs to be .php, not .html like your example.
Related
Thanks for reading!
I am managing a header with links using a PHP include. It is within a folder /includes/header.php.
Here's an example of what header.php looks like:
<nav>
<ul>
<li>Home</li>
<li>Page</li>
</ul>
</nav>
When I add the include to a file within the root directory, like /index.php, I add it like so: <?php include_once("header.php"); ?>. This all works fine, and the links point where they need to.
When I do the same thing but with a file in a subdirectory, for instance a file called /foo/page.php I will add the include like this: <?php include_once("../includes/header.php"); ?> - this way it grabs the file correctly.
My problem is that all of the links in the header.php file aren't going where I want them to. I found some information about using a set environment function in .htaccess, but I don't know what to make of it.
If you have an answer to this problem I'd love to hear it! Thanks!
Start all the links in the header from the root web directory.
Just do;
"/index.html"
"/subdirectory/link.html"
So basically just start all the links with a forward slash, as without it, it will look for the page within its current directory.
You can set the base url in your HTML head.
Store the base url of your application in a config file or database and then use it to build absolute links not relative ones. For example you have a file like config.php:
<?php
$baseUrl = "http://yourdomain/yourapp/";
And in header.php:
<?php include_once("config.php"); ?>
Page
It may seem inconvenient having to edit a file in case you move your application, but this way your links will work in any directory any time, and as your application grows there will be some other things like DB access that also have to be changed if you move your application, and can be stored in the same config file.
I have a php page(index.php) that includes another html file(footer.html). Am using dreamweaver, and my problem is: The included file(footer.html) shows in the main file (index.php) in dreamweaver design view, but does not show in the browser preview. I have both the php files in the root directory, so they're at the same level. I have tried using require rather than include with the same result. My include code is:
<?php include("footer.html");?>
does anyone see something wrong off the bat?
Do realy exist file "footer.html"?
If no create them.
Is file "footer.html" in the same directory as "index.php"?
If no add it to the same directory.
Do you have any content in your "footer.html" file?
If no add any and check if it appears on page.
Is there a quicker way to make the navigation bar/buttons go onto all my pages of my site instead of going into each .php file and copying and pasting it. How would I link up the class="button" to another file so that it changes the whole navigation on my site?
Just use an include statement.
include 'nav.php';
on each page.
Nav.php could then contain nothing more than just the navigation.
First Separate your php file as header.php for header menu and footer.php for footer menu.
And then include the file in pages .
See this example for reference.
One of my view file in cake is getting very long, like 300+ lines already. And i find it very difficult to keep track of the understanding.
Is it a good idea to split them up into smaller files and then including them in the parent view file?
If its ok to be done,
In what extension should i create the smaller files? .ctp or .php?
Including them with require_once(view-child1.ext) should be fine, right?
Im fairly new to cakePHP. So i prefer advises from the experts over here. Please put me in the right direction.
EDIT
Thanks for the help guys.
I tried it. But i cant seem to pass the variable. echo $this->Element('reviews/view-goal',$history); Parent view shows and error saying undefined variable in that element.
Im calling the elements from this loop:
foreach($histories as $date => $history)
Cant pass $history. But $histories is being passed correctly.
You should make elements in View/Elements folder with .ctp extension.
This link would help you to make clean separation of your view files with the related/repeated code.
An element is basically a mini-view that can be included in other views, in layouts, and even within other elements. Elements can be used to make a view more readable, placing the rendering of repeating elements in its own file. They can also help you re-use content fragments in your application.
Elements live in the /app/View/Elements/ folder, and have the .ctp filename extension. They are output using the element method of the view:
<?php echo $this->element('helpbox'); //without extension ?>
You can pass variables from your view to the element.
In your view:
<?php echo $this->Element('reviews/view-goal', array('history' => $history));
In view-goal.ctp element you can directly access $history variable.
Yes, it is a very good idea. But don't use the normal require() of PHP.
CakePHP has a feature called "elements", a mechanism to put parts of a view into separate .ctp files. The files go in a special folder, View/Elements
You can include an element like this:
echo $this->element('sidebar/recent_comments');
If you need any variables inside the element, you need to pass them in an additional array parameter:
echo $this->element('sidebar/recent_comments', array('variable_name' => /* Variable content */));
In order to keep your view files small, you should also make sure that you put stuff that is shared by most pages (header, footer) into the Layout file. And obviously: keep JS and CSS in external files.
I split up my website using code refactoring with templates.
When I go to construct a basic link in my nav.php file or I suppose anywhere on the site for that mater, I need to grab multiple files as the site is templated -- let's assume for this example the user is going to services.php.
I believe I would need to attach all of these files in the link:
header.php
nav.php
pages/services.php
footer.php
How do I go about constructing a link to accomplish this? Could I make some sort of array and attach all of the files in the link and then loop through the array with a foreach statement and echo the results?
You don't grab the other files through the link. You link to one URL, which processes a single PHP file and that PHP file includes all of the other necessary files. For example, your link might be:
Services
Which will run services.php, which will then include the header, nav, footer, etc. For example services.php could look like this:
require( "header.php" );
// Some PHP code specific to servicer.php goes here.
require( "nav.php" );
// Some more PHP code specific to servicer.php goes here.
require( "footer.php" );
Using something like Smarty would be a nice and clean way of implemtning views in a MVC setup.