20i Reseller Hosting

Building a Site-Wide Template with PHP

Discussion in 'Scripts and Coding' started by accelerator, Mar 5, 2016.

  1. accelerator United Kingdom

    accelerator Well-Known Member

    Joined:
    Apr 2005
    Posts:
    7,461
    Likes Received:
    114
    How do you build a site-wide template with PHP? If for example you have a 1,000 page site, and you alter one of your structural divs, how can you have designed the site so that it's simple to do this just by amending a main template.

    At the moment I'm just using PHP includes, but that is not good enough. I need to take it a step further. I've been looking at PHP Object Orientation but I'm still not sure how to fundamentally design the site.

    Thanks
     
  2. Domain Forum

    Acorn Domains Elite Member

    Joined:
    1999
    Messages:
    Many
    Likes Received:
    Lots
    articles.co.uk
     
  3. monaghan United Kingdom

    monaghan Moderator Staff Member

    Joined:
    May 2007
    Posts:
    2,103
    Likes Received:
    70
    I presume the data for each of these 1000 pages is within a database?

    If so, then it's just a case of a little bit of logic at the start of the page load to get the right data and then to push it into the desired div on the page, when you want to change, then you only have the 1 place to make the change.

    You could also look at one of the PHP template libraries, again you'd need to wrap it with the necessary logic to collect the desired data.

    Alternatively, use WordPress, Joomla, Drupal or one of the various other content management systems out there to do the leg work for you.
     
  4. AssetDomains

    AssetDomains Well-Known Member

    Joined:
    Feb 2010
    Posts:
    3,046
    Likes Received:
    69
    As above don't reinvent the wheel if your site is going to be database driven use one of the CMS mentioned if your looking for a bit more control or to go in a little deeper a framework may be the way to go I'm a big fan of Laravel now but there are others

    http://www.sitepoint.com/best-php-framework-2015-sitepoint-survey-results/

    If you decide to take a look at laravel check out https://laracasts.com/ there a mix of free and paid content but the beginner stuff is mostly free and provides an excellent groundwork
     
  5. Skinner

    Skinner Well-Known Member

    Joined:
    Jul 2008
    Posts:
    4,658
    Likes Received:
    136
    I'm not convinced you need a "template system".

    If you have 1,000 pages of data, stored in a database, and limited PHP skills. I would just pull the data from the database, and build the page in the loop on the fly.

    If you modify the loop, it would modify every page, not strictly a template but would fill your needs. You can advance this, by still using includes for modules.
     
  6. accelerator United Kingdom

    accelerator Well-Known Member

    Joined:
    Apr 2005
    Posts:
    7,461
    Likes Received:
    114
    So you guys are saying I just need to create a database that also stores the unique content for each page? Then build a page template that retrieves the unique data for each page?

    So I can just use standard PHP includes and build a data-enabled template? That is enough for a 1,000 page site?

    I would rather custom code it, because then I know how it all works. Any tips for best practice?

    Thanks
     
  7. AssetDomains

    AssetDomains Well-Known Member

    Joined:
    Feb 2010
    Posts:
    3,046
    Likes Received:
    69
  8. RobM

    RobM Retired Member

    Joined:
    Mar 2012
    Posts:
    3,289
    Likes Received:
    469
    The same could also be done with one page of php and one template page. Never quite sure why people want to install megs of framework to 'simplify' an already simple task.
     
  9. accelerator United Kingdom

    accelerator Well-Known Member

    Joined:
    Apr 2005
    Posts:
    7,461
    Likes Received:
    114
  10. monaghan United Kingdom

    monaghan Moderator Staff Member

    Joined:
    May 2007
    Posts:
    2,103
    Likes Received:
    70
    The complex part will be classifying the data and building the necessary links/menus from it. You'll also need to have some form of admin system to modify content & menus.

    You really do seem to be reinventing a basic CMS with this, but if you do want to code your own the one of the popular php frameworks will give you a lot of building blocks. I'd personally not code from scratch unless you want this as a development project to learn php, in which case read a lot about code security SQL injection and so on
     
  11. AssetDomains

    AssetDomains Well-Known Member

    Joined:
    Feb 2010
    Posts:
    3,046
    Likes Received:
    69
    Agreed but these type of projects often evolve using a framework makes things easier to scale if need be in the future there are boilerplates for user systems, stripe payments and much much more. Also some inherent security / best practice built in.
    Pretty much any type of website you can think of building has probably been done multiple times in laravel so there's a massive community to call on and numerous git example etc.
    It could well be overkill for Chris for this project all depends on were he sees it going
     
  12. RobM

    RobM Retired Member

    Joined:
    Mar 2012
    Posts:
    3,289
    Likes Received:
    469
    Still unnecessary overheads. We (an advertising company of mine - not my sad attempts at webdesign) design all our CMS from scratch and they are far more versatile and less resource hungry using a created system than existing frameworks. But back to the OP - I think what he was asking doesn't necessitate installation of a complete framework and learning the convoluted syntax. If you want to send me a PM accelerator I'll have a look for you.
     
    Last edited: Mar 5, 2016
  13. spiderspider

    spiderspider Active Member

    Joined:
    Feb 2013
    Posts:
    676
    Likes Received:
    48
    From the first post, I can't quite work out if you're doing this to a new site, or your existing ones.

    If existing, how do you currently do things? Is each page in its own .php file (meaning any edit would need to be made in each file)?

    Do you currently have a database with the page data in?

    Without knowing what the starting blocks are, and advice is pie in the sky, and a guessing game.
     
  14. tifosi United Kingdom

    tifosi Well-Known Member

    Joined:
    Oct 2004
    Posts:
    3,424
    Likes Received:
    55
    Whether you use procedural PHP or OOP PHP is not the issue here I don't think, it's the structure of the site itself.

    Breaking the page down into modular constituent parts is important whether you're using basic HTML, HTML simply connected to a DB, a CMS such as WordPress, or an MVC framework such as codeigniter. Of course using an MVC you're going to need some skill & knowledge with OOP.

    Probably WordPress for all its failings is a solution here.
     
  15. accelerator United Kingdom

    accelerator Well-Known Member

    Joined:
    Apr 2005
    Posts:
    7,461
    Likes Received:
    114
    Thanks for the comments guys. It's partly to get back in to doing some coding, which I haven't done for a few years.
     
  16. spiderspider

    spiderspider Active Member

    Joined:
    Feb 2013
    Posts:
    676
    Likes Received:
    48
    If you're after help, can you answer the questions that have been asked. Otherwise its looking like your just posting for the sig link...... again.

    Or ask admin and mods to now close the thread.
     
  17. accelerator United Kingdom

    accelerator Well-Known Member

    Joined:
    Apr 2005
    Posts:
    7,461
    Likes Received:
    114
    To answer your questions ...

    From the first post, I can't quite work out if you're doing this to a new site, or your existing ones.

    - I will build a new template and apply it to any old sites I have. So I need to keep the .php extension.

    If existing, how do you currently do things? Is each page in its own .php file (meaning any edit would need to be made in each file)?

    - I built a .php template file, that has a lot of includes in it. I created each page from the template. However, the page structure itself is controlled at the page level, so I can only ever make site wide changes to the includes. So, I was just doing File / Save As to create all the pages, with no ability to make site wide structural changes.

    Do you currently have a database with the page data in?

    - I have a database for things like merchants and keywords. However, the unique page content is not in the database, and I guess it should be.
     
  18. Kari

    Kari Retired Member

    Joined:
    Jan 2015
    Posts:
    350
    Likes Received:
    4
    Agree with rob

    You can have as much content as you want stored in a database
    With one page and just define what content to pull based on variables

    The recruitment site in building has over 130k jobs and only 5 pages
    The URLs are seo friendly so appears to have individual pages for each job but all handles by htaccess and variables
     
  19. accelerator United Kingdom

    accelerator Well-Known Member

    Joined:
    Apr 2005
    Posts:
    7,461
    Likes Received:
    114
    So this is a coding technique question really ...

    Let's say I build a 10 page website, and most of the content being displayed on the site is pulled from a database.

    I want those 10 pages all to have a consistent page layout.

    1) So, for example, I would want the same main menu on each page. What is the best coding technique to use for this? I am familiar with the php include technique but is there a better one?

    2) This is the real question I want to ask ... I want all the 10 pages to have the same, easily modifiable layout. I know how to create a CSS page layout, but how do I code the page so that the layout itself can be easily maintained? Is this where something like PHP Objects comes in? I know I can't use a simple include, it needs to be something more sophisticated to deal with the complexity of the overall page layout. I want it such that if I had to make a modification to say the HTML of the main content div, this would be instantly reflected across all 10 pages.

    For 2), people are saying use a PHP Framework like Laravel, but I'm asking what coding techniques are fundamentally used here to achieve the easy maintenance goal.

    Thanks
     
  20. spiderspider

    spiderspider Active Member

    Joined:
    Feb 2013
    Posts:
    676
    Likes Received:
    48
    Asking that question shows that this is above you. That coupled with your other thread, you're really saying:

    'I have no idea, but am going to try the long way round doing things'.

    Seriously, get Wordpress or something similar, and play with the code.

    Or

    Go on a course and learn coding. Get a .php ebook and learn, practice, try and try again.

    Or

    Pay someone to do it for you.
     
  21. AssetDomains

    AssetDomains Well-Known Member

    Joined:
    Feb 2010
    Posts:
    3,046
    Likes Received:
    69