Jump to content

Website - Separate file for text

DJEB
Go to solution Solved by Dredgy,

@DJEB Super easy. DO NOT INSTALL a content management system such as wordpress. That will just make things much more difficult. Coding for wordpress without PHP knowledge is next to impossible. And coding with PHP knowledge you realise just how terribly written it is.

 

This can be done simply either with SSI (server side includes) or PHP (or any other language that your web host supports). I'll use PHP for this example.

 

1. First off, the file extension of the main page needs to be .php. and the website needs to be hosted on a server that has PHP. So we will call it index.php.

 

2. Create another directory called "pages" and in that directory have all of your text files. These can be any extension, either .htm or .txt will be fine - it will show as HTML code either way. The pages can be "home.htm", "contact.htm" and "whatever.htm".

 

So in index.php have all of your menus and the main template. 

 

At the top of your index.php file put this:

<?php$page = (isset($_GET['page'])) ? $_GET['page'] : 'home'; //If there is no page specified, the default will be 'home'.$pageUrl = 'pages/'. $page .'.htm'; ?>

Then, in index.php, wherever you want the content to be shown, do this:

<?php include_once($pageUrl); ?>

So this means that the only file you need to do all the templating is "index.php". So anyone viewing your page will by default be taken to the home page. When you are linking to another page, the link needs to be like this:

<a href="index.php?page=contact">Contact Us</a>

If you need any help getting that implemented, just ask (but tag me as I don't tend to check replies otherwise)

Hi everybody,

 

I'm building a website for my parents at the moment and I was wondering if I can have separate files for the text. So the HTML-file (or PHP if it has to be something else than HTML) contains just the general stuff of the website (like the header, menu and footer) and in a separate file is the text that will be shown on the website. The HTML-file would have to contain some code which will use whatever text there is written in the separate file and show it on the website. 

 

I would like to do this to make it easier to manage and update the website without having to enter and alter the HTML-file were I or my parents could screw the entire website up. 

 

I hope (and think) it is possible, but I would like to have some help with it since I don't think I know enough of HTML (and zero of php to be honest) to do this on my own. I have googled my problem, but that didn't help me, so I'm hoping any of you guys can. 

 

Thanks in advance,

DJEB

Link to comment
Share on other sites

Link to post
Share on other sites

@DJEB Super easy. DO NOT INSTALL a content management system such as wordpress. That will just make things much more difficult. Coding for wordpress without PHP knowledge is next to impossible. And coding with PHP knowledge you realise just how terribly written it is.

 

This can be done simply either with SSI (server side includes) or PHP (or any other language that your web host supports). I'll use PHP for this example.

 

1. First off, the file extension of the main page needs to be .php. and the website needs to be hosted on a server that has PHP. So we will call it index.php.

 

2. Create another directory called "pages" and in that directory have all of your text files. These can be any extension, either .htm or .txt will be fine - it will show as HTML code either way. The pages can be "home.htm", "contact.htm" and "whatever.htm".

 

So in index.php have all of your menus and the main template. 

 

At the top of your index.php file put this:

<?php$page = (isset($_GET['page'])) ? $_GET['page'] : 'home'; //If there is no page specified, the default will be 'home'.$pageUrl = 'pages/'. $page .'.htm'; ?>

Then, in index.php, wherever you want the content to be shown, do this:

<?php include_once($pageUrl); ?>

So this means that the only file you need to do all the templating is "index.php". So anyone viewing your page will by default be taken to the home page. When you are linking to another page, the link needs to be like this:

<a href="index.php?page=contact">Contact Us</a>

If you need any help getting that implemented, just ask (but tag me as I don't tend to check replies otherwise)

Link to comment
Share on other sites

Link to post
Share on other sites

@xXxYOLOxSWAGxXx_420BlazeIt

I didn't want to install any cms, because I was told (and found out myself by trying) that it is hard to make a template if you don't know php. 

 

@Dredgy

Thank you very much!

I've tried it a couple of minutes ago for the first page and it worked perfectly right away!

I will now make all of the pages and make correct links to them. 

 

These can be any extension, either .htm or .txt will be fine

 

I tried it first with .txt extension and that didn't work. Then I changed it to .htm and now it does work.

 

Link to comment
Share on other sites

Link to post
Share on other sites

@Dredgy

Thank you very much!

I've tried it a couple of minutes ago for the first page and it worked perfectly right away!

I will now make all of the pages and make correct links to them. 

 

 

I tried it first with .txt extension and that didn't work. Then I changed it to .htm and now it does work.

 

Sorry about that, if you want to use a different extension, go to this line:

$pageUrl = 'pages/'. $page .'.htm'; 

And simply change htm to whatever extension you want.

 

Glad I was able to help :)

Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...

@Dredgy

 

I've got it working for all pages now. For every page I had set a title to be shown in the title bar, but (of course) every page now has the same text in the title bar. Do you know how I can make the text in the title bar different for every page?

 

Thanks in advance!

Link to comment
Share on other sites

Link to post
Share on other sites

I agree that using Drupal or Wordpress is bad if you want to do your own specific code or styling, but if you would like to try out a CMS in future there is a great new one just in beta called October. It basically gives you very neat sort of IDE for developing Laravel framework sites but also provides plugin functionality in case you don't want to do your own coding to get more complex features in your site. The plugin list is small at the moment, but hopefully it will soon grow. I have been playing around with it and so far I really like it.

Link to comment
Share on other sites

Link to post
Share on other sites

@DJEB - without a database, the only real way to do it is check the page name and assign a variable to it.

Place this below $pageTitle = etc.

And before the "?>"

switch($page){    case 'contact':          $title = 'Contact Us';    break;    case 'about':          $title = 'About Us';    break;    default:          $title = 'Home';    break; }

Sorry if there's any errors but I'm writing on my iPad. Basically what that does is checks the value of the page variable and sees what page it's on. If no page is set, then it defaults to home. You can add as many cases as you like. Then in the HTML, something like this:

<title><?php echo $title; ?> - Your Website Name</title>
You can set up other page specific variables like this as well.
Link to comment
Share on other sites

Link to post
Share on other sites

@Dredgy

 

Thanks for helping me again!

I put it in my index.php file, added as many cases as I needed and now it's all working.

 

 

I'm glad that there are people out there actually helping others instead of just saying 'use a cms' or something like that.

Link to comment
Share on other sites

Link to post
Share on other sites

@DJEB

 

What I've done is the most efficient, rather than the most basic method of doing it. If you need actual lessons in rudimentary PHP or don't understand what a bit of code actually does, please let me know via PM.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×